Ruby on Rails: Clear a cached page

Ruby on-RailsCaching

Ruby on-Rails Problem Overview


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 a elements are incorrect.

I haven't made any changes to the development.rb file and I haven't knowingly added any caching commands to the controllers.

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.

What am I missing?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

This line in development.rb ensures that caching is not happening.

config.action_controller.perform_caching             = false

You can clear the Rails cache with

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)

Solution 2 - Ruby on-Rails

rake tmp:cache:clear might be what you're looking for.

Solution 3 - Ruby on-Rails

I was able to resolve this problem by cleaning my assets cache:

$ rake assets:clean

Solution 4 - Ruby on-Rails

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.

Solution 5 - Ruby on-Rails

More esoteric ways:

Rails.cache.delete_matched("*")

For Redis:

Redis.new.keys.each{ |key| Rails.cache.delete(key) }

Solution 6 - Ruby on-Rails

If you're doing fragment caching, you can manually break the cache by updating your cache key, like so:

###Version #1

<% cache ['cool_name_for_cache_key', 'v1'] do %>

###Version #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:

<% cache @user_object do %>

With this ^ method, any time the user object is updated, the cache will automatically be reset.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncraigView Question on Stackoverflow
Solution 1 - Ruby on-RailsApieView Answer on Stackoverflow
Solution 2 - Ruby on-RailsLorenView Answer on Stackoverflow
Solution 3 - Ruby on-RailsDanView Answer on Stackoverflow
Solution 4 - Ruby on-RailsDavid HarbageView Answer on Stackoverflow
Solution 5 - Ruby on-RailsНиколай АгеевView Answer on Stackoverflow
Solution 6 - Ruby on-Railsjeffdill2View Answer on Stackoverflow