zoukankan      html  css  js  c++  java
  • 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);
        }
    }
  • 相关阅读:
    支付宝-单笔转账接口
    # Creating Server TCP listening socket *:6379: bind: No such file or directory
    Window 下安装 Redis,配置redis环境变量
    Ajax的跨域(一)
    web人脸识别(二)
    web人脸识别(一)
    给GridView添加列头复选框
    计算两个时间相差多少年月日的sql算法
    MUI下拉加载安卓手机无效的解决方法
    博主回来了!
  • 原文地址:https://www.cnblogs.com/tlxma/p/4023841.html
Copyright © 2011-2022 走看看