修改文件 config/cache.php
return [
// 缓存配置为复合类型
'type' => 'complex',
'default' => [
'type' => 'file',
// 全局缓存有效期(0为永久有效)
'expire'=> 0,
// 缓存前缀
'prefix'=> '',
// 缓存目录
'path' => '../runtime/cache/',
],
'redis' => [
'type' => 'redis',
// 地址
'host' => '127.0.0.1',
// 全局缓存有效期(0为永久有效)
'expire'=> 0,
// 缓存前缀
'prefix'=> '',
// 端口
'port'=> '6379',
// 密码
'password' => '123456',
],
];
添加内置函数 ,在thinkphp/helper.php最后添加
if (!function_exists('redis')) {
/**
* 获取容器对象实例
* @return Container
*/
function redis($select=0)
{
Cache::store('redis')->select($select);
return Cache::store('redis');
}
}
调用方法
//判断redis缓存
redis(0)->has($name);
//读取redis缓存
redis(0)->get($name);
//写入redis缓存
redis(0)->set($name,$value);
//删除redis缓存
redis(0)->rm($name);
//删除redis缓存
redis(0)->clear($name);
//选择数据库为1,默认不写为0
redis(1)->set($name,$value);