86

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?

craig
  • 25,664
  • 27
  • 119
  • 205

6 Answers6

133

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)
Apie
  • 6,371
  • 7
  • 27
  • 25
  • That line is present in the development.rb file. Where do I add that command? Yes. I made changes to the page that should have been displayed if the page was 'live'--This is how I discovered the caching issue. – craig Mar 17 '10 at 14:36
  • It appears to have been an idiotic mistake on my part. *blush* Thanks for your time and assistance--I did learn a lot. – craig Mar 17 '10 at 15:09
  • No problem. Would love to know what the issue was - these things are always pretty obscure - most of all when you are making a idiotic mistake - I figure i'm bound to make the same one sometime soon :) – Apie Mar 17 '10 at 15:32
  • 1
    I've nested routes for two related models. On one page, I use the edit_parent_child_path in the link_to. Unfortunately, I omitted the parent's reference; it was edit_parent_child_path(child), it needed to be edit_parent_child_path(@parent,child). – craig Mar 17 '10 at 16:41
  • "You aren't perhaps looking at the live version of that page? I have done that once (blush)." LOL this just happened to me haha – Robert Vunabandi May 17 '20 at 15:57
116

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

Loren
  • 3,476
  • 3
  • 23
  • 15
  • 1
    If the environment is something other than development then you will need to add (for example staging) - `RAILS_ENV=staging bundle exec rake tmp:cache:clear` otherwise i guess it won't work – poorva Sep 24 '15 at 06:45
  • this will just remove files from tmp/cache directory. it will not clear the cache store data. https://stackoverflow.com/questions/19017983/what-is-the-difference-between-rails-cache-clear-and-rake-tmpcacheclear – Oshan Wisumperuma Jun 23 '20 at 08:12
26

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

$ rake assets:clean
Dan
  • 1,729
  • 18
  • 11
4

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.

David Harbage
  • 687
  • 3
  • 8
  • 25
  • In Rails 4, cache items located at `/tmp/cache/` directory. – Ivan Chau Apr 03 '15 at 17:31
  • This did the trick for me. I kept getting a render page I was using to test some new code. The server console wasn't reporting any new pages I was requesting that would render the same text. I cleared my cache thinking that was the issue, but that didn't help. I found the page in the public folder, deleted it, and now its working properly. – Reimus Klinsman Oct 25 '15 at 08:20
3

More esoteric ways:

Rails.cache.delete_matched("*")

For Redis:

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

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.

jeffdill2
  • 3,968
  • 2
  • 30
  • 47
  • what about want to be automatically reset after changes only for specific fragment cache? – Astm Sep 29 '20 at 10:10
  • @Astm that's where you'd use the last option – have an AR object as part of your cache key. When the `updated_at` is modified, the AR object will no longer match the cache key, which will cause the fragment to be recached. – jeffdill2 Sep 29 '20 at 13:43
  • I found easy way to clear the cache automatically if any changes happen by using the syntax <% cache(['User_Cache', user_object], expires_in: 1.hour) do %> if there's any change in the user_object it will automatically update the cache – Astm Oct 05 '20 at 17:34
  • 1
    @Astm right, that's the last option I mentioned using a an AR object as part of the cache key. – jeffdill2 Oct 05 '20 at 17:49