Home Backend Development PHP Tutorial Detailed explanation of caching technology under PHP

Detailed explanation of caching technology under PHP

Jul 25, 2016 am 09:11 AM

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

See all articles