[PHP] 使用Redis作为TP6的缓存驱动

102 阅读1分钟
  • windows相关配置 下载PHP-Redis扩展 (windows中) URL

将两个文件放到扩展目录: php_redis.dll, redis-cli.pdb

D:\phpstudy_pro\Extensions\php\php7.3.4nts\ext

修改php.ini文件,尾部加入,然后重启nginx, 然后php -m | findstr redis查看是否

extension=redis
  • 1.config/cache配置文件
<?php

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

return [
    'default'    =>    'redis',
    'stores'    =>    [
        // 文件缓存
        'file'   =>  [
            // 驱动方式
            'type'   => 'file',
            // 设置不同的缓存保存目录
            'path'   => '../runtime/file/',
        ],  
        // redis缓存
        'redis'   =>  [
            // 驱动方式
            'type'   => 'redis',
            // 服务器地址
            'host'       => '127.0.0.1',
            'password'   => '123456'
        ],  
    ],
];
  • 2.在列表中使用
     public function index()
     {
        // Cache::store('redis')->set('demo', time(), 30);
        // exit;
        $cacheKey  = Request::controller() .'.'. Request::action();
        $cachedData = Cache::store('redis')->get($cacheKey);
        if (! empty($cachedData)) {
            $this->success('用了缓存', $cachedData);
        }

         $this->request->filter(['strip_tags', 'trim']);
         if ($this->request->param('select')) {
             $this->select();
         }
 
         list($where, $alias, $limit, $order) = $this->queryBuilder();
         $res = $this->model
             ->field($this->indexField)
             ->withJoin($this->withJoinTable, $this->withJoinType)
             ->alias($alias)
             ->where($where)
             ->order($order)
             ->paginate(100);

        $data = [
            'list'   => $res->items(),
            'total'  => $res->total(),
        ]; 
        Cache::store('redis')->set($cacheKey, $data, 300); 

        $this->success('没用缓存', $data);
     }

目前性能没有任何提升, 下一步测试在Linux中的效果