Ruby on Rails:清除缓存页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2462889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ruby on Rails: Clear a cached page
提问by craig
I have a RoR application (ruby v1.8.7; rails v2.3.5) that is caching a page in the development environment. This wouldn't be so much of an issue, but the cached page's aelements are incorrect.
我有一个 RoR 应用程序(ruby v1.8.7;rails v2.3.5),它在开发环境中缓存页面。这不是什么大问题,但缓存页面的a元素不正确。
I haven't made any changes to the development.rb file and I haven't knowingly added any caching commands to the controllers.
我没有对 development.rb 文件进行任何更改,也没有故意向控制器添加任何缓存命令。
I've tried clearing the browser's (Firefox 3.5 on OSX) cookie and page caches for this site (localhost). I've also restarted Mongrel. Nothing seems to help.
我已尝试清除此站点 (localhost) 的浏览器(OSX 上的 Firefox 3.5)cookie 和页面缓存。我也重新启动了 Mongrel。似乎没有任何帮助。
What am I missing?
我错过了什么?
回答by Apie
This line in development.rb ensures that caching is not happening.
development.rb 中的这一行确保不会发生缓存。
config.action_controller.perform_caching = false
You can clear the Rails cache with
您可以使用以下命令清除 Rails 缓存
Rails.cache.clear
That said - I am not convinced this is a caching issue. Are you making changes to the page and not seeing them reflected? You aren't perhaps looking at the live version of that page? I have done that once (blush).
也就是说 - 我不相信这是一个缓存问题。您是否对页面进行了更改而没有看到它们的反映?您可能没有查看该页面的实时版本?我做过一次(脸红)。
Update:
更新:
You can call that command from in the console. Are you sure you are running the application in development?
您可以从控制台调用该命令。您确定正在开发中运行该应用程序吗?
The only alternative is that the page that you are trying to render isn't the page that is being rendered.
唯一的选择是您尝试呈现的页面不是正在呈现的页面。
If you watch the server output you should be able to see the render command when the page is rendered similar to this:
如果您查看服务器输出,您应该能够在页面呈现类似于以下内容时看到渲染命令:
Rendered shared_partials/_latest_featured_video (31.9ms)
Rendered shared_partials/_s_invite_friends (2.9ms)
Rendered layouts/_sidebar (2002.1ms)
Rendered layouts/_footer (2.8ms)
Rendered layouts/_busy_indicator (0.6ms)
回答by Karen
rake tmp:cache:clearmight be what you're looking for.
rake tmp:cache:clear可能就是你要找的。
回答by Dan
I was able to resolve this problem by cleaning my assets cache:
我能够通过清理我的资产缓存来解决这个问题:
$ rake assets:clean
回答by David Harbage
Check for a static version of your page in /public and delete it if it's there. When Rails 3.x caches pages, it leaves a static version in your public folder and loads that when users hit your site. This will remain even after you clear your cache.
检查 /public 中页面的静态版本,如果存在,则将其删除。当 Rails 3.x 缓存页面时,它会在您的公共文件夹中保留一个静态版本,并在用户访问您的站点时加载它。即使在您清除缓存后,这仍将保留。
回答by jeffdill2
If you're doing fragment caching, you can manually break the cache by updating your cache key, like so:
如果您正在执行片段缓存,您可以通过更新缓存键来手动中断缓存,如下所示:
Version #1
版本#1
<% cache ['cool_name_for_cache_key', 'v1'] do %>
Version #2
版本#2
<% cache ['cool_name_for_cache_key', 'v2'] do %>
Or you can have the cache automatically reset based on the state of a non-static object, such as an ActiveRecord object, like so:
或者,您可以根据非静态对象(例如 ActiveRecord 对象)的状态自动重置缓存,如下所示:
<% cache @user_object do %>
With this ^ method, any time the user object is updated, the cache will automatically be reset.
使用这个 ^ 方法,任何时候更新用户对象,缓存都会自动重置。
回答by Николай Агеев
More esoteric ways:
更深奥的方式:
Rails.cache.delete_matched("*")
For Redis:
对于Redis:
Redis.new.keys.each{ |key| Rails.cache.delete(key) }

