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.
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.
General Cache Technology
Data Cache: The data cache 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.
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)
Time-triggered cache:
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.
Content trigger cache:
When data is inserted or updated, the cache is forcibly updated.
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 the cache solution on the server. Non-code-level solutions can only be implemented through multi-party cooperation.
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 );
?>
Php Buffer:
There are eaccelerator, apc, phpa, and xcache. You don't need to talk about this. You can search for a bunch of them and read them by yourself. If you know what it is, OK.
MYSQL cache:
This is not code-level. The classic database uses this method. See the following running time, such as 0.09xxx.
My post part is modified according to the guy in blue. ini. The MYISAM table of 2 GB can be around 5s. It is said that he has changed it for almost a year.
[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
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;
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;
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;
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;
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;
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;
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;
Include proxy_setting.conf;
}
Access_log logs/wsi. log test_com;
}
#
Server {
Listen 10.10.230: 80;
Server_name * .test.com;
Location ~ ^/NginxStatus /{
Stub_status on;
Access_log off;
}
Location /{
Proxy_pass;
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;
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;
Mod_proxy example:
ServerName
ServerAdmin admin@zxsv.com
# Reverse proxy setting
ProxyPass/: 8080/
ProxyPassReverse/: 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
I will not explain the SQUID example. There are too many articles written on the Internet. You can search for them by yourself.
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.