An in-depth explanation of common caching technologies for PHP enterprise-level applications_PHP Tutorial

WBOY
Release: 2016-07-22 09:02:15
Original
744 people have browsed it

Universal 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 cache data exists. If it does not exist, connect to the database and get data, and serialize the query results and save them to the 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.

As 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. 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 caching:

Every time you access 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 next time The page file comes into play when accessed. (Template engines and some common cache classes on the Internet usually have this function)

Time-triggered caching:

Check whether the file exists and the timestamp is less than the set expiration time, if the file modification timestamp is less than the current timestamp If the expiration timestamp is large, then use the cache, otherwise update the cache.

Content-triggered cache:

Force the cache to be updated when data is inserted or updated.

Static cache:

The static cache mentioned here refers to static, which directly generates text files such as HTML or .

The above content is a code-level solution. I directly CP other frameworks and am too lazy to change. The content is similar, it is easy to do, and can be used in several ways, but the following content is a server-side caching solution. Non-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:

<ol class="dp-c">
<li class="alt"><span><span><?php  </span></span></li><li><span class="vars">$memcache</span><span> = </span><span class="keyword">new</span><span> Memcache;  </span></li><li class="alt"><span class="vars">$memcache</span><span>->connect(‘localhost’, 11211) </span><span class="keyword">or</span><span> </span><span class="keyword">die</span><span> (“Could not connect”);  </span></span></li>
<li>
<span class="vars">$version</span><span> = </span><span class="vars">$memcache</span><span>->getVersion();  </span>
</li>
<li class="alt">
<span class="func">echo</span><span> “Server’s version: “.</span><span class="vars">$version</span><span>.”n”;  </span>
</li>
<li>
<span class="vars">$tmp_object</span><span> = </span><span class="keyword">new</span><span> stdClass;  </span>
</li>
<li class="alt">
<span class="vars">$tmp_object</span><span>->str_attr = ‘test’;  </span>
</li>
<li>
<span class="vars">$tmp_object</span><span>->int_attr = 123;  </span>
</li>
<li class="alt">
<span class="vars">$memcache</span><span>->set(‘key’, </span><span class="vars">$tmp_object</span><span>, false, 10) </span><span class="keyword">or</span><span> </span><span class="keyword">die</span><span> (“Failed to save data at the server”);  </span>
</li>
<li>
<span class="func">echo</span><span> “Store data in the cache (data will expire in 10 seconds)n”;  </span>
</li>
<li class="alt">
<span class="vars">$get_result</span><span> = </span><span class="vars">$memcache</span><span>->get(‘key’);  </span>
</li>
<li>
<span class="func">echo</span><span> “Data from the cache:n”;  </span>
</li>
<li class="alt">
<span>var_dump(</span><span class="vars">$get_result</span><span>);  </span>
</li>
<li><span>?> </span></li>
</ol>
Copy after login

Example of reading the library:

<ol class="dp-c">
<li class="alt"><span><span><?php  </span></span></li><li><span class="vars">$sql</span><span> = &lsquo;SELECT * FROM users&rsquo;;  </span></li><li class="alt"><span class="vars">$key</span><span> = md5(</span><span class="vars">$sql</span><span>);   </span><span class="comment">//memcached 对象标识符 </span><span> </span></li><li><span class="keyword">if</span><span> ( !(</span><span class="vars">$datas</span><span> = </span><span class="vars">$mc</span><span>->get(</span><span class="vars">$key</span><span>)) ) {  </span></span></li>
<li class="alt">
<span class="comment">//  在 memcached 中未获取到缓存数据,则使用数据库查询获取记录集。 </span><span> </span>
</li>
<li>
<span class="func">echo</span><span> “n”.</span><span class="func">str_pad</span><span>(‘Read datas from MySQL.’, 60, ‘_’).”n”;  </span>
</li>
<li class="alt">
<span class="vars">$conn</span><span> = mysql_connect(‘localhost’, ‘test’, ‘test’);  </span>
</li>
<li><span>mysql_select_db(‘test’);  </span></li>
<li class="alt">
<span class="vars">$result</span><span> = mysql_query(</span><span class="vars">$sql</span><span>);  </span>
</li>
<li>
<span class="keyword">while</span><span> (</span><span class="vars">$row</span><span> = mysql_fetch_object(</span><span class="vars">$result</span><span>))  </span>
</li>
<li class="alt">
<span class="vars">$datas</span><span>[] = </span><span class="vars">$row</span><span>;  </span>
</li>
<li>
<span class="comment">//  将数据库中获取到的结果集数据保存到 memcached 中,以供下次访问时使用。 </span><span> </span>
</li>
<li class="alt">
<span class="vars">$mc</span><span>->add(</span><span class="vars">$key</span><span>, </span><span class="vars">$datas</span><span>);  </span>
</li>
<li>
<span>} </span><span class="keyword">else</span><span> {  </span>
</li>
<li class="alt">
<span class="func">echo</span><span> “n”.</span><span class="func">str_pad</span><span>(‘Read datas from memcached.’, 60, ‘_’).”n”;  </span>
</li>
<li><span>}  </span></li>
<li class="alt">
<span>var_dump(</span><span class="vars">$datas</span><span>);  </span>
</li>
<li><span>?> </span></li>
</ol>
Copy after login

PHP buffer:

There are eaccelerator, apc, PHPa, xcache, I won’t talk about this one, search it There are a lot of them, take a look for yourself, it’s OK if you know that this thing exists

MYSQL Cache:

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

I will post a section of my.ini modified by the blue guy. The 2G MYISAM table can be around 0.05S. It is said that he has changed it for almost a year

<ol class="dp-c">
<li class="alt"><span><span>[client]  </span></span></li>
<li><span>……  </span></li>
<li class="alt">
<span class="keyword">default</span><span>-character-set=gbk  </span>
</li>
<li>
<span class="keyword">default</span><span>-storage-engine=MYISAM  </span>
</li>
<li class="alt"><span>max_connections=600  </span></li>
<li><span>max_connect_errors=500  </span></li>
<li class="alt"><span>back_log=200  </span></li>
<li><span>interactive_timeout=7200  </span></li>
<li class="alt"><span>query_cache_size=64M  </span></li>
<li><span>……  </span></li>
<li class="alt"><span>table_cache=512  </span></li>
<li><span>……  </span></li>
<li class="alt"><span>myisam_max_sort_file_size=100G  </span></li>
<li><span>myisam_max_extra_sort_file_size=100G  </span></li>
<li class="alt"><span>myisam_sort_buffer_size=128M  </span></li>
<li><span>key_buffer_size=1024M  </span></li>
<li class="alt"><span>read_buffer_size=512M  </span></li>
<li><span>……  </span></li>
<li class="alt"><span>thread_concurrency=8 </span></li>
</ol>
Copy after login

Web based on reverse proxy Caching:

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

Examples of NGINX

<ol class="dp-c">
<li class="alt"><span><span><nginx.conf>  </span></span></li>
<li><span>#user  nobody;  </span></li>
<li class="alt"><span>worker_processes  4;  </span></li>
<li>
<span class="func">error_log</span><span>  logs/error.log crit;  </span>
</li>
<li class="alt"><span>pid        logs/nginx.pid;  </span></li>
<li><span>worker_rlimit_nofile 10240;  </span></li>
<li class="alt"><span>events {  </span></li>
<li>
<span class="keyword">use</span><span> epoll;  </span>
</li>
<li class="alt"><span>worker_connections  51200;  </span></li>
<li><span>}  </span></li>
<li class="alt"><span>http {  </span></li>
<li>
<span class="keyword">include</span><span>       mime.types;  </span>
</li>
<li class="alt"><span>default_type  application/octet-stream;  </span></li>
<li><span>sendfile    on;  </span></li>
<li class="alt"><span>keepalive_timeout 65;  </span></li>
<li><span>tcp_nodelay on;  </span></li>
<li class="alt"><span># server pool  </span></li>
<li><span>upstream bspfrontsvr {  </span></li>
<li class="alt"><span>server 10.10.10.224:80   weight=1;  </span></li>
<li><span>server 10.10.10.221:80   weight=1;  </span></li>
<li class="alt"><span>}  </span></li>
<li><span> </span></li>
<li class="alt"><span>upstream bspimgsvr {  </span></li>
<li><span>server 10.10.10.201:80   weight=1;  </span></li>
<li class="alt"><span>}  </span></li>
<li><span> </span></li>
<li class="alt"><span>upstream bspstylesvr {  </span></li>
<li><span>server 10.10.10.202:80   weight=1;  </span></li>
<li class="alt"><span>}  </span></li>
<li><span> </span></li>
<li class="alt"><span>upstream bsphelpsvr {  </span></li>
<li><span>server 10.10.10.204:80   weight=1;  </span></li>
<li class="alt"><span>}  </span></li>
<li><span> </span></li>
<li class="alt"><span>upstream bspwsisvr {  </span></li>
<li><span>server 10.10.10.203:80   weight=1;  </span></li>
<li class="alt"><span>}  </span></li>
<li><span> </span></li>
<li class="alt"><span>upstream bspadminsvr {  </span></li>
<li><span>server 10.10.10.222:80   weight=1;  </span></li>
<li class="alt"><span>}  </span></li>
<li><span> </span></li>
<li class="alt"><span>upstream bspbuyersvr {  </span></li>
<li><span>server 10.10.10.223:80   weight=1;  </span></li>
<li class="alt"><span>}  </span></li>
<li><span> </span></li>
<li class="alt"><span>upstream bspsellersvr {  </span></li>
<li><span>server 10.10.10.225:80   weight=1;  </span></li>
<li class="alt"><span>}  </span></li>
<li><span>upstream  bsploginsvr  {  </span></li>
<li class="alt"><span>server 10.10.10.220:443  weight=1;  </span></li>
<li><span>}  </span></li>
<li class="alt"><span>upstream  bspregistersvr  {  </span></li>
<li><span>server 10.10.10.220:80  weight=1;  </span></li>
<li class="alt"><span>}  </span></li>
<li>
<span>log_format  test_com  ‘</span><span class="vars">$remote_addr</span><span> – </span><span class="vars">$remote_user</span><span> [</span><span class="vars">$time_local</span><span>] “</span><span class="vars">$request</span><span>” ‘  </span>
</li>
<li class="alt">
<span>‘</span><span class="vars">$status</span><span> </span><span class="vars">$body_bytes_sent</span><span> “</span><span class="vars">$http_referer</span><span>” “</span><span class="vars">$http_user_agent</span><span>” ‘;  </span>
</li>
<li><span>#——————————————————————–  </span></li>
<li class="alt"><span>#img.test.com  </span></li>
<li><span>server {  </span></li>
<li class="alt"><span>listen       10.10.10.230:80;  </span></li>
<li><span>server_name  img.test.com;  </span></li>
<li class="alt"><span>location / {  </span></li>
<li>
<span>proxy_pass      http:</span><span class="comment">//bspimgsvr; </span><span> </span>
</li>
<li class="alt">
<span class="keyword">include</span><span>         proxy_setting.conf;  </span>
</li>
<li><span>}  </span></li>
<li class="alt"><span>access_log  logs/img.log  test_com;  </span></li>
<li><span>}  </span></li>
<li class="alt"><span> </span></li>
<li><span>#style.test.com  </span></li>
<li class="alt"><span>server {  </span></li>
<li><span>listen       10.10.10.230:80;  </span></li>
<li class="alt"><span>server_name  style.test.com;  </span></li>
<li><span>location / {  </span></li>
<li class="alt">
<span>proxy_pass      http:</span><span class="comment">//bspstylesvr; </span><span> </span>
</li>
<li>
<span class="keyword">include</span><span>         proxy_setting.conf;  </span>
</li>
<li class="alt"><span>}  </span></li>
<li><span>access_log  logs/style.log  test_com;  </span></li>
<li class="alt"><span>}  </span></li>
<li><span> </span></li>
<li class="alt"><span>#help.test.com  </span></li>
<li><span>server {  </span></li>
<li class="alt"><span>listen       10.10.10.230:80;  </span></li>
<li><span>server_name  help.test.com;  </span></li>
<li class="alt"><span>location / {  </span></li>
<li>
<span>proxy_pass      http:</span><span class="comment">//bsphelpsvr; </span><span> </span>
</li>
<li class="alt">
<span class="keyword">include</span><span>         proxy_setting.conf;  </span>
</li>
<li><span>}  </span></li>
<li class="alt"><span>access_log  logs/help.log  test_com;  </span></li>
<li><span>}  </span></li>
<li class="alt"><span> </span></li>
<li><span>#admin.test.com  </span></li>
<li class="alt"><span>server {  </span></li>
<li><span>listen       10.10.10.230:80;  </span></li>
<li class="alt"><span>server_name  admin.test.com;  </span></li>
<li><span>location / {  </span></li>
<li class="alt">
<span>proxy_pass      http:</span><span class="comment">//bspadminsvr; </span><span> </span>
</li>
<li>
<span class="keyword">include</span><span>         proxy_setting.conf;  </span>
</li>
<li class="alt"><span>}  </span></li>
<li><span>access_log  logs/admin.log  test_com;  </span></li>
<li class="alt"><span>}  </span></li>
<li><span> </span></li>
<li class="alt"><span>#buyer.test.com  </span></li>
<li><span>server {  </span></li>
<li class="alt"><span>listen       10.10.10.230:80;  </span></li>
<li><span>server_name  buyer.test.com;  </span></li>
<li class="alt"><span>location / {  </span></li>
<li>
<span>proxy_pass      http:</span><span class="comment">//bspbuyersvr; </span><span> </span>
</li>
<li class="alt">
<span class="keyword">include</span><span>         proxy_setting.conf;  </span>
</li>
<li><span>}  </span></li>
<li class="alt"><span>access_log  logs/buyer.log  test_com;  </span></li>
<li><span>}  </span></li>
<li class="alt"><span> </span></li>
<li><span>#seller.test.com  </span></li>
<li class="alt"><span>server {  </span></li>
<li><span>listen       10.10.10.230:80;  </span></li>
<li class="alt"><span>server_name  seller.test.com;  </span></li>
<li><span>location / {  </span></li>
<li class="alt">
<span>proxy_pass      http:</span><span class="comment">//bspsellersvr; </span><span> </span>
</li>
<li>
<span class="keyword">include</span><span>         proxy_setting.conf;  </span>
</li>
<li class="alt"><span>}  </span></li>
<li><span>access_log  logs/seller.log  test_com;  </span></li>
<li class="alt"><span>}  </span></li>
<li><span>#wsi.test.com  </span></li>
<li class="alt"><span>server {  </span></li>
<li><span>listen       10.10.10.230:80;  </span></li>
<li class="alt"><span>server_name  wsi.test.com;  </span></li>
<li><span>location / {  </span></li>
<li class="alt">
<span>proxy_pass      http:</span><span class="comment">//bspwsisvr; </span><span> </span>
</li>
<li>
<span class="keyword">include</span><span>         proxy_setting.conf;  </span>
</li>
<li class="alt"><span>}  </span></li>
<li><span>access_log  logs/wsi.log  test_com;  </span></li>
<li class="alt"><span>}  </span></li>
<li><span>#www.test.com  </span></li>
<li class="alt"><span>server {  </span></li>
<li><span>listen       10.10.10.230:80;  </span></li>
<li class="alt"><span>server_name  www.test.com   *.test.com;  </span></li>
<li><span>location ~ ^/NginxStatus/ {  </span></li>
<li class="alt"><span>stub_status on;  </span></li>
<li><span>access_log off;  </span></li>
<li class="alt"><span>}  </span></li>
<li><span>location / {  </span></li>
<li class="alt">
<span>proxy_pass      http:</span><span class="comment">//bspfrontsvr; </span><span> </span>
</li>
<li>
<span class="keyword">include</span><span>         proxy_setting.conf;  </span>
</li>
<li class="alt"><span>}  </span></li>
<li><span>access_log  logs/www.log  test_com;  </span></li>
<li class="alt"><span>error_page   500 502 503 504  /50x.html;  </span></li>
<li><span>location = /50x.html {  </span></li>
<li class="alt"><span>root   html;  </span></li>
<li><span>}  </span></li>
<li class="alt"><span>}  </span></li>
<li><span>#login.test.com  </span></li>
<li class="alt"><span>server {  </span></li>
<li><span>listen       10.10.10.230:443;  </span></li>
<li class="alt"><span>server_name  login.test.com;  </span></li>
<li><span>ssl                  on;  </span></li>
<li class="alt"><span>ssl_certificate      cert.pem;  </span></li>
<li><span>ssl_certificate_key  cert.key;  </span></li>
<li class="alt"><span>ssl_session_timeout  5m;  </span></li>
<li><span>ssl_protocols  SSLv2 SSLv3 TLSv1;  </span></li>
<li class="alt">
<span>ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+</span><span class="func">EXP</span><span>;  </span>
</li>
<li><span>ssl_prefer_server_ciphers   on;  </span></li>
<li class="alt"><span>location / {  </span></li>
<li>
<span>proxy_pass        https:</span><span class="comment">//bsploginsvr; </span><span> </span>
</li>
<li class="alt">
<span class="keyword">include</span><span>         proxy_setting.conf;  </span>
</li>
<li><span>}  </span></li>
<li class="alt"><span>access_log  logs/login.log  test_com;  </span></li>
<li><span>}  </span></li>
<li class="alt">
<span>#login.test.com </span><span class="keyword">for</span><span> register  </span>
</li>
<li><span>server {  </span></li>
<li class="alt"><span>listen       10.10.10.230:80;  </span></li>
<li><span>server_name  login.test.com;  </span></li>
<li class="alt"><span>location / {  </span></li>
<li>
<span>proxy_pass        http:</span><span class="comment">//bspregistersvr; </span><span> </span>
</li>
<li class="alt">
<span class="keyword">include</span><span>         proxy_setting.conf;  </span>
</li>
<li><span>}  </span></li>
<li class="alt"><span>access_log  logs/register.log  test_com;  </span></li>
<li><span>}  </span></li>
<li class="alt"><span> </span></li>
<li><span>}  </span></li>
<li class="alt"><span><conf/proxy_setting.conf>  </span></li>
<li><span>proxy_redirect          off;  </span></li>
<li class="alt">
<span>proxy_set_header        Host </span><span class="vars">$host</span><span>;  </span>
</li>
<li>
<span>proxy_set_header        X-Real-IP </span><span class="vars">$remote_addr</span><span>;  </span>
</li>
<li class="alt">
<span>proxy_set_header        X-Forwarded-For </span><span class="vars">$proxy_add_x_forwarded_for</span><span>;  </span>
</li>
<li><span>client_max_body_size    10m;  </span></li>
<li class="alt"><span>client_body_buffer_size 128k;  </span></li>
<li><span>proxy_connect_timeout   90;  </span></li>
<li class="alt"><span>proxy_send_timeout      90;  </span></li>
<li><span>proxy_read_timeout      90;  </span></li>
<li class="alt"><span>proxy_buffer_size       4k;  </span></li>
<li><span>proxy_buffers           4 32k;  </span></li>
<li class="alt"><span>proxy_busy_buffers_size 64k;  </span></li>
<li><span>proxy_temp_file_write_size 64k;  </span></li>
<li class="alt"><span>mod_proxy的例子:  </span></li>
<li><span> </span></li>
<li class="alt"><span><VirtualHost *>  </span></li>
<li><span>ServerName www.zxsv.com  </span></li>
<li class="alt"><span>ServerAdmin admin@zxsv.com  </span></li>
<li><span># reverse proxy setting  </span></li>
<li class="alt">
<span>ProxyPass / http:</span><span class="comment">//www.zxsv.com:8080/ </span><span> </span>
</li>
<li>
<span>ProxyPassReverse / http:</span><span class="comment">//www.zxsv.com:8080/ </span><span> </span>
</li>
<li class="alt"><span># cache dir root  </span></li>
<li>
<span>CacheRoot “/</span><span class="keyword">var</span><span>/www/proxy”  </span>
</li>
<li class="alt"><span># max cache storage  </span></li>
<li><span>CacheSize 50000000  </span></li>
<li class="alt"><span># hour: every 4 hour  </span></li>
<li><span>CacheGcInterval 4  </span></li>
<li class="alt"><span># max page expire time: hour  </span></li>
<li><span>CacheMaxExpire 240  </span></li>
<li class="alt"><span># Expire time = (now – last_modified) * CacheLastModifiedFactor  </span></li>
<li><span>CacheLastModifiedFactor 0.1  </span></li>
<li class="alt"><span># defalt expire tag: hour  </span></li>
<li><span>CacheDefaultExpire 1  </span></li>
<li class="alt"><span># force complete after precent of content retrived: 60-90%  </span></li>
<li><span>CacheForceCompletion 80  </span></li>
<li class="alt"><span>CustomLog /usr/local/apache/logs/dev_access_log combined  </span></li>
<li><span></VirtualHost> </span></li>
</ol>
Copy after login

I won’t explain the examples of SQUID. There are too many written about this on the Internet. Search for it yourself

DNS polling:

BIND is an open source DNS server software. This is a big deal to mention. Search it for yourself. Everyone knows that this thing exists.

I know that some big 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.

http://www.cnblogs.com/cnblogs20112011/archive/2011/01/27/1946286.html


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445846.htmlTechArticle Universal caching technology Data caching: The data caching mentioned here refers to the database query cache. Every time you access a page, it will first detect whether the corresponding cached data exists. If not...
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!