PHP Common Cache Technology Summary _php tutorial

Source: Internet
Author: User
first, the data cache

In this case, the data cache refers to the database query cache, each time the page is accessed, the corresponding cache data will be detected, if not, connect to the database, get the data, and the query results are serialized and saved to the 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 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.

Second, page cache

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 capability).

Three, time trigger 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.

Iv. Content Triggering cache

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

Five, static cache

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.
The above content is a code-level solution, I direct CP other framework, too lazy to change, the content is almost, very easy to do, and will be used in several ways, but the following content is the server-side caching scheme, non-code level, to have multi-party cooperation to do.

VI, Memory cache

memcached is a high-performance, distributed memory object caching system for reducing database load and increasing access speed in dynamic applications.
Here's an example of memcached:
copy code code as follows:
!--? php $memcache = new Memcache;
$memcache->connect (' localhost ', 11211) or Die ("Could not Connect");
$version = $memcache->getversion ();
echo "Server ' 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:
Copy the Code code as follows:
$sql = ' SELECT * from users ';
$key = MD5 ($sql); Memcached Object identifiers
if (! ( $datas = $MC->get ($key))) {
If the cached data is not obtained in memcached, 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);

Vii. buffers, accelerators for PHP

There are eaccelerator, APC, Phpa,xcache, this will not say, search a bunch of a bunch of, see for yourself, know that this thing is OK.

Eight, MySQL cache

This is also non-code level, the classic database is used in this way, look at the following running time, 0.09xxx and the like
I put a paragraph according to the Blue Guy to modify the latter part of the My.ini bar, 2G MyISAM table can be around 0.05S, it is said that he changed back and forth for a year.

Copy the Code code as follows:
[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

Nine, 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:
Copy CodeThe code is as follows:

#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 Apache Mod_proxy:

Copy the Code code as follows:

ServerName jb51.net
ServerAdmin admin@zxsv.com
# Reverse Proxy setting
proxypass/http://jb51.net:8080/
proxypassreverse/http://jb51.net: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/jb51_net_access_log combined



X. DNS Polling

Bind is an open source DNS server software, this to say it is big, their own search, you know there is this thing on the line.

I know that there are chinacache and other major stations to do so, said the simple point is a multi-server, the same page or file cache to different servers, according to the North-South automatic resolution to the relevant server.

http://www.bkjia.com/PHPjc/765164.html www.bkjia.com true http://www.bkjia.com/PHPjc/765164.html techarticle first, the data cache refers to the data cache is the database query cache, each time you visit the page, will first detect the corresponding cache data exists, if not exist, connect ...

  • 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.