PHP緩存之內存緩存(Memcache)

jopen 10年前發布 | 40K 次閱讀 PHP緩存 PHP開發

  1. PHP內存緩存應該也是數據緩存的一種,是將數據庫查詢出來的數據緩存在內存中,共享內存塊中。這種方式可以通過擴展軟件來實現,現在主流的主要是Memcache。  
  2. </ol>

    1、PHP開啟Memcache模塊(windows)

             PHP開啟memcache, 只需要將PHP的配置文件php.ini中

    extension=php_memcache.dll  

    前的#號去掉即可。在php.ini中還有幾個memcache的配置參數:

    memcache.allow_failover  
    類型:Boolean
    說明:在錯誤時是否透明的故障轉移到其他服務器上處理

    memcache.max_failover_attempts 
    類型:integer
    說明:定義服務器的數量類設置和獲取數據,只聯合 memcache.allow_failover 一同使用。

    memcache.chunk_size
    類型:integer
    說明:數據將會被分成指定大小(chunk_size)的塊來傳輸,這個值(chunk_size)越小,寫操作的請求就越多,如果發現其他的無法解釋的減速,請試著將這個值增大到32768.

    memcache.hash_strategy 
    類型:string
    說明:控制在映射 key 到服務器時使用哪種策略。設置這個值一致能使hash 算法始終如一的使用于服務器接受添加或者刪除池中變量時將不會被重新映射。設置這個值以標準的結果在舊的策略被使用時。

    memcache.hash_function 
    類型:string
    說明:控制哪種 hsah 函數被應用于 key映射 到服務器過程中,默認值“crc32”使用 CRC32 算法,而“fnv”則表示使用 FNV-1a 算法。

    session.save_handler 
    類型:string
    說明:通過設置這個值為memcache來確定使用 memcache 用于通信對話的處理(session handler)。

    session.save_path 
    類型:string
    說明:定義用于通話存儲的各服務器鏈接的分隔符號,例如:“tcp://host1:11211, tcp://host2:11211”。

     

           案例:

        memcache.allow_failover = On                 
        memcache.max_failover_attempts = 20  
        memcache.chunk_size = 8192  
        memcache.default_port = 11211  
        ;memcache.hash_strategy = "standard"  
        ;memcache.hash_function = "crc32"  
        ;session.save_handler = "files"  
        ;session.save_path = ""  

    2、memcache和memcached之間的區別

             對于內存緩存,比較常用的有兩種memcache和memcached擴展。而memcached和memcache的守護進程memcached同名,比較容易引起混淆,甚至提到memcached,有些人第一想到的是后臺的守護進程,這里還是有必要分析一下兩者之間的區別。
             memcache是完全在PHP框架內開發的,memecached是使用libmemcached的。從手冊上看,memcached 會比 memcache 多幾個方法,使用方式上都差不多。
    具體可以參考PHP官網:

    Memcached:http://cn2.php.net/manual/zh/book.memcached.php
    Memcache:http://cn2.php.net/manual/zh/book.memcache.php

    3、PHP操作memcache的簡單案例

    class MemcacheModel {  
    
        private $memcache;  
    
        //連接memcache  
        public function __construct($host = '127.0.0.1', $port = 11211) {  
            $this->memcache = new Memcache();  
            $this->memcache->connect($host, $port);  
            return $this->memcache;  
        }  
    
        /** 
         * add  添加一個新key,但是如果 key已經在服務端存在,此操作會失敗。 
         * @param string $Key    KEY名稱 
         * @param string $value  值----可以是數組,對象,單值 
         * @param int $timelift 生存時間   add生存時間默認為0表示數據用不過期 
         */  
        public function add($key, $value, $timeLife) {  
            if ($time > 0) {  
                $retMes = $this->memcache->add($key, $value, MEMCACHE_COMPRESSED, $timeLife);  
            } else {  
                $retMes = $this->memcache->add($key, $value);  
            }  
            return $retMes;  
        }  
    
        /** 
         * set設置一致key  修改鍵名的值 
         * @param string $key 鍵名 
         * @param string $value 鍵值 
         * @param int $timeLife 生命周期 
         */  
        public function set($key, $value, $timeLife) {  
            if ($timeLife > 0) {  
                $retMes = $this->memcache->set($key, $value, MEMCACHE_COMPRESSED, $timeLife);  
            } else {  
                $retMes = $this->memcache->set($key, $value);  
            }  
            return $retMes;  
        }  
    
        /** 
         * 獲取key 
         * @param string  $key 鍵名 
         */  
        public function get($key) {  
            $retMes = $this->memcache->get($key);  
            return $retMes;  
        }  
    
        /** 
         * 刪除單個key 
         * @param string $key 鍵名 
         */  
        public function deleteKey($key) {  
            $retMes = $this->memcache->delete($key);  
            return $retMes;  
        }  
    
        /** 
         * 刪除所有key 
         */  
        public function deleteAll() {  
            return $this->memcache->flush();  
        }  
    
        /** 
         * 釋放 
         */  
        public function __destruct() {  
            $this->memcache->close();  
        }  
    
    }  
 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!