我正在为我的网站设置缓存,但在测试缓存是否正常工作时遇到了困难。我的控制器中有以下内容:
public function read($id, $slug = null)
{
$this->cache = Cache::instance();
$story = $this->cache->get("story".$id);
if (!$story) {
$story_model = new Story_Model;
$story = $story_model->get_story($id);
if (!$story) throw new Kohana_404_Exception();
$this->cache->set("story".$id, $story);
}
$this->template->content = new View('story');
$this->template->title = htmlspecialchars($story->title);
$this->template->content->story = $story;
}
这很好用,我什至可以验证是否找到了缓存,并且在设置缓存后没有输入 if() 检查。我的困惑是,为什么当我编辑缓存文件时,更改没有反映在视图中?例如,我的缓存如下所示:
O:8:"stdClass":11:{
s:2:"id";s:3:"636";
s:5:"title";s:45:"Some Article Title";
s:4:"link";s:50:"http://www.somesite.com";
s:8:"category";s:2:"12";
s:4:"user";s:1:"5";
s:4:"slug";s:45:"some-article-title";
s:7:"pubdate";s:19:"2009-08-05 03:57:50";
s:6:"sticky";s:1:"0";
s:7:"summary";N;
s:13:"categorytitle";s:13:"International";
s:8:"username";s:7:"usernameHere";
}
如果我将title
值更改为“Some Article Title Part 2”并刷新我的视图,我仍然会看到旧的 Title 名称,并且我对缓存文件所做的更改消失了。
我做错了吗?如何测试是否正在访问我的缓存文件而不是我的数据库?我的配置文件内容如下:
$config['default'] = array
(
'driver' => 'file',
'params' => APPPATH.'cache',
'lifetime' => 1800,
'requests' => 1000
);