Rails: control file store cache size - ruby-on-rails

The documentation for the file-based cache in rails says:
Note that the cache will grow until the disk is full unless you
periodically clear out old entries.
Unfortunately it doesnt give any information about how to clear old entries periodically. Does setting an appropriate value for :expires_in do the job or is there some other sort of black magic behind clearing the cache?
Also, the documenation gives an option to limit the memory-based cache in size:
config.cache_store = :memory_store, { size: 64.megabytes }
Does this also work for the file based cache? And even more importantly, what happens when the cache growths below that size limit? Does it remove old cached values or will it throw some kind of exception?
thanks in advance,
danijoo

Experimenting with FileStore cache I found that :expires_in options works, but :size one doesn't.
If you want to specify options then you need to also specify the path, try with the following example:
config.cache_store = :file_store, Rails.root.join('tmp', 'cache'), { expires_in: 1.minute }
Put the code in config/application.rb and remember to activate the cache in config/environments/development.rb and restart the app.
P.S. I use 1 minute to easily do a quick test.

Yes the limit applies in case of file-based cache too. And yes a value for :expires_in will do the job.
When this limit is reached, no further stuff will be cached. No exception is thrown.

Related

Rails 4 cache: How to disable default caching for particular controller action or view?

We have Rails 4.2 application running on production with the following configurations (cache related). And we have not implemented any caching technique so far (default setup).
config.action_controller.perform_caching = true
# Use a different cache store in production.
# config.cache_store = :mem_cache_store
I am having an issue with particular view which is not showing updated data accordingly.
Is there a way that I can disable fragment caching for particular view not in entire application?
TLDR: while caching can cause problems, it's worth verifying that you are actually using it before trying to fix it!
The simplest way to disable caching for a single view is also the most obvious (but potentially quite easy to miss):
Remove the cache model do block from inside your view.
Rails does not do any caching for you by default, so this will take care of it entirely. If you don't have a call to cache with a block inside your view, you are not using caching.
E.g:
Cached:
# app/views/something_important/show.html.haml
= cache something_important do
= render_something_expensive
Not cached:
# app/views/something_important/show.html.haml
= render_something_expensive

Rails redis cache storage size

I have rails app storing values to redis cache through a cron job overnight. But some values stored is showing as nil when I have checked in the morning.
What is the storage size/limit of redis cache in rails?
How can I change this value through configuration and runtime?
From the Rails Documentation
You can set up your application's default cache store by setting the config.cache_store configuration option. Other parameters can be passed as arguments to the cache store's constructor:
config.cache_store = :memory_store, { size: 64.megabytes }
Alternatively, you can call ActionController::Base.cache_store outside of a configuration block.
You can access the cache by calling Rails.cache.

Rails fragment caching not working locally

I've added the following at the top of my index.html.haml:
- cache do
content
And as far as I can see, the content is not cached. My server output shows that when I reload the page, it still fetches all the info from the database again.
I haven't tried on live as I don't want to push anything before it's 100% working. What am I doing wrong? Am I not understanding how it's supposed to work? I've set the config.action_controller.perform_caching = true.
cache_store configures which cache store to use for Rails caching so you need to specify that
You need to set cache store in general config.
config.cache_store = xyz,abc # PUT THIS
Options that you can set:
:memory_store, :file_store, :mem_cache_store

Rails memcache store default auto expiration time

I have been struggling for a while to find out if there is some default expiration time set by Rails, in case we don't provide any while storing a key-value pair to memcache?
e.g. Rails.cache.write('some-key', 'some-value')
Would rails set some expiration time by default if we haven't specified?
If you're using the default, built-in MemCacheStore class provided by Rails, then no. It won't assume an expiry time when you create new cache entries. You can read the applicable code to verify that. It checks to see if you've passed an expires_in option to the #write method like
Rails.cache.write("key", "content", expires_in: 2.hours)
and if you haven't, simply passes 0 to memcache indicating no expiry time. Hope this helps!
If you are using the newer (and I think better) Dalli memcached gem, you can configure it at the adapter-level using a line like the following:
config.cache_store = :dalli_store, 'cache-1.example.com', 'cache-2.example.com',
{ :namespace => NAME_OF_RAILS_APP, :expires_in => 1.day}
See the README for a detailed explanation of the :expires_in option. Overall, I think Dalli is worth checking out for more than just this feature, its also faster and supports some newer authentication features, etc.

How to set Rails Cache log level separately?

I use a pretty old rails version which is 2.3.2 because of legacy project.
I set global log_level to :debug in our rails app. But since we also use Rails.cache the log file are full of annoying lines such as
Cache read: ...
Cache miss: ...
I want to just suppress these but not affect other 'more useful' info such as SQL logging.
How to do that?
Well, after initializing your cache store (in the example below, I use memory store) in your specific environment.rb file, you can redirect cache_store's log to a separate file and also tweak the logger level:
config.cache_store = ActiveSupport::Cache::MemoryStore.new(:expires_in => 5.minutes)
config.cache_store.logger = Logger.new("#{Rails.root}/log/#{ENV['RAILS_ENV']}_cache.log")
config.cache_store.logger.level = Logger::INFO
In addition to that, the cache store has a method called silence! that will turn off the logger :-|
config.cache_store.silence!

Resources