tp5配置引入使用redis

1.首先你的php得是已经安装了redis扩展的

2.在tp里找到config.php配置文件,找到cache,改成下面的样子

    'cache'                  => [
        
              // 选择模式

      'type'   => 'complex',

      // 默认(文件缓存)
      'default'=>[
        // 驱动方式
        'type'   => 'File',
        // 缓存保存目录
        'path'   => CACHE_PATH,
        // 缓存前缀
        'prefix' => '',
        // 缓存有效期 0表示永久缓存
        'expire' => 0,

      ],
        
        //redis缓存设置
        'redis'   =>  [
            // 驱动方式
            'type'   => 'redis',
            // 服务器地址
            'host'   => '127.0.0.1',  //redis服务器ip
            'password' => '',
            'port'   => '6379',
            'password'=> "",
            'timeout' => 3600
        ], 
        
    ],

3.控制器里写上以下代码测试一下

<?php
namespace app\index\controller;

class Index
{
    public $redis;
    
    public function __construct(){
        $this->redis = new \Redis();
        $this->redis->connect('127.0.0.1',6379);
    }    

    
    public function index()
    {
        $redis = $this->redis;
        echo $redis->ping();
    }
  
}

猜你喜欢

转载自www.cnblogs.com/junyi-bk/p/11752717.html
今日推荐