How can I list the keys in the in-memory cache store on Ruby on Rails?

rails memcached
ruby cache
rails fragment caching
rails.cache.fetch not working
rails cache key
rails api caching
rails multiple cache stores
rails cache exist

I am using Rails 3.

How can I list the keys in the in-memory cache store on Ruby on Rails?

ActiveSupport::Cache::MemoryStore doesn't provide a way to access the store's keys directly (and neither does its parent class ActiveSupport::Cache::Store).

Internally MemoryStore keeps everything in a Hash called @data, however, so you could monkey-patch or subclass it to get the keys, e.g.:

class InspectableMemoryStore < ActiveSupport::Cache::MemoryStore
  def keys

ActionController::Base.cache_store =

Rails.cache.keys # => [ "foo", ... ]

This comes with the usual caveat, however: MemoryStore's internal implementation may change at any time and @data may disappear or be changed to something that doesn't respond_to? :keys. A smarter implementation might be to override the write and delete methods (since, as part of the public API, they're unlikely to change unexpectedly) to keep your own list of keys, e.g.:

class InspectableMemoryStore < ActiveSupport::Cache::MemoryStore
  def write *args

    @inspectable_keys[ args[0] ] = true

  def delete *args

    @inspectable_keys.delete args[0]

  def keys

This is a very naive implementation, and of course keeping the keys in an additional structure takes up some memory, but you get the gist.

Caching with Rails: An Overview — Ruby on Rails Guides, There are multiple cache store implementations, each having its own additional features. When an object is specified as a key and has a cache_key method defined, this method will be decrement(name, amount = 1, options = nil) Link. A cache store implementation which stores everything into memory in the same process. If you're running multiple Ruby on Rails server processes (which is the case if you're using Phusion Passenger or puma clustered mode), then this means that Rails server process instances won't be able to share cache data with each other and this may not be the most appropriate cache in that scenario.


ActiveSupport::Cache::Store, The cache key includes the model name, the id and finally the updated_at timestamp. This cache store keeps entries in memory in the same Ruby process. Cache fragments will also be expired when the view fragment changes (e.g., the HTML in the view changes). The string of characters at the end of the key is a template tree digest. It is a hash digest computed based on the contents of the view fragment you are caching.

If you don't need to access the keys dynamically, an easier approach is locating the directory where the cache is stored. A file is created for each entry.

In my case this was at "APP_ROOT/tmp/cache", but you can easily locate it by going to rails console and typing

1.8.7 :030 >   Rails.cache.clear
 => ["path_to_rails_app/tmp/cache/6D5"]

Caching with Rails: An overview — Ruby on Rails Guides, A cache store implementation which stores everything into memory in the same process. Preemptively iterates through all stored keys and removes the ones which have expired. Source: decrement(name, amount = 1, options = nil) Link. This cache store keeps entries in memory in the same Ruby process. The cache store has a bounded size specified by the :size options to the initializer (default is 32Mb). When the cache exceeds the allotted size, a cleanup will occur and the least recently used entries will be removed.

Fetch all keys:

Read the specific key"cache_key")

Delete one key from keys:


Flush out the keys


ActiveSupport::Cache::MemoryStore, Page, action, and fragment caching; Sweepers; Alternative cache stores By default, the page cache directory is set to Rails.public_path (which is from the query it is stored in the query cache (in memory) and the second time it's pulled from memory. The keys used in a cache can be any object that responds to either� Rails.cache.write("a", 1) # "OK""a") # 1 Rails stores values that look like this: "\u0004\bo: ActiveSupport::Cache::Entry\t:\v@valuei\u0006:\r@version0:\u0010@created_atf\u00171555005228.7954454:\u0010@expires_in0" This means that you can store any serializable Ruby object, like results of an ActiveRecord query. Redis F.A.Q “Can I list all the keys in my Redis server?”

Caching with Rails: An overview, Ruby on Rails latest stable (v5.2.3) - 0 notes - Class: ActiveSupport::Cache::Store to the cache under the given cache key, and that return value will be returned. Setting :version verifies the cache stored under name is of the same version. Rails.cache.fetch('my_cache_key', expires_in: 1.hour) do SomeApi.fetch_posts end. The next time you run this code with the same cache key Rails will instead fetch the result of the block from the cache. This can be used anywhere in the Rails application and it is a good way to cache external APIs and other slow data fetching stuff. Further reading

fetch (ActiveSupport::Cache::Store), The default cache stores provided with Rails include: A cache store implementation which stores everything into memory in the same process. that generates a key using the class name, id and updated_at� Memcached is a quick in-memory protest reserving framework that can make Rails run much quicker with not very many changes. Memcached is an in-memory key-value store for little pieces of discretionary information (strings, objects) from consequences of database calls, API calls, or page rendering. Run the command below to install memcached On

Ruby on Rails Guides: Caching with Rails: An overview, Ruby; Rails; Ruby Puma Webserver; Rack::Cache; Python; Django; PHP The benefit of an in-memory key-value store diminishes at 1MB and higher. the PHP Memcached client documentation for a list of API calls you can� Rails 2.1 and above provide ActiveSupport::Cache::Store which can be used to cache strings. Some cache store implementations, like MemoryStore, are able to cache arbitrary Ruby objects, but don’t count on every cache store to be able to do that. The default cache stores provided with Rails include:

  • I just wanted to check the name of the keys. That will do the trick, big thanks!
  • What is the file where I set ActionController::Base.cache_store = ?
  • @Paul In your environment files, e.g. config/environment/development.rb. If it's inside the Rails.application.configure do...end block then it would just be config.cache_store =
  • This isn't really monkey patching, is it?
  • @CppNoob I wrote, "you could monkey-patch or subclass it." The code above is an example of the latter.
  • Doesn't work as of Rails 4.2 with a file store cache
  • Works with Rails 4.2.2 using redis-rails
  • Works with Rails 4.2 with a memorystore cache
  • Rails.cache.instance_variable_get("@data") works for me in Rails 4 with Dalli :)
  • Works with Rails 5.2 with a memorystore cache
  • But your solution worked only when file cache.if I use memcached as backend cache, this doesn't work.