程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 基於php緩存的詳解

基於php緩存的詳解

編輯:關於PHP編程

nginx緩存
nginx有兩種緩存機制:fastcgi_cache和proxy_cache
下面我們來說說這兩種緩存機制的區別吧
proxy_cache作用是緩存後端服務器的內容,可能是任何內容,包括靜態的和動態的
fastcgi_cache作用是緩存fastcgi生成的內容,很多情況是php生成的動態內容
proxy_cache緩存減少了nginx與後端通信的次數,節省了傳輸時間和後端帶寬
fastcgi_cache緩存減少了nginx與php的通信次數,更減輕了php和數據庫的壓力。

proxy_cache緩存設置
#注:proxy_temp_path和proxy_cache_path指定的路徑必須在同一分區
proxy_temp_path   /data0/proxy_temp_dir;
#設置Web緩存區名稱為cache_one,內存緩存空間大小為200MB,1天沒有被訪問的內容自動清除,硬盤緩存空間大小為30GB。
proxy_cache_path  /data0/proxy_cache_dir  levels=1:2   keys_zone=cache_one:200m inactive=1d max_size=30g;

server
  {
    listen       80;
    server_name  www.yourdomain.com 192.168.8.42;
    index index.html index.htm;
    root  /data0/htdocs/www; 

    location /
    {
         #如果後端的服務器返回502、504、執行超時等錯誤,自動將請求轉發到upstream負載均衡池中的另一台服務器,實現故障轉移。
         proxy_next_upstream http_502 http_504 error timeout invalid_header;
         proxy_cache cache_one;
         #對不同的HTTP狀態碼設置不同的緩存時間
         proxy_cache_valid  200 304 12h;
         #以域名、URI、參數組合成Web緩存的Key值,Nginx根據Key值哈希,存儲緩存內容到二級緩存目錄內
         proxy_cache_key $host$uri$is_args$args;
         proxy_set_header Host  $host;
         proxy_set_header X-Forwarded-For  $remote_addr;
         proxy_pass http://backend_server;
         expires      1d;
    }

    #用於清除緩存,假設一個URL為http://192.168.8.42/test.txt,通過訪問http://192.168.8.42/purge/test.txt就可以清除該URL的緩存。
    location ~ /purge(/.*)
    {
     #設置只允許指定的IP或IP段才可以清除URL緩存。
     allow            127.0.0.1;
     allow            192.168.0.0/16;
     deny            all;
     proxy_cache_purge    cache_one   $host$1$is_args$args;
    }   

    #擴展名以.php、.jsp、.cgi結尾的動態應用程序不緩存。
    location ~ .*\.(php|jsp|cgi)?$
    {
         proxy_set_header Host  $host;
         proxy_set_header X-Forwarded-For  $remote_addr;
         proxy_pass http://backend_server;
    }

    access_log  off;
  }
}

fastcgi_cache緩存設置
#定義緩存存放的文件夾
fastcgi_cache_path   /tt/cache  levels=1:2 keys_zone=NAME:2880m inactive=2d max_size=10G;
#定義緩存不同的url請求
fastcgi_cache_key "$scheme$request_method$host$uri$arg_filename$arg_x$arg_y";

server {
        listen       8080;
        server_name  www.example .com;
        location / {
            root   /www;
            index  index.html index.htm index.php;
        }

        location ~ (|.php)$ {
            root           /www;
            fastcgi_pass   127.0.0.1:9000;

            fastcgi_cache   NAME;
            fastcgi_cache_valid 200 48h;
            fastcgi_cache_min_uses  1;
            fastcgi_cache_use_stale error  timeout invalid_header http_500;

            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi.conf;
            #設置緩存的過程中發現無法獲取cookie,經查需要定義這句話
            fastcgi_pass_header Set-Cookie;
        }

        log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
              '$status $body_bytes_sent "$http_referer" '
              '"$http_user_agent" $http_x_forwarded_for';
access_log  /httplogs/access.log  access;
}
總的來說  nginx的proxy_cache和fastcgi_cache的緩存配置差不多。
--------------------------------------------------------------------------------
memcache緩存
在討論memcache緩存之前,我們先了解下mysql的內存緩存吧
mysql的內存緩存可以在my.cnf中指定大小:內存表和臨時表不同,臨時表也是存放內存中,臨時表最大的內存需要通過tmp_table_size=128M設定。當數據查過臨時表的最大值設定時,自動轉為磁盤表,此時因需要進行IO操作,性能會大大下降,而內存表不會,內存滿了後,會提示數據滿錯誤。
例:
create table test
(
    id int unsigned not null auto_increment primary key
    state char(10),
    type char(20),
    date char(30)
)engine=memory default charset=utf8

內存表的特性:
1.內存表的表定義存放在磁盤上,擴展名為.frm,所以重啟不會丟失
2.內存表的數據是存放在內存中,重啟會丟失數據
3.內存表使用一個固定的長度格式
4.內存表不支持blob或text列,比如varchar與text字段就不會被支持
5.內存表支持auto_increment列和對可包含null值的列的索引
6.內存表不支持事物
7.內存表是表鎖,當修改頻繁時,性能可能會下降

下面我們來看看memcache,相對而言mysql的內存表限制較多。
memcache的用途
1.提高系統的並發能力
2.減輕數據庫的負擔
注:memcache linux系統32位只支持4G內存,同時memcache最長保存時間為30天。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved