Rails如何使目录过期?

时间:2009-12-10 19:24:49

标签: ruby-on-rails caching will-paginate

如何使Rails中的目录失效?

我有一个博客,其中/posts/列出了所有帖子。控制器帖子,动作索引。很标准的东西。

使用will_paginate以10个为一组对帖子进行分页。

页面缓存如下:

/posts/
/posts/index/2
/posts/index/3
/posts/index/4
...
/posts/index/134
/posts/index/135
... 
etc..

当我需要使此页面过期时,expire_page(posts_path)将无法完成此任务,它只会过期/posts.html

使分页页面过期的最佳方法是什么?由于页面数量不确定,我应该将整个/posts/index/目录过期吗? 如何使目录失效

感谢

3 个答案:

答案 0 :(得分:4)

你正在进行页面缓存,对吗?为什么不删除目录?

system("rm -rf #{RAILS_ROOT}/public/posts")
#Or, in more Rubyish code
FileUtils.rm_rf "#{RAILS_ROOT}/public/posts"

答案 1 :(得分:0)

您可以将Regexp传递给expire_fragment:

expire_fragment(r%{/posts/index/\d+})

但请注意,只有在缓存存储能够遍历所有密钥时才能正常工作。例如,它不适用于memcached

编辑:我看到这是页面缓存,而不是片段缓存。似乎expire_page不接受正则表达式。 HMM。

答案 2 :(得分:0)

当您需要使这些页面过期时,您会希望清扫器运行。看看这个Rails Envy帖子http://railsenvy.com/2007/02/28/rails-caching-tutorial#sweepers

如果您无法使用一组哈希参数删除页面,那么可以在http://railsenvy.com/2007/02/28/rails-caching-tutorial#clearing删除整个缓存文件目录的示例

在该文章中可能会有更多信息可以帮助您,这是对所有Rails缓存的非常好的2部分解释。