A summary of common PHP Cache technologies. I. data cache the data cache here refers to the database query cache. each time you access the page, it will first check whether the cache data exists. if it does not exist, it will connect
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:
The code is as follows:
$ 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:
The code is as follows:
$ 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.
The 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:
The code is as follows:
# 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;
}
}
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:
The code is as follows:
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
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.
The data cache mentioned here refers to the database query cache. each time you access the page, the system first checks whether the cache data exists. if it does not exist, it connects...