ThinkPHP使用memcache緩存服務器


(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);
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM