PHP Common Caching technology Rollup _php tips

Source: Internet
Author: User

One, data caching

The data cache mentioned here refers to the database query cache, every time you visit the page, you will first detect the existence of the corresponding cached data, if it does not exist, connect the database, get the data, and the query results serialized after the save to the file, the same query results will be directly from the cache table or file.

The broadest example looks at the Discuz search function, caches the result ID in a table, and searches for the cached table the next time the same keyword is searched.
In a common way, when multiple tables are associated, the contents of the table to generate an array to save the main tables in a field, the need to decompose the array, the advantage is to read only a table, the disadvantage is that two data synchronization will be more than a few steps, the database is always a bottleneck, with hard disk for speed, is this key point.

Second, the page cache

Every time you visit the page, you will first detect the corresponding cache page file exists, if it does not exist, connect the database, get the data, display the page and generate cached page files, so the next time the page file will play a role. (the template engine and some common online caching classes typically have this feature).

Third, time trigger cache

Check that the file exists and that the timestamp is less than the set expiration time, or update the cache if the timestamp of the file modification is greater than the current timestamp minus the expiration time.

Iv. Content Trigger Caching

Forces an update of the cache when data is inserted or updated.

V. Static caching

Static caching here refers to static, directly generate HTML or XML and other text files, there are updates to regenerate once, suitable for the page not too changed, this is not said.
The above content is a code-level solution, I direct CP other framework, also lazy to change, the content is similar, very easy to do, and will be used in several ways, but the following content is the server side of the cache scheme, not code-level, to have a variety of cooperation to achieve.

Six, 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 a memcached example:

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, All) 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 Code code as follows:

<?php
$sql = ' SELECT * from users ';
$key = MD5 ($sql); Memcached Object identifier
if (!) ( $datas = $MC->get ($key))) {
When cached data is not obtained in memcached, a database query is used to get the recordset.
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);

Seven, the PHP buffer, accelerator

There are eaccelerator, APC, Phpa,xcache, this is not to say it, search a pile of a bunch of, you see, know that there is this thing OK.

Eight, MySQL cache

This is not a code level, the classic database is used in this way, look at the following running time, 0.09xxx and so on
I posted a paragraph based on the Blue Guy modified part of the My.ini bar, 2G MyISAM table can be around 0.05S, it is said that he changed back and forth for nearly a year.

Copy 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

Ix. Web caching based on reverse proxy

such as Nginx,squid,mod_proxy (apache2 above and divided into Mod_proxy and Mod_cache)
Examples of Nginx:

Copy Code code as follows:

<nginx.conf>
#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;
}
}
<conf/proxy_setting.conf>
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 Code code as follows:

<virtualhost *>
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
</VirtualHost>


10. DNS Polling

Bind is an open source DNS server software, this to say is big, own search go, we know this thing on the line.

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

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.