PHP Enterprise-level application caching technology detailed

Source: Internet
Author: User
Previously, we explored the PHP cache technology in depth, which mainly referred to the data cache. Data cache mainly refers to the database query cache, each time access to the page, will first detect the corresponding cache data exists, if not exist, connect the database, get data, and the query knot ...




Previously, we explored the PHP cache technology in depth, which mainly referred to the data cache. Data cache mainly refers to the database query cache, each time access to the page, will first detect the corresponding cache data exists, if not exist, connect to the database, get the data, and the query results are serialized after saving to the file, the same query results are 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 the next time the same keyword is searched.

For a common method, multi-table association, the schedule of the contents of the array is saved to a field in the main table, the need for the array decomposition, the advantage is only read a table, the disadvantage is that two data synchronization will be more than a few steps, the database is always the bottleneck, with the hard disk speed, is the key point.

Page Caching

Each time you visit the page, will detect the corresponding cache page file exists, if not exist, connect to the database, get data, display the page and generate the cache page file at the same time, so the next visit to the page file will play a role. (the template engine and some common caching classes on the Web typically have this feature)

Time-Triggered cache

Check that the file exists and that the timestamp is less than the expiration time of the setting, and if the timestamp of the file modification is greater than the current timestamp minus the expiration timestamp, then cache is used, otherwise the cache is updated.

Content-Triggered caching

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

Static caching

Static caching refers to static, directly generated HTML or XML and other text files, there is an update when re-generated once, suitable for the less changing pages, this does not say.

Memory Cache

Memcached is a high-performance, distributed memory object caching system for reducing database load and increasing access speed in dynamic applications.



<?php
$memcache = new Memcache;

$memcache->connect (' localhost ', 11211) or Die ("Could 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, or Die ("Failed-to-save data at the server");

echo "Store data in the cache (data would expire in seconds) \ n";

$get _result = $memcache->get (' key ');

echo "Data from the cache:\n";

Var_dump ($get _result);

?>



Examples of reading libraries:



<?php
$sql = ' SELECT * from users ';

$key = MD5 ($sql); Memcached Object identifiers

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

The cached data is not fetched in memcached, then the recordset is obtained using a database query

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;

Saves the result set data obtained in the database to memcached for use on the next visit

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

} else {

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

}

Var_dump ($datas);

?>



Buffer for PHP

Like Eaccelerator,apc,phpa,xcache and so on.

MySQL Cache

This is also a non-code level, the classic database is used in this way, look at the following running time, 0.09xxx and so on.



[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



Reverse proxy-based Web caching

such as Nginx,squid,mod_proxy (Apache2 and above are divided into mod_proxy and Mod_cache)

Examples 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;

Sendfile on;

Keepalive_timeout 65;

Tcp_nodelay on;

# Server Pool

Upstream Bspfrontsvr {

Server 10.10.10.224: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.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;

}

#help. test.com

server {

Listen 10.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.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.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.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 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;



Examples of mod_proxy:



<virtualhost *>

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

</VirtualHost>
  • Related Article

    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.