Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is Rails.cache purged between tests?

We cache id/path mapping using Rails.cache in a Rails 3.2 app. On some machines it works OK, but on the others values are wrong. The cause is hard to track so I have some questions about the Rails.cache itself. Is it purged between tests? Is it possible that values cached in development mode is used in test mode? If it's not purged, how could I do it before running specs?

My cache store is configuration is:

#in: config/environments/development.rb config.cache_store = :memory_store, {:size => 64.megabytes}  #in: config/environments/production.rb # config.cache_store = :mem_cache_store 
like image 453
mrzasa Avatar asked Nov 09 '12 13:11

mrzasa


People also ask

Where is Rails cache stored?

Memory store Since version 5.0, Rails automatically sets up the :memory_store in the development configuration when generating a new application. When using the memory store, cached data is kept in memory in the Ruby web server's process.

How does Rails query cache work?

Query caching is a Rails feature that caches the result set returned by each query. If Rails encounters the same query again for that request, it will use the cached result set as opposed to running the query against the database again.

What does Rails cache clear do?

clear will clear your app cache. In that case rake tmp:cache:clear will just try to remove files from "#{Rails. root}/tmp/cache" but probably won't actually do anything since nothing is probably being cached on the filesystem.

What is cache sweeper in Rails?

Cache sweeping is a mechanism which allows you to get around having a ton of expire_{page,action,fragment} calls in your code. It does this by moving all the work required to expire cached content into na ActionController::Caching::Sweeper class.


1 Answers

A more efficient (and easier) method is to set the test environment's cache to use NullStore:

# config/environments/test.rb: config.cache_store = :null_store 

The NullStore ensures that nothing will ever be cached.

For instance in the code below, it will always fall through to the block and return the current time:

Rails.cache.fetch('time') { Time.now } 

Also see the Rails Caching guide: http://guides.rubyonrails.org/caching_with_rails.html#activesupport-cache-nullstore

like image 130
jaustin Avatar answered Sep 28 '22 13:09

jaustin