最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于php緩存的詳解

 更新時間:2013年05月15日 17:33:35   作者:  
本篇文章是對php中的緩存進行了詳細(xì)的分析介紹,需要的朋友參考下

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

proxy_cache緩存設(shè)置
#注:proxy_temp_path和proxy_cache_path指定的路徑必須在同一分區(qū)
proxy_temp_path   /data0/proxy_temp_dir;
#設(shè)置Web緩存區(qū)名稱為cache_one,內(nèi)存緩存空間大小為200MB,1天沒有被訪問的內(nèi)容自動清除,硬盤緩存空間大小為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 /
    {
         #如果后端的服務(wù)器返回502、504、執(zhí)行超時等錯誤,自動將請求轉(zhuǎn)發(fā)到upstream負(fù)載均衡池中的另一臺服務(wù)器,實現(xiàn)故障轉(zhuǎn)移。
         proxy_next_upstream http_502 http_504 error timeout invalid_header;
         proxy_cache cache_one;
         #對不同的HTTP狀態(tài)碼設(shè)置不同的緩存時間
         proxy_cache_valid  200 304 12h;
         #以域名、URI、參數(shù)組合成Web緩存的Key值,Nginx根據(jù)Key值哈希,存儲緩存內(nèi)容到二級緩存目錄內(nèi)
         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;
    }

    #用于清除緩存,假設(shè)一個URL為http://192.168.8.42/test.txt,通過訪問http://192.168.8.42/purge/test.txt就可以清除該URL的緩存。
    location ~ /purge(/.*)
    {
     #設(shè)置只允許指定的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結(jié)尾的動態(tài)應(yīng)用程序不緩存。
    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緩存設(shè)置
#定義緩存存放的文件夾
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;
            #設(shè)置緩存的過程中發(fā)現(xiàn)無法獲取cookie,經(jīng)查需要定義這句話
            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的內(nèi)存緩存吧
mysql的內(nèi)存緩存可以在my.cnf中指定大?。簝?nèi)存表和臨時表不同,臨時表也是存放內(nèi)存中,臨時表最大的內(nèi)存需要通過tmp_table_size=128M設(shè)定。當(dāng)數(shù)據(jù)查過臨時表的最大值設(shè)定時,自動轉(zhuǎn)為磁盤表,此時因需要進行IO操作,性能會大大下降,而內(nèi)存表不會,內(nèi)存滿了后,會提示數(shù)據(jù)滿錯誤。
例:
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

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

下面我們來看看memcache,相對而言mysql的內(nèi)存表限制較多。
memcache的用途
1.提高系統(tǒng)的并發(fā)能力
2.減輕數(shù)據(jù)庫的負(fù)擔(dān)
注:memcache linux系統(tǒng)32位只支持4G內(nèi)存,同時memcache最長保存時間為30天。

相關(guān)文章

最新評論

徐州市| 温泉县| 潼南县| 上栗县| 光泽县| 积石山| 黄山市| 波密县| 福贡县| 秀山| 梅州市| 阿勒泰市| 潮安县| 隆林| 大理市| 祁连县| 新昌县| 祁门县| 双牌县| 西充县| 郑州市| 安溪县| 句容市| 宁安市| 岳池县| 尤溪县| 甘孜| 琼海市| 富阳市| 白玉县| 鹤山市| 安新县| 巴南区| 大丰市| 廊坊市| 华池县| 恩施市| 诸暨市| 石城县| 库伦旗| 尤溪县|