Gem Cache Digests 为何签名没变化?

suupic · 2012年11月12日 · 最后由 suupic 回复于 2012年11月12日 · 2521 次阅读

有这样一段 view 代码:

<% cache ['timelines', 'show', 'timeline-info', @timeline] do %>
  <div class="span4 timeline-info">
      <ul class="delimited">
        <li>
          <span class="menu-category">分类</span>
          <% @timeline.catalogs.each do |catalog| %>
            <%= link_to catalog.name, catalog_path(catalog) %>
          <% end %>
        </li>
      </ul>
  </div>
<% end %>

将其中的“分类”2 字修改、保存、刷新页面,发现 cache 没刷新

对比日志,可以看到 cache key 确实做了签名,但签名没有变化,why?

修改前:

Event Load (0.1ms)  SELECT `events`.* FROM `events` WHERE `events`.`timeline_id` IN (3)
Cache read: views/timelines/show/timeline-info/timelines/3-20121112053010/e6bfaea5957ab05d7fd41135f2cd4713
Read fragment views/timelines/show/timeline-info/timelines/3-20121112053010/e6bfaea5957ab05d7fd41135f2cd4713 (0.3ms)
  Rendered timelines/show/_timeline_info.html.erb (0.8ms)

修改后:

 Timeline Load (0.1ms)  SELECT `timelines`.* FROM `timelines` WHERE `timelines`.`published` = 1 AND `timelines`.`id` = 3 LIMIT 1
  Event Load (0.1ms)  SELECT `events`.* FROM `events` WHERE `events`.`timeline_id` IN (3)
Cache read: views/timelines/show/timeline-info/timelines/3-20121112053010/e6bfaea5957ab05d7fd41135f2cd4713
Read fragment views/timelines/show/timeline-info/timelines/3-20121112053010/e6bfaea5957ab05d7fd41135f2cd4713 (0.4ms)
  Rendered timelines/show/_timeline_info.html.erb (0.9ms)

development.rb

config.action_controller.perform_caching = true
config.cache_store = :dalli_store
gem 'dalli'
gem 'cache_digests'
  1. catalogs 修改的时候没有改动过 timeline 所以 Timeline 对象的 updated_at
  2. 就算改了 updated_at,Cache Digests 也不会变动(它只会在 View 文件修改过后才会改变),儿改变的是 20121112053010 这段数字。

#1 楼 @huacnlee catalogs 没有改动,其实我就是想测试 view 的修改,只是把“分类”改成“分类 1”这样 莫非一定要重启 server 吗?每次重启后是最新的 view

log 里有

Cache digest for timelines/show/timeline_info.html: 61822f4e84f4560472d45560d5654922

这样的记录,但只在重启后第一次访问时出现

#2 楼 @suupic 哦,我以为你修改的是数据

可能只在启动时候计算?

从设计考虑来说,应该只能在重新读取 view 文件的时候才会改变 digest

#5 楼 @Rei #6 楼 @huacnlee

看起来确实是需要重启的,在 Railscasts 的评论里发现一条信息: I don't understand the purpose of this gem at all.

Templates don't (or, at least, shouldn't) change in production, so there are no changes to detect.

Templates do change in development, however this gem requires extra code (explicit partial and collection arguments to render) and developer behavior changes (frequent server restarts).

I've wondered for years why there isn't better (any?) support for testing caching. Wouldn't it be better to have that than to jump through these hoops to test caching in development mode? http://railscasts.com/episodes/387-cache-digests?view=comments

需要 登录 后方可回复, 如果你还没有账号请 注册新账号