標籤: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快取服務器