Summary of common PHP cache Technologies

Source: Internet
Author: User

I. Data Cache

The data cache mentioned here refers to the database query cache. Each time you access the page, it first checks whether the cache data exists. If it does not exist, it connects to the database to obtain the data, serialize the query results and save them to the file. Later, the same query results will be obtained directly from the cache table or file.

The most widely used example is the Discuz search function. The result ID is cached in a table and the table is searched in the cache next time when the same keywords are searched.
For example, when multiple tables are joined, an array generated in the appendix is saved to a field in the master table. If necessary, the array is decomposed, the advantage is read-only tables. The disadvantage is that two data synchronization steps are many more steps, and the database is always the bottleneck. Changing the disk speed is the key point.

Ii. Page Cache

Each time you access the page, the system first checks whether the corresponding cached page file exists. If it does not exist, it connects to the database to obtain data, displays the page, and generates cache page files at the same time, in this way, the page file will play a role in the next visit. (The template engine and some common cache classes on the Internet usually have this function ).

Iii. cache triggered by Time

Check whether the file exists and the timestamp is earlier than the set expiration time. If the modified timestamp of the file is greater than the current timestamp minus the expiration timestamp, use the cache; otherwise, update the cache.

Iv. Content-triggered Cache

When data is inserted or updated, the cache is forcibly updated.

V. Static Cache

The static cache mentioned here refers to static, which directly generates HTML, XML, and other text files. It is re-generated once when there is an update. This is suitable for pages that do not change much.
The above content is a code-level solution. I am not too lazy to change the content of the CP framework. The content is similar and easy to implement. It can be used together in several ways, however, the following content is a server-side cache solution. Non-code-level solutions can only be achieved through multi-party cooperation.

Vi. Memory Cache

Memcached is a high-performance, distributed memory object cache system that reduces database loads in dynamic applications and improves access speeds.
Here is an example of Memcached:
Copy codeThe Code is as follows:
<? Php
$ Memcache = new Memcache;
$ Memcache-> connect ('localhost', 11211) or die ("cocould not connect ");
$ Version = $ memcache-> getVersion ();
Echo "Server's version:". $ version. "\ n ";
$ Tmp_object = new stdClass;
$ Tmp_object-> str_attr = 'test ';
$ Tmp_object-> int_attr = 123;
$ Memcache-> set ('key', $ tmp_object, false, 10) or die ("Failed to save data at the server ");
Echo "Store data in the cache (data will expire in 10 seconds) \ n ";
$ Get_result = $ memcache-> get ('key ');
Echo "Data from the cache: \ n ";
Var_dump ($ get_result );

Example of database reading:
Copy codeThe Code is as follows:
<? Php
$ SQL = 'select * FROM users ';
$ Key = md5 ($ SQL); // memcached object identifier
If (! ($ Datas = $ mc-> get ($ key ))){
// If no cached data is obtained in memcached, use database query to obtain the record set.
Echo "n". str_pad ('read datas from MySQL. ', 60,' _ '). "n ";
$ Conn = mysql_connect ('localhost', 'test', 'test ');
Mysql_select_db ('test ');
$ Result = mysql_query ($ SQL );
While ($ row = mysql_fetch_object ($ result ))
$ Datas [] = $ row;
// Save the result set data obtained from the database to memcached for the next access.
$ Mc-> add ($ key, $ datas );
} Else {
Echo "n". str_pad ('read datas from memcached. ', 60,' _ '). "n ";
}
Var_dump ($ datas );

VII. PHP buffer and accelerator

If you have eaccelerator, apc, phpa, and xcache, you don't need to talk about it anymore. Search for a bunch of them and read them. If you know what it is, OK.

VIII. MYSQL Cache

This is not code-level. The classic database uses this method. See the following running time, such as 0.09xxx.
I recommend that you modify the part my. ini according to the guy in blue. The MYISAM table of 2 GB can be around 5s. It is said that he has changed it for almost a year.

Copy codeThe Code is as follows:
[Client]
......
Default-character-set = gbk
Default-storage-engine = MYISAM
Max_connections = 600
Max_connect_errorrs = 500
Back_log = 200
Interactive_timeout = 7200
Query_cache_size = 64 M
......
Table_cache = 512
......
Myisam_max_sort_file_size = 100G
Myisam_max_extra_sort_file_size = 100G
Myisam_sort_buffer_size = 128 M
Key_buffer_size = 1024 M
Read_buffer_size = 512 M
......
Thread_concurrency = 8

9. Reverse Proxy-based Web Cache

For example, Nginx, SQUID, mod_proxy (apache2 and above are also divided into mod_proxy and mod_cache)
NGINX example:
Copy codeThe Code is as follows:
<Nginx. conf>
# User nobody;
Worker_processes 4;
Error_log logs/error. log crit;
Pid logs/nginx. pid;
Worker_rlimit_nofile 10240;
Events {
Use epoll;
Worker_connections 51200;
}
Http {
Include mime. types;
Default_type application/octet-stream;
Sendfile on;
Keepalive_timeout 65;
Tcp_nodelay on;
# Server pool
Upstream bspfrontsvr {
Server 10.10.10.weight: 80 weight = 1;
Server 10.10.10.221: 80 weight = 1;
}
Upstream bspimgsvr {
Server 10.10.10.201: 80 weight = 1;
}
Upstream bspstylesvr {
Server 10.10.10.202: 80 weight = 1;
}
Upstream bsphelpsvr {
Server 10.10.10.204: 80 weight = 1;
}
Upstream bspwsisvr {
Server 10.10.10.203: 80 weight = 1;
}
Upstream bspadminsvr {
Server 10.10.10.222: 80 weight = 1;
}
Upstream bspbuyersvr {
Server 10.10.10.223: 80 weight = 1;
}
Upstream bspsellersvr {
Server 10.10.10.225: 80 weight = 1;
}
Upstream bsploginsvr {
Server 10.10.10.220: 443 weight = 1;
}
Upstream bspregistersvr {
Server 10.10.10.220: 80 weight = 1;
}
Log_format test_com '$ remote_addr-$ remote_user [$ time_local] "$ request "'
'$ Status $ body_bytes_sent "$ http_referer" "$ http_user_agent "';
#--------------------------------------------------------------------
# Img.test.com
Server {
Listen 10.10.230: 80;
Server_name img.test.com;
Location /{
Proxy_pass http: // bspimgsvr;
Include proxy_setting.conf;
}
Access_log logs/img. log test_com;
}
# Style.test.com
Server {
Listen 10.10.230: 80;
Server_name style.test.com;
Location /{
Proxy_pass http: // bspstylesvr;
Include proxy_setting.conf;
}
Access_log logs/style. log test_com;
}
# Help.test.com
Server {
Listen 10.10.230: 80;
Server_name help.test.com;
Location /{
Proxy_pass http: // bsphelpsvr;
Include proxy_setting.conf;
}
Access_log logs/help. log test_com;
}
# Admin.test.com
Server {
Listen 10.10.230: 80;
Server_name admin.test.com;
Location /{
Proxy_pass http: // bspadminsvr;
Include proxy_setting.conf;
}
Access_log logs/admin. log test_com;
}
# Buyer.test.com
Server {
Listen 10.10.230: 80;
Server_name buyer.test.com;
Location /{
Proxy_pass http: // bspbuyersvr;
Include proxy_setting.conf;
}
Access_log logs/buyer. log test_com;
}
# Seller.test.com
Server {
Listen 10.10.230: 80;
Server_name seller.test.com;
Location /{
Proxy_pass http: // bspsellersvr;
Include proxy_setting.conf;
}
Access_log logs/seller. log test_com;
}
# Wsi.test.com
Server {
Listen 10.10.230: 80;
Server_name wsi.test.com;
Location /{
Proxy_pass http: // bspwsisvr;
Include proxy_setting.conf;
}
Access_log logs/wsi. log test_com;
}
# Www.test.com
Server {
Listen 10.10.230: 80;
Server_name www.test.com * .test.com;
Location ~ ^/NginxStatus /{
Stub_status on;
Access_log off;
}
Location /{
Proxy_pass http: // bspfrontsvr;
Include proxy_setting.conf;
}
Access_log logs/www. log test_com;
Error_page 500 502 503 x.html;
Location =/50x.html {
Root html;
}
}
# Login.test.com
Server {
Listen 10.10.230: 443;
Server_name login.test.com;
Ssl on;
Ssl_certificate cert. pem;
Ssl_certificate_key cert. key;
Ssl_session_timeout 5 m;
Ssl_protocols SSLv2 SSLv3 TLSv1;
Ssl_ciphers ALL :! ADH :! EXPORT56: RC4 + RSA: + HIGH: + MEDIUM: + LOW: + SSLv2: + EXP;
Ssl_prefer_server_ciphers on;
Location /{
Proxy_pass https: // bsploginsvr;
Include proxy_setting.conf;
}
Access_log logs/login. log test_com;
}
# Login.test.com for register
Server {
Listen 10.10.230: 80;
Server_name login.test.com;
Location /{
Proxy_pass http: // bspregistersvr;
Include proxy_setting.conf;
}
Access_log logs/register. log test_com;
}
}
<Conf/proxy_setting.conf>
Proxy_redirect off;
Proxy_set_header Host $ host;
Proxy_set_header X-Real-IP $ remote_addr;
Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
Client_max_body_size 10 m;
Client_body_buffer_size 128 k;
Proxy_connect_timeout 90;
Proxy_send_timeout 90;
Proxy_read_timeout 90;
Proxy_buffer_size 4 k;
Proxy_buffers 4 32 k;
Proxy_busy_buffers_size 64 k;
Proxy_temp_file_write_size 64 k;

Example of apache mod_proxy:

Copy codeThe Code is as follows:
<VirtualHost *>
ServerName jb51.net
ServerAdmin admin@zxsv.com
# Reverse proxy setting
ProxyPass/http://jb51.net: 8080/
ProxyPassReverse/http://jb51.net: 8080/
# Cache dir root
CacheRoot "/var/www/proxy"
# Max cache storage
CacheSize 50000000
# Hour: every 4 hour
CacheGcInterval 4
# Max page expire time: hour
CacheMaxExpire 240
# Expire time = (now-last_modified) * CacheLastModifiedFactor
CacheLastModifiedFactor 0.1
# Defalt expire tag: hour
CacheDefaultExpire 1
# Force complete after precent of content retrived: 60-90%
CacheForceCompletion 80
CustomLog/usr/local/apache/logs/jb51_net_access_log combined
</VirtualHost>


10. DNS round robin

BIND is an open-source DNS server software, which is too big to be mentioned. You can search for it by yourself.

I know that chinacache and other major sites are doing this. Simply put, they are multiple servers and cache the same page or file on different servers, it is automatically resolved to the relevant server by North and South.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.