码迷,mamicode.com
首页 > 其他好文 > 详细

TP5中用redis缓存

时间:2019-07-14 21:29:25      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:prefix   strong   本地   这一   com   color   tp5   window   复合   

在config.php配置文件下找到缓存设置,将原来的文件缓存修改为redis缓存,也可以改为多种类型的缓存:

    // +----------------------------------------------------------------------
    // | 缓存设置
    // +----------------------------------------------------------------------


/*    ‘cache‘                  => [
        // 驱动方式
        ‘type‘   => ‘File‘,
        // 缓存保存目录
        ‘path‘   => CACHE_PATH,
        // 缓存前缀
        ‘prefix‘ => ‘‘,
        // 缓存有效期 0表示永久缓存
        ‘expire‘ => 0,
    ],*/

    ‘cache‘ => [
        // 使用复合缓存类型
        ‘type‘ => ‘complex‘,
        // 默认使用的缓存
        ‘default‘ => [
            // 驱动方式
            ‘type‘ => ‘File‘,
            // 缓存保存目录
            ‘path‘ => CACHE_PATH,
        ],
        // 文件缓存
        ‘file‘ => [
            // 驱动方式
            ‘type‘ => ‘file‘,
            // 设置不同的缓存保存目录
            ‘path‘ => RUNTIME_PATH . ‘file/‘,
        ],
        // redis缓存
        ‘redis‘ => [
            // 驱动方式
            ‘type‘ => ‘redis‘,
            // 服务器地址
            ‘host‘ => ‘127.0.0.1‘, // 本地环境先开启redis服务端 redis-service.exe
            ‘port‘ => ‘6379‘,
        ],
    ],

这样就可以使用redis来缓存数据了。用法如下:

\think\Cache::store(‘redis‘)->handler()->hMSet(‘test‘, array(‘k1‘=>123));

其实就是因为  \think\Cache::store(‘redis‘)->handler()  这一步返回是redis实例化对象,所以通过这个对象可以操作其他redis数据结构方法

    /**
     * 返回句柄对象,可执行其它高级方法
     *
     * @access public
     * @return object
     */
    public function handler()
    {
        return $this->handler;
    }

 

注意: 不过在window下测试redis, 还要开启它服务端才行 redis-service.exe

 

TP5中用redis缓存

标签:prefix   strong   本地   这一   com   color   tp5   window   复合   

原文地址:https://www.cnblogs.com/pyspang/p/11185759.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!