ThinkPHP使用memcache快取伺服器
阿新 • • 發佈:2018-11-10
(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使用測試:
![複製程式碼](https://common.cnblogs.com/images/copycode.gif)
$test = S('test'); if(!$test){ $test = '快取資訊'; S('test',$test,300); echo $test.'-來自資料庫'; }else{ echo $test.'-來自memcached'; }
![複製程式碼](https://common.cnblogs.com/images/copycode.gif)
附:S函式程式碼
![複製程式碼](https://common.cnblogs.com/images/copycode.gif)
/** * 快取管理 * @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); } }
![複製程式碼](https://common.cnblogs.com/images/copycode.gif)