Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails.cache.fetch caching in development

Using Rails.cache.fetch like below is caching even in my development environment (with caching turned off):

@boat_features = Rails.cache.fetch("boat_features", expires_in: 10.minutes) do
  BoatFeature.all
end

Has anyone run into this before?

like image 900
robotmay Avatar asked Jan 23 '12 12:01

robotmay


People also ask

What does Rails cache fetch do?

fetch method. This method does both reading and writing to the cache. When passed only a single argument, the key is fetched and value from the cache is returned. If a block is passed, that block will be executed in the event of a cache miss.

Which of the following caching techniques is are available in Rails?

This is an introduction to three types of caching techniques: page, action and fragment caching. Rails provides by default fragment caching. In order to use page and action caching, you will need to add actionpack-page_caching and actionpack-action_caching to your Gemfile.

Does Rails cache use Redis?

Rails 5.2 introduced built-in Redis cache store, which allows you to store cache entries in Redis.

Where does Rails store cache?

Rails (as of 2.1) provides different stores for the cached data created by action and fragment caches. Page caches are always stored on disk. Rails 2.1 and above provide ActiveSupport::Cache::Store which can be used to cache strings.


2 Answers

That's normal. That sort of caching isn't turned off in development. In a previous app where this was a problem we used the memory store and then added a middleware that did Rails.cache.clear after every request.

Something like

config.middleware.use ClearCache

in development.rb

and then your ClearCache middleware should look something like

class ClearCache
  def initialize(app)
    @app = app
  end

  def call(env)
    @app.call(env)
  ensure
    Rails.cache.clear
  end
end

In Rails 3.2 there's also ActiveSupport::Cache::NullStore

like image 189
Frederick Cheung Avatar answered Sep 17 '22 19:09

Frederick Cheung


I had the same problem. I worked around a lot then came up with this simple solution. In your development configuration file config/environments/development.rb add these settings

config.perform_caching = false config.cache_store = :null_store

like image 43
Qaisar Nadeem Avatar answered Sep 18 '22 19:09

Qaisar Nadeem