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
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With