8

I am unable to see anything in the cache from the console in either development or production.

For development, I have turned on caching and set it to memory store, in production I use the dalli gem and Memcachier on Heroku.

Every key I try comes back nil.

However, in development, if I put in a binding.pry before somewhere I'm doing a Rails.cache.fetch, I can do a Rails.cache.read there and see something returning, and indeed if so, execution does not go in to the fetch's block.

From the console, if I try Rails.cache.reading the same key that just returned a cached result in the pry breakpoint console, I get nothing. It seems like the console has a separate cache, as if from the console I do Rails.cache.write("whatever", "blah") and Rails.cache.read("whatever"), I get "blah" in return, as expected.

Is there a way to experiment with the cache for a running server from the console?

Hsiu Dai
  • 1,303
  • 2
  • 14
  • 21

1 Answers1

8

Change your cache store: the memory store just stores everything in the process's memory, so every instance of the application will have a separate cache. In particular the console won't see anything that is set from the running web application.

You could use the file store (which stores data in temporary files in tmp) or you could also use dalli store locally - memcached is very easy to run.

Frederick Cheung
  • 83,189
  • 8
  • 152
  • 174
  • Alright, that works for development, I'm still getting nothing in production though, but I will accept your answer because it does answer the question I asked. For fragment caching in a view, where I do `- cache("the_key") do` in HAML, can I access that from the console using Rails.cache.read? Even after switching to file store, it does not seem so, even in development. How can I get this cached stuff? – Hsiu Dai Jul 25 '13 at 16:37
  • 1
    NM, I found my own answer, using `ActionController::Base.new.fragment_cache_key("the_key")` I get the slightly modified cache key (in this case, with `view/` prepended to it), and can read with `Rails.cache.read` and that modified one or just do `ActionController::Base.new.read_fragment("the_key")` with the unmodified key to read. Thanks for your answer! – Hsiu Dai Jul 25 '13 at 16:43