Common caching techniques for PHP enterprise applications

  • 2020-03-31 21:32:48
  • OfStack

General caching technique
Data cache data cache: here refers to a database query cache, each time you visit the page, will check whether the cached data of the corresponding first, if not, just connect to the database, get the data, and serialized to save the query results to a file, after the same query result is obtained directly from the cache table or file.
The most widely used example is Discuz's search feature, which caches the result ids into a table and searches the cache table the next time you search for the same keyword.
For the commonly used method, many a table, then the contents of the table to generate an array to a field of the main table, array decomposition when needed, so are the benefits of read only one table, harm is more than two data synchronization will many steps, the database is always the bottleneck, use the hard drive speed, is the key point.
Page cache:
Every time you visit a page, you will first check whether the corresponding cached page file exists. If not, you will connect to the database, get the data, display the page and generate the cached page file at the same time, so that the next time you visit the page file will play a role. (template engines and some caching classes that are common on the web often have this functionality)
Time trigger cache:
Check that the file exists and that the timestamp is less than the set expiration time. If the timestamp for the file changes is larger than the current timestamp minus the expiration timestamp, use the cache, otherwise update the cache.
Content trigger cache:
Force the cache to be updated when data is inserted or updated.
Static cache:
By static caching, I mean staticization, direct generation of text files such as HTML or XML, regenerated once there is an update, suitable for pages that do not change much.
The above content is code level solution, I directly CP other frameworks, also lazy to change, the content is the same, it is easy to do, and will be used in several ways together, but the following content is the server-side caching scheme, non-code level, to have multi-party cooperation
Memory cache:
Memcached is a high-performance, distributed memory object caching system for reducing database load and increasing access speed in dynamic applications.
Here's an example of Memcached:
 
<?php 
$memcache = new Memcache; 
$memcache->connect( ' localhost', 11211) or die ( " Could 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); 
?> 

Examples of library reading:
 
<?php 
$sql =  ' SELECT * FROM users'; 
$key = md5($sql); //Memcached object identifier
if ( !($datas = $mc->get($key)) ) { 
//If no cached data is retrieved in memcached, the recordset is retrieved using a database query.
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 from the database to memcached for next access.
$mc->add($key, $datas); 
} else { 
echo  " n " .str_pad( ' Read datas from memcached.', 60,  ' _'). " n " ; 
} 
var_dump($datas); 
?> 

PHP buffer:
There's eaccelerator, there's apc, there's phpa, there's xcache, and I'm not going to do that, I'm going to do a bunch of searches, I'm going to do a bunch of searches
MYSQL cache:
This is also non-code level, and that's the way the classic database works, and look at the running time, 0.09 XXX or something like that
I pasted the section according to the blue guy after the modification of my. Ini bar, the 2G MYISAM table can be around 0.05S, it is said that he has changed about a year
[client]
...
Default - character - set = GBK
Default - storage - engine = MYISAM
Max_connections = 600
Max_connect_errors = 500
Back_log = 200
Interactive_timeout = 7200
Query_cache_size = 64 m
...
Your table_cache = 512
...
Myisam_max_sort_file_size = 100 g
Myisam_max_extra_sort_file_size = 100 g
Myisam_sort_buffer_size = 128 m
Key_buffer_size = 1024 m
Read_buffer_size = 512 m
...
Thread_concurrency = 8
Web caching based on reverse proxy:
For example, Nginx, SQUID, mod_PRoxy(apache2 above is divided into mod_PRoxy and mod_cache)
An example of NGINX
 
<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.224: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.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.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.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.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.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.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.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.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 504 /50x.html; 
location = /50x.html { 
root html; 
} 
} 
#login.test.com 
server { 
listen 10.10.10.230:443; 
server_name login.test.com; 
ssl on; 
ssl_certificate cert.pem; 
ssl_certificate_key cert.key; 
ssl_session_timeout 5m; 
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.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 10m; 
client_body_buffer_size 128k; 
proxy_connect_timeout 90; 
proxy_send_timeout 90; 
proxy_read_timeout 90; 
proxy_buffer_size 4k; 
proxy_buffers 4 32k; 
proxy_busy_buffers_size 64k; 
proxy_temp_file_write_size 64k; 
mod_proxy Example:  
<VirtualHost *> 
ServerName www.zxsv.com 
ServerAdmin admin@zxsv.com 
# reverse proxy setting 
ProxyPass / http://www.zxsv.com:8080/ 
ProxyPassReverse / http://www.zxsv.com: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/dev_access_log combined 
</VirtualHost> 

And I won't go into the SQUID case, there's a lot of stuff on the web, so you can search for it yourself
DNS round-robin:
BIND is an open source DNS server software, this is big to say, your own search, you know there is this thing on the line.
I know that chinacache and other large stations are doing this, say simple point is multiple servers, the same page or file cache to different servers, according to the north and south automatically resolved to the relevant server.

Related articles: