0

我有一个 Ruby on Rails 应用程序,其中使用了片段缓存并使用 memcached 来存储数据。我还有一个清扫器,它在对模型进行更改时使缓存过期。

index.html.erb

<% cache 'recent_albums' do %>
contents to be cached
<%end %>


class AlbumsSweeper < ActionController::Caching::Sweeper
  observe Album

  def after_save(album)
    expire_cache(album)
  end

  def after_destroy(album)
    expire_cache(album)
  end

  def expire_cache(album)
    expire_fragment 'recent_albums'
  end

end

我有一个要求,在用户点击相册页面之前,过期的片段需要用新数据刷新。有人可以帮助如何实现片段缓存数据刷新吗?

4

1 回答 1

0

缓存的整个概念是“做某事并记住它以备下次使用”。

如果我理解你的问题是正确的;您正在尝试做其他事情,即“始终为运行某些事情做好准备” ---因此您可能应该使用其他事情。

Memcached 支持存储 key:value 数据对 - 因此,如果您希望某些内容始终准备好并“预烘焙”,您可以跳过使用缓存,而只需将 key:values 存储到 Memcached。然后在 after_save 和 after_destroy 等上更新它。

除此之外 - 片段缓存将在访问时自动更新,而不是在缓存中。

于 2015-05-04T09:21:41.890 回答