tp5缓存有效期配置无效问题(cache方法,缓存类型File)

浏览:11859 发布日期:2017/05/17 分类:ThinkPHP5专区 关键字: thinkphp5 tp5 缓存时间无效
配置文件config.php下面是这么写的:

但实际上如果你在设置缓存的时候直接这么写:cache('test',['a'=>123]);最终你会发现,这个缓存有效期是永久的,看源码后发现(/thinkphp/helper.php,375行)cache函数下面有这样的代码:// 缓存数据
if (is_array($options)) {
      $expire = isset($options['expire']) ? $options['expire'] : null; //修复查询缓存无法设置过期时间
} else {
        $expire = is_numeric($options) ? $options : null; //默认快捷缓存设置过期时间
}
就是说,到了这一步如果$options为null或者空,$expire是设置为null的;
再看看缓存驱动文件(/thinkphp/cache/driver/File.php)的set函数:

$this->options['expire']的值默认是0的,所以整个运行下来,居然没有发现载入配置的地方,可能是我没有看完,但我还是直接写了自己的自定义方法,手动加载配置文件function safeCache($name, $value = '', $options = null, $tag = null){
    if (is_null($options)){
        $options = config('cache');    //引入配置参数(修正由于框架里面没有对有效时间设置)
    }
    return cache($name, $value, $options, $tag);
}


最佳答案
评论( 相关
后面还有条评论,点击查看>>