TP5中配置Redis

2022-04-08
2530

Tp5

thinkphp5

1、首先在.env配置文件中添加配置



#我们这里连接的是阿里云redis服务器

[redis]
host = 地址
port=  端口
password=用户名:密码     

2、缓存设置


在config.php中配置


// +----------------------------------------------------------------------
    // | 缓存设置
    // +----------------------------------------------------------------------
    'cache'                  => [
        // 使用复合缓存类型
        'type'  =>  'complex',
        // 默认使用的缓存类型
        'default'   =>  [
            // 驱动方式
            'type'   => 'File',
            // 缓存保存目录
            'path'   => CACHE_PATH,
            // 缓存前缀
            'prefix' => '',
            // 缓存有效期 0表示永久缓存
            'expire' => 0,
        ],
        // redis缓存
        'redis'   =>  [
            // 驱动方式
            'type'   => 'redis',
            // 服务器地址
            'host'       => Env::get('redis.host', '127.0.0.1'),
            'port'       => 6379,
            'password'   => Env::get('redis.password', ''),
        ],
    ],

我们这里获取ENV中的配置信息需要引入Env类

use think\Env;




3、使用Redis



Cache::store('redis')->set('a',777);
Cache::store('redis')->set('b',888);
Cache::store('redis')->get('a');  


注意:使用之前需要引入  Cache 类



use think\Cache;