【问题标题】:Kohana, Editing Cache doesn't Reflect in View?Kohana,编辑缓存不会反映在视图中?
【发布时间】:2010-11-19 06:55:21
【问题描述】:

我正在为我的网站设置缓存,但在测试缓存是否正常工作时遇到了困难。我的控制器中有以下内容:

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
);

【问题讨论】:

  • 只是一个小提示,我认为 Kohana 建议使用html::specialchars() 而不是原生PHP htmlspecialchars()(虽然我不记得为什么)

标签: php optimization caching kohana


【解决方案1】:

您需要编辑原始数据存储中的数据并将缓存视为黑盒。

如果您使用数据库,请获得一个好的数据库管理器,以便您可以轻松地编辑值。我用DBVisualizer...别问我为什么。

在使用文件缓存驱动时,我会删除application/cache下的所有内容,以便清除缓存并测试缓存代码。

我唯一一次编辑缓存文件是,如果我真的在编写一个缓存系统来替换文件或 memcached 驱动程序。

【讨论】:

    【解决方案2】:

    序列化的 PHP 对象对值施加约束。看下面一行:

    s:5:"title";s:45:"Some Article Title";
    

    s:5 表示以下值将包含五个字符 "T-i-t-l-e"。一共五个。下一个值s:45 应该有 45 个字符。您将文本从原来的内容更改为 "Some Article Title" 以便在此处发布,但原始文本共有 45 个字符。

    添加更多字符或减去字符将破坏值的字符串长度与与之相关的 int-val 之间的关系。如果s:3,你的字符串应该是三个字符长。

    与其对值进行加/减,只需更改几个字符即可。将"Title" 更改为"Ninja",然后刷新页面。

    【讨论】:

    • 在 JSON 中不会有问题。
    • 这在 PHP 中也不是问题,只要你明白发生了什么。
    猜你喜欢
    • 2016-05-15
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-11
    • 1970-01-01
    • 2012-10-12
    • 1970-01-01
    相关资源
    最近更新 更多