ThinkPHP6采用think\Cache类(实际使用think\facade\Cache类即可)提供缓存功能支持。
内置支持的缓存类型包括file、memcache、wincache、sqlite、redis。
ThinkPHP6全局的缓存配置直接修改配置目录下面的cache.php文件。
新版的缓存支持多通道,你可以事先定义好所有的缓存类型及配置参数,然后在使用的时候可以随时切换。默认使用的是文件缓存类型,你可以添加redis缓存支持,例如:
return [
'default' => 'file',
'stores' => [
// 文件缓存
'file' => [
// 驱动方式
'type' => 'file',
// 设置不同的缓存保存目录
'path' => '../runtime/file/',
],
// redis缓存
'redis' => [
// 驱动方式
'type' => 'redis',
// 服务器地址
'host' => '127.0.0.1',
],
],
];参数介绍:
type缓存类型
expire缓存有效期 (默认为0 表示永久缓存)
prefix缓存前缀(默认为空)
serialize缓存序列化和反序列化方法
如何使用缓存?
<?php
use think\facade\Cache;
//写入缓存
class democache{
public function getCatche(){
if (Cache::get('ApiList'.$dbUrl->id) !=null) {
$cjcate = Cache::get('ApiList'.$dbUrl->id);
}else{
$cjcate=$json['list'];
Cache::set('ApiList'.$dbUrl->id, $cjcate, 604800);
}
}
}
?>上面一个简单的例子,获取缓存,判断是否有缓存,有的话读取缓存,没有的话设置缓存,通过Cache::get()方法获取缓存,通过 Cache::set()设置缓存。
关于简忆
简忆诞生的故事



粤ICP备16092285号
文章评论(0)