ThinkPHP使用memcache快取服務器

來源:互聯網
上載者:User

標籤:style   blog   color   io   os   使用   ar   檔案   sp   

(1)Thinkphp的預設緩衝方式是以File方式,在/Runtime/Temp 下產生了好多快取檔案。

伺服器裝了memcached後想給更改成memecache方式

在Conf/config.php 中添加

‘DATA_CACHE_TYPE‘ => ‘Memcache‘,

‘MEMCACHE_HOST‘   => ‘tcp://127.0.0.1:11211‘, 

‘DATA_CACHE_TIME‘ => ‘3600‘,

(2)thinkphp官方下載擴充ThinkPHP_Extend_3.1.2/Extend/Driver/Cache/CacheMemcache.class.php 儲存到      ThinkPHP/Lib/Driver/Cache/CacheMemcache.class.php

(3)測試: S(‘test‘,‘memcache‘);$test = S(‘test‘); echo $test;//輸出memcache 測試成功。

(4)memcached使用測試:

$test = S(‘test‘);if(!$test){     $test = ‘緩衝資訊‘;     S(‘test‘,$test,300);     echo $test.‘-來自資料庫‘;}else{     echo $test.‘-來自memcached‘;}

 

附:S函數代碼

/** * 緩衝管理 * @param mixed $name 緩衝名稱,如果為數組表示進行緩衝設定 * @param mixed $value 緩衝值 * @param mixed $options 緩衝參數 * @return mixed */function S($name,$value=‘‘,$options=null) {    static $cache   =   ‘‘;    if(is_array($options) && empty($cache)){        // 快取作業的同時初始化        $type       =   isset($options[‘type‘])?$options[‘type‘]:‘‘;        $cache      =   Cache::getInstance($type,$options);    }elseif(is_array($name)) { // 緩衝初始化        $type       =   isset($name[‘type‘])?$name[‘type‘]:‘‘;        $cache      =   Cache::getInstance($type,$name);        return $cache;    }elseif(empty($cache)) { // 自動初始化        $cache      =   Cache::getInstance();    }    if(‘‘=== $value){ // 擷取緩衝        return $cache->get($name);    }elseif(is_null($value)) { // 刪除緩衝        return $cache->rm($name);    }else { // 快取資料        if(is_array($options)) {            $expire     =   isset($options[‘expire‘])?$options[‘expire‘]:NULL;        }else{            $expire     =   is_numeric($options)?$options:NULL;        }        return $cache->set($name, $value, $expire);    }}

 

ThinkPHP使用memcache快取服務器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.