TP5.0
config类配置redis缓存
'cache' => [
// 使用复合缓存类型
'type' => 'complex',
// 默认使用的缓存
'default' => [
// 驱动方式
'type' => 'File',
// 缓存保存目录
'path' => CACHE_PATH,
],
'file' => [
// 驱动方式
'type' => 'File',
// 缓存保存目录
'path' => CACHE_PATH,
//缓存前缀
'prefix' => '',
//缓存有效期 0表示永久缓存
'expire' => 0,
],
// redis缓存
'redis' => [
// 驱动方式
'type' => 'redis',
// 服务器地址
'host' => '127.0.0.1',
//库10
'select' => 10,
],
],
创建类导入cache类并在类方法使用redis缓存
//导入Cache类
use think\Cache;
//方法内使用redis缓存
$redis = Cache::store('redis')->handler();
//存入reids缓存
$redis->hset("data",date('Y-m-d', strtotime('last Friday')),json_encode($data));
TP5.1
config文件下cache类配置缓存驱动方法
return [
// 驱动方式
'type' => 'redis',
// 缓存保存目录
'path' => '',
// 缓存前缀
'prefix' => '',
// 缓存有效期 0表示永久缓存
'expire' => 0,
];
创建类导入cache类助手函数并在类方法使用redis缓存
//导入Cache类助手函数
use think\facade\Cache;
//类方法内使用redis缓存
$cache=Cache::init();
$redis=$cache->handler();
//存入hash函数
$redis->hset("ceshi",1,"01");
//取hash函数
var_dump($redis->hget("ceshi",1));