Home  >  Article  >  Backend Development  >  Detailed explanation of caching technology under PHP

Detailed explanation of caching technology under PHP

WBOY
WBOYOriginal
2016-07-25 09:11:47849browse

Common caching technology

Data cache: The data cache mentioned here refers to the database query cache. Every time you access the page, it will first detect whether the corresponding cached data exists. If it does not exist, connect to the database, get the data, and put the query results. After serialization, save it to a file. In the future, the same query results will be obtained directly from the cache table or file.

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.

For a common method, when multiple tables are associated, generate an array and save the contents in the attached table to a field in the main table. When necessary, decompose the array. This has the advantage of only reading one table, but has two disadvantages. Data synchronization will take many more steps, and the database is always the bottleneck. Trading hard disk for speed is the key point in this.

Page Caching: 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 play a role 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 triggered 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. At the 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:

  1. $memcache = new Memcache;
  2. $memcache->connect('localhost', 11211) or die ("Could not connect");
  3. $version = $memcache-> getVersion();
  4. echo "Server's version: ".$version."n";
  5. $tmp_object = new stdClass;
  6. $tmp_object->str_attr = 'test';
  7. $tmp_object->int_attr = 123;
  8. $ memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
  9. echo "Store data in the cache (data will expire in 10 seconds)n";
  10. $get_result = $memcache->get('key');
  11. echo "Data from the cache:n";
  12. var_dump($get_result);
  13. ?>
Copy code

Example of reading library :

  1. $sql = 'SELECT * FROM users';
  2. $key = md5($sql); //memcached object identifier
  3. if ( !($datas = $mc-> get($key)) ) {
  4. // If cached data is not obtained in memcached, use database query to obtain the record set.
  5. echo "n".str_pad('Read datas from MySQL.', 60, '_')."n";
  6. $conn = mysql_connect('localhost', 'test', 'test');
  7. mysql_select_db(' test');
  8. $result = mysql_query($sql);
  9. while ($row = mysql_fetch_object($result))
  10. $datas[] = $row;
  11. // Save the result set data obtained from the database to memcached in for your next visit.
  12. $mc->add($key, $datas);
  13. } else {
  14. echo "n".str_pad('Read datas from memcached.', 60, '_')."n";
  15. }
  16. var_dump ($datas);
  17. ?>
Copy code

php buffer: There are eaccelerator, apc, phpa, xcache, let’s not talk about these. Search a bunch of them and see for yourself. If you know that there is such a thing, it’s OK

MYSQL Cache: This is also considered non-code level. Classic databases use this method. Look at the running time below, it is 0.09xxx and so on. I am posting the part of my.ini modified by the guy in blue. The 2G MYISAM table can be around 0.05S. It is said that he modified it for almost a year

Code copy box

  1. [client]
  2. default-character-set=gbk
  3. default-storage-engine=MYISAM
  4. max_connections=600
  5. max_connect_errors=500
  6. back_log=200
  7. interactive_timeout=7200
  8. query _cache_size=64M
  9. … …
  10. table_cache=512
  11. myisam_max_sort_file_size=100G
  12. myisam_max_extra_sort_file_size=100G
  13. myisam_sort_buffer_size=128M
  14. key_buffer_size=1024M
  15. read_buffer_size=512M
  16. thread_ concurrency=8
Copy code

Reverse proxy based web caching: Such as Nginx, SQUID, mod_proxy (apache2 and above are divided into mod_proxy and mod_cache) NGINX example

  1. #user nobody;
  2. worker_processes 4;
  3. error_log logs/error.log crit;
  4. pid logs/nginx.pid;
  5. worker_rlimit_nofile 10240;
  6. events {
  7. use epoll;
  8. worker_connections 51200;
  9. }
  10. http {
  11. include mime.types;
  12. default_type application/octet-stream;
  13. sendfile on;
  14. keepalive_timeout 65;
  15. tcp_nodelay on;
  16. # server pool
  17. upstream bspfrontsvr {
  18. server 10.10.10.224:80 weight=1;
  19. server 10.10.10.221:80 weight=1;
  20. }

  21. upstream bspimgsvr {

  22. server 10.10.10.201:80 weight=1;
  23. }

  24. upstream bspstylesvr {

  25. server 10.10.10.202:80 weight=1;
  26. }

  27. upstream bsphelpsvr {

  28. server 10.10.10.204:80 weight=1;
  29. }

  30. upstream bspwsisvr {

  31. server 10.10.10.203:80 weight=1;
  32. }

  33. upstream bspadminsvr {

  34. server 10.10.10.222:80 weight=1;
  35. }

  36. upstream bspbuyersvr {

  37. server 10.10.10.223:80 weight=1;
  38. }

  39. upstream bspsellersvr {

  40. server 10.10.10.225:80 weight=1;
  41. }
  42. upstream bsploginsvr {
  43. server 10.10.10.220:443 weight=1;
  44. }
  45. upstream bspregistersvr {
  46. server 10.10.10.220:80 weight=1;
  47. }
  48. log_format test_com '$remote_addr - $remote_user [$time_local] "$request" '
  49. '$status $body_bytes_sent "$http_referer" "$http_user_agent" ';
  50. #--------------------------------------------------------------------
  51. #img.test.com
  52. server {
  53. listen 10.10.10.230:80;
  54. server_name img.test.com;
  55. location / {
  56. proxy_pass http://bspimgsvr;
  57. include proxy_setting.conf;
  58. }
  59. access_log logs/img.log test_com;
  60. }

  61. #style.test.com

  62. server {
  63. listen 10.10.10.230:80;
  64. server_name style.test.com;
  65. location / {
  66. proxy_pass http://bspstylesvr;
  67. include proxy_setting.conf;
  68. }
  69. access_log logs/style.log test_com;
  70. }

  71. #help.test.com

  72. server {
  73. listen 10.10.10.230:80;
  74. server_name help.test.com;
  75. location / {
  76. proxy_pass http://bsphelpsvr;
  77. include proxy_setting.conf;
  78. }
  79. access_log logs/help.log test_com;
  80. }

  81. #admin.test.com

  82. server {
  83. listen 10.10.10.230:80;
  84. server_name admin.test.com;
  85. location / {
  86. proxy_pass http://bspadminsvr;
  87. include proxy_setting.conf;
  88. }
  89. access_log logs/admin.log test_com;
  90. }

  91. #buyer.test.com

  92. server {
  93. listen 10.10.10.230:80;
  94. server_name buyer.test.com;
  95. location / {
  96. proxy_pass http://bspbuyersvr;
  97. include proxy_setting.conf;
  98. }
  99. access_log logs/buyer.log test_com;
  100. }

  101. #seller.test.com

  102. server {
  103. listen 10.10.10.230:80;
  104. server_name seller.test.com;
  105. location / {
  106. proxy_pass http://bspsellersvr;
  107. include proxy_setting.conf;
  108. }
  109. access_log logs/seller.log test_com;
  110. }
  111. #wsi.test.com
  112. server {
  113. listen 10.10.10.230:80;
  114. server_name wsi.test.com;
  115. location / {
  116. proxy_pass http://bspwsisvr;
  117. include proxy_setting.conf;
  118. }
  119. access_log logs/wsi.log test_com;
  120. }
  121. #www.test.com
  122. server {
  123. listen 10.10.10.230:80;
  124. server_name www.test.com *.test.com;
  125. location ~ ^/NginxStatus/ {
  126. stub_status on;
  127. access_log off;
  128. }
  129. location / {
  130. proxy_pass http://bspfrontsvr;
  131. include proxy_setting.conf;
  132. }
  133. access_log logs/www.log test_com;
  134. error_page 500 502 503 504 /50x.html;
  135. location = /50x.html {
  136. root html;
  137. }
  138. }
  139. #login.test.com
  140. server {
  141. listen 10.10.10.230:443;
  142. server_name login.test.com;
  143. ssl on;
  144. ssl_certificate cert.pem;
  145. ssl_certificate_key cert.key;
  146. ssl_session_timeout 5m;
  147. ssl_protocols SSLv2 SSLv3 TLSv1;
  148. ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
  149. ssl_prefer_server_ciphers on;
  150. location / {
  151. proxy_pass https://bsploginsvr;
  152. include proxy_setting.conf;
  153. }
  154. access_log logs/login.log test_com;
  155. }
  156. #login.test.com for register
  157. server {
  158. listen 10.10.10.230:80;
  159. server_name login.test.com;
  160. location / {
  161. proxy_pass http://bspregistersvr;
  162. include proxy_setting.conf;
  163. }
  164. access_log logs/register.log test_com;
  165. }

  166. }

  167. proxy_redirect off;
  168. proxy_set_header Host $host;
  169. proxy_set_header X-Real-IP $remote_addr;
  170. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  171. client_max_body_size 10m;
  172. client_body_buffer_size 128k;
  173. proxy_connect_timeout 90;
  174. proxy_send_timeout 90;
  175. proxy_read_timeout 90;
  176. proxy_buffer_size 4k;
  177. proxy_buffers 4 32k;
  178. proxy_busy_buffers_size 64k;
  179. proxy_temp_file_write_size 64k;

Copy code

Example of mod_proxy:

  1. ServerName www.zxsv.com
  2. ServerAdmin admin@zxsv.com
  3. # reverse proxy setting
  4. ProxyPass / http://www.zxsv.com:8080/
  5. ProxyPassReverse / http ://www.zxsv.com:8080/
  6. # cache dir root
  7. CacheRoot "/var/www/proxy"
  8. # max cache storage
  9. CacheSize 50000000
  10. # hour: every 4 hour
  11. CacheGcInterval 4
  12. # max page expire time : hour
  13. CacheMaxExpire 240
  14. # Expire time = (now - last_modified) * CacheLastModifiedFactor
  15. CacheLastModifiedFactor 0.1
  16. # defalt expire tag: hour
  17. CacheDefaultExpire 1
  18. # force complete after precent of content retrived: 60-90%
  19. CacheForceCompletion 8 0
  20. CustomLog/ usr/local/apache/logs/dev_access_log combined
Copy code

For examples of SQUID, you can refer to here.

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.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn