search
HomeBackend DevelopmentPHP TutorialDetailed explanation of PHP enterprise-level application caching technology

We have discussed PHP caching technology in depth before, which mainly mentioned data caching. Data caching mainly refers to database query caching. Every time you access a page, it will first detect whether the corresponding cached data exists. If it does not exist, connect to the database, get the data, and complete the query...​​​​




We have discussed PHP caching technology in depth, which mainly mentioned data caching. Data caching mainly refers to database query caching. Every time you access a page, 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 and save them to a file. The same query results are 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, 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 disadvantages are two. There will be many more steps to synchronize the data. The database is always the bottleneck. Trading the hard disk for speed is the key point.

 Page Cache

 Every time you visit a page, it will first detect whether the corresponding cached page file exists. If it does not exist, connect to the database, get the data, display the page and generate a cached page file at the same time, so that the next time you visit The page file comes into play. (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 timestamp of the file modification is less than the current timestamp minus the expiration timestamp If it is large, then use the cache, otherwise update the cache.

 Content-triggered cache

 When data is inserted or updated, the cache is forced to be 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.

 Memory Cache

 Memcached is a high-performance, distributed memory object caching system used to reduce database load and improve access speed in dynamic applications.



    $memcache = new Memcache;

  $memcache->connect('localhost', 11211) or die (“Could not connect”); 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:



  $sql = 'SELECT * FROM users';

 $key = md5($sql); //memcached object identifier

 if ( !($ datas = $mc->get($key)) ) {

   // If the cached data is not obtained in memcached, use the 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 Use

  $mc->add($key, $datas);

 } else {

  echo "n".str_pad('Read datas from memcached.', 60, '_')."n";

  }

 var_dump($datas);

 ?>



  PHP buffer

  Such as eaccelerator, apc, phpa, xcache, etc.

 MySQL Cache

This is also considered non-code level. Classic databases use this method. Look at the running time below, it is like 0.09xxx.



 [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



  Web cache based on reverse proxy

Such as Nginx, SQUID, mod_PRoxy (apache2 and above are divided into mod_proxy and mod_cache)

  Example of NGINX:



 #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; lay on;

  # server pool

  upstream bspfrontsvr {

 server 10.10.10.224:80 weight=1;

 server 10.10.10.221:80 weight=1; 0 weight=1;

 }

  upstream bspstylesvr {

 server 10.10.10.202:80 weight=1;

  }

 upstream bsphelpsvr {

 server 10.10.10.204:80 weight=1;

  }

 up stream bspwsisvr {

 server 10.10.10.203:80 weight =1;

  }

 upstream bspadminsvr {

 server 10.10.10.222:80 weight=1; 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;
server helpsvr;

 include proxy_setting .conf;

  }

  access_log logs/help.log test_com; 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 log s/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; 

  } 

  } 

   

  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的例子: 



   

  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 

  

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
Optimize PHP Code: Reducing Memory Usage & Execution TimeOptimize PHP Code: Reducing Memory Usage & Execution TimeMay 10, 2025 am 12:04 AM

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),