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);
    }
}
原文地址:https://www.cnblogs.com/tlxma/p/4023841.html