Universal caching technology
Data cache: The data cache mentioned here refers to the database query cache. Every time a page is accessed, it will first detect whether the corresponding cached data exists. If it does not exist, connect to the database, obtain the data, and serialize the query results. Save it to a file, and the same query results will be obtained directly from the cache table or file in the future.
The most widely used example is the search function of Discuz, which caches the result ID into a table and searches the cache table first when searching for the same keyword next time.
As a common method, when multiple tables are associated, the contents in the attached table are generated into an array and saved in a field of the main table. When necessary, the array is decomposed. The advantage of this is that only one table can be read, but the disadvantage is that two data Synchronization will take many more steps, and the database is always the bottleneck. Trading the hard disk for speed is the key point in this.
Page cache:
Every time a page is accessed, it will first detect whether the corresponding cached page file exists. If it does not exist, it will connect to the database, get the data, display the page and generate a cached page file at the same time, so that the page file will come into play the next time you visit. . (Template engines and some common cache classes on the Internet usually have this function)
Time triggered cache:
Check whether the file exists and the timestamp is less than the set expiration time. If the file modification timestamp is greater than the current timestamp minus the expiration timestamp, then use the cache, otherwise update the cache.
Content triggers caching:
Force the cache to be updated when data is inserted or updated.
Static cache:
The static cache mentioned here refers to static, directly generating text files such as HTML or XML, and regenerating them when there are updates. It is suitable for pages that do not change much, so I won’t talk about it here.
The above content is a code-level solution. I directly CP other frameworks and am too lazy to change. The content is almost the same. It is easy to do and can be used in several ways. However, the following content is a server-side caching solution, not code. level, it requires the cooperation of multiple parties to achieve it
Memory cache:
Memcached is a high-performance, distributed memory object caching system used to reduce database load and improve access speed in dynamic applications.
Here is 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);
?>
Example of reading the library:
<?php
$sql = 'SELECT * FROM users';
$key = md5($sql); //memcached object identifier
if ( !($datas = $mc->get($key)) ) {
// If cached data is not 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 use during the next access.
$mc->add($key, $datas);
} else {
echo "n".str_pad('Read datas from memcached.', 60, '_')."n";
}
var_dump($datas);
?>
php buffer:
There are eaccelerator, apc, phpa, and xcache. I won’t talk about these. Search a bunch of them and see for yourself. It’s OK if you know that there is such a thing.
MYSQL cache:
This is also considered non-code level. Classic databases use this method. Look at the running time below. I will post a section like 0.09xxx based on the modified part of my.ini based on the blue guy. A 2G MYISAM table can be used. Around 0.05S, it is said that he changed it for almost 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=64M
…
table_cache=512
…
myisam_max_sort_file_size=100G
myisam_max_extra_sort_file_size=100G
myisam_sort_buffer_size=128M
key_buffer_size=1024M
read_buffer_size=512M
…
thread_concurrency=8
Reverse proxy based web caching:
Such as Nginx, SQUID, mod_proxy (apache2 and above are divided into mod_proxy and mod_cache)
NGINX example
<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 {
roothtml;
}
}
#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;
Example of mod_proxy:
<VirtualHost *>
ServerName www.zxsv.com
ServerAdmin [email protected]
# 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 hours
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%
CacheForceCompletion80
CustomLog /usr/local/apache/logs/dev_access_log combined
</VirtualHost>
I won’t explain the example of SQUID. There are too many articles about this on the Internet. You can search for it yourself.
DNS polling:
BIND is an open source DNS server software. This is a big deal to mention. Just search it yourself and everyone knows that it exists.
I know that some large websites such as chinacache do this. To put it simply, it is multi-server. The same page or file is cached on different servers and automatically parsed to the relevant server according to the north and south.