Maison  >  Article  >  développement back-end  >  PHP企业级应用之常见缓存技术深入解读_PHP教程

PHP企业级应用之常见缓存技术深入解读_PHP教程

WBOY
WBOYoriginal
2016-07-22 09:02:15747parcourir

普遍缓存技术

数据缓存:这里所说的数据缓存是指数据库查询缓存,每次访问页面的时候,都会先检测相应的缓存数据是否存在,如果不存在,就连接数据库,得到数据, 并把查询结果序列化后保存到文件中,以后同样的查询结果就直接从缓存表或文件中获得。

用的最广的例子看Discuz的搜索功能,把结果ID缓存到一个表中,下次搜索相同关键字时先搜索缓存表。

举个常用的方法,多表关联的时候,把附表中的内容生成数组保存到主表的一个字段中,需要的时候数组分解一下,这样的好处是只读一个表,坏处就是两个 数据同步会多不少步骤,数据库永远是瓶颈,用硬盘换速度,是这个的关键点。

页面缓存:

每次访问页面的时候,都会先检测相应的缓存页面文件是否存在,如果不存在,就连接数据库,得到数据,显示页面并同时生成缓存页面文件,这样下次访问 的时候页面文件就发挥作用了。(模板引擎和网上常见的一些缓存类通常有此功能)

时间触发缓存:

检查文件是否存在并且时间戳小于设置的过期时间,如果文件修改的时间戳比当前时间戳减去过期时间戳大,那么就用缓存,否则更新缓存。

内容触发缓存:

当插入数据或更新数据时,强制更新缓存。

静态缓存:

这里所说的静态缓存是指静态化,直接生成HTML或xml等文本文件,有更新的时候重生成一次,适合于不太变化的页面,这就不说了。

以上内容是代码级的解决方案,我直接CP别的框架,也懒得改,内容都差不多,很容易就做到,而且会几种方式一起用,但下面的内容是服务器端的缓存方 案,非代码级的,要有多方的合作才能做到

内存缓存:

Memcached是高性能的,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速度。

这里说下Memcached的例子:

<ol class="dp-c">
<li class="alt"><span><span><?php  </span></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>
</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>

读库的例子:

<ol class="dp-c">
<li class="alt"><span><span><?php  </span></span></span></li>
<li>
<span class="vars">$sql</span><span> = ‘SELECT * FROM users’;  </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>
</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>

PHP的缓冲器:

有eaccelerator, apc, PHPa,xcache,这个这个就不说了吧,搜索一堆一堆的,自己看啦,知道有这玩意就OK

MYSQL缓存:

这也算非代码级的,经典的数据库就是用的这种方式,看下面的运行时间,0.09xxx之类的

我贴段根据蓝色那家伙修改后部分my.ini 吧,2G的MYISAM表可以在0.05S左右,据说他前后改了有快一年

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

基于反向代理的Web缓存:

如Nginx,SQUID,mod_PRoxy(apache2以上又分为mod_proxy和mod_cache)

NGINX的例子

<ol class="dp-c">
<li class="alt"><span><span><nginx.conf>  </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>  </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>  </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> </span></li>
</ol>

而SQUID的例子我就不说明了,这方面网上有写的太多,大家自己搜索一下

DNS轮询:

BIND是一款开放源码的DNS服务器软件,这个要说起来就大了,自己搜索去,大家知道有这个东西就行了。

我知道的有chinacache 等大站就是这样做的,说简单点就是多服务器啦,把同一个页面或文件缓存到不同的服务器上,按南北自动解析到相关的服务器中。

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


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445846.htmlTechArticle普遍缓存技术 数据缓存:这里所说的数据缓存是指数据库查询缓存,每次访问页面的时候,都会先检测相应的缓存数据是否存在,如果不存...
Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn