注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

~~

~~

 
 
 

日志

 
 

php 缓存  

2012-02-18 17:31:39|  分类: php |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

   生成缓存主要是采用正则替换的方式.

   1、普遍缓存技术

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

如:

       Discuz的搜索功能,把结果ID缓存到一个表中,下次搜索相同关键字时先对缓存表进行搜寻.

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

   2、页面缓存

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

   3、时间触发缓存

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

   4、内容触发缓存

       在向插入数据或更新数据时,强制更新缓存

   5、静态缓存 

        静态缓存也就是我们平时所说的静态化, 直接生成html或者是xml等文本文件; 当有更新的文件是更新一遍重新生成静态文件,这种使用于不经常变化的页面。

    6、内存缓存

         memcache(分布式内存对象缓存系统),主要用于在动态应用中减少数据库的负载,提升访问速度.

         实例:

          <?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, 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);
           ?>

          链接数据库实例:

          <?php
                    $sql = 'SELECT * FROM users';
                    $key = md5($sql);   //memcached 对象标识符
                    if ( !($datas = $mc->get($key)) ) {
                             //  在 memcached 中未获取到缓存数据,则使用数据库查询获取记录集。
                             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;
                                         //  将数据库中获取到的结果集数据保存到 memcached 中,以供下次访问时使用。
                                         $mc->add($key, $datas);
                    } else {
                              echo "n".str_pad('Read datas from memcached.', 60, '_')."n";
                    }
                    var_dump($datas);
              ?>

     7、php缓冲器

           eaccelerator, apc, phpa,xcache这些都是可以百度一下

     8、MYSQL缓存

          修改mysql配置文件mysql.ini ;修改方法如下:

                [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

       9、基于反向代理的Web缓存

           如Nginx,SQUID,mod_proxy(apache2以上又分为mod_proxy和mod_cache)
           nginx实例:

         <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;
   }
  #hp.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   500 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;

            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>

 10、DNS轮询

       bind是开放源码的DNS服务器软件。chinacache等大站就是这样做的,简单点说就是多服务器,把同一个页面或文件缓存到不同的服务器上,自动解析到相关的服务器中

         php缓存技术越来越重要了,学习啦!

  评论这张
 
阅读(282)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018