在config.php配置文件下找到缓存设置,将原来的文件缓存修改为redis缓存,也可以改为多种类型的缓存:
// +----------------------------------------------------------------------
// | 缓存设置
// +----------------------------------------------------------------------
/* 'cache' => [
// 驱动方式
'type' => 'File',
// 缓存保存目录
'path' => CACHE_PATH,
// 缓存前缀
'prefix' => '',
// 缓存有效期 0表示永久缓存
'expire' => 0,
],*/
'cache' => [
// 使用复合缓存类型
'type' => 'complex',
// 默认使用的缓存
'default' => [
// 驱动方式
'type' => 'File',
// 缓存保存目录
'path' => CACHE_PATH,
],
// 文件缓存
'file' => [
// 驱动方式
'type' => 'file',
// 设置不同的缓存保存目录
'path' => RUNTIME_PATH . 'file/',
],
// redis缓存
'redis' => [
// 驱动方式
'type' => 'redis',
// 服务器地址
'host' => '127.0.0.1', // 本地环境先开启redis服务端 redis-service.exe
'port' => '6379',
],
],
这样就可以使用redis来缓存数据了。用法如下:
\think\Cache::store('redis')->handler()->hMSet('test', array('k1'=>123));
其实就是因为 \think\Cache::store('redis')->handler() 这一步返回是redis实例化对象,所以通过这个对象可以操作其他redis数据结构方法
/**
* 返回句柄对象,可执行其它高级方法
*
* @access public
* @return object
*/
public function handler()
{
return $this->handler;
}
注意: 不过在window下测试redis, 还要开启它服务端才行 redis-service.exe
你的坚持 ------ 终将美好 ~