PHP enterprise application cache technology details

Source: Internet
Author: User
We have discussed the PHP caching technology in depth before, and mainly talked about data caching. Data cache mainly refers to the database query cache. when accessing the page every time, the corresponding cache data will be checked first... we have discussed the PHP caching technology in depth before, and mainly talked about data caching. Data cache mainly 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, close the query...




We have discussed the PHP caching technology in depth before, and mainly talked about data caching. Data cache mainly 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.

Memory cache

Memcached is a high-performance, distributed memory object cache system that reduces database loads in Dynamic Applications and improves access speeds.



   $ 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:



   $ 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

For example, eaccelerator, apc, phpa, and xcache.

MySQL cache

This is not code-level. the classic database uses this method. See the following running time, such as 0.09xxx.



[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:



# 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 http: // bspimgsvr;

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 http: // bspstylesvr;

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 http: // bsphelpsvr;

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 http: // bspadminsvr;

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 http: // bspbuyersvr;

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 http: // bspsellersvr;

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 http: // bspwsisvr;

Include proxy_setting.conf;

}

Access_log logs/wsi. log test_com;

}

# Www.test.com

Server {

Listen 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 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 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 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 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

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.