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.
Related
I am having the mentioned error on a rails project, after I added the following line of code in the config/environments/development.rb
config.cache.store = :dalli_store
I am working on a personal project using Rails 4.2, installed on OS X 10.7.5. Also memcached 1.4.5 is installed an running in my computer. I don't understand why this is happening. I want to use cache.store on my project but, I don't know how to solve this problem. Any help will be well received !
Please change:
config.cache.store = :dalli_store
to
config.cache_store = :dalli_store
Guide says Cache Stores
Rails provides different stores for the cached data created by action and fragment caches.
Configuration
You can set up your application's default cache store by calling config.cache_store = :dalli_store
Alternatively, you can call ActionController::Base.cache_store outside
of a configuration block.
You can access the cache by calling Rails.cache
The cache store has a bounded size specified by the :size options to the initializer (default is 32Mb).
If you need to increase that then like below
config.cache_store = :dalli_store, { size: 64.megabytes }
And finally you written config.cache.store = :dalli_store that is wrong because Rails guide says config.cache_store
If you need to know more about Rails 4.2 Caching then please visit Rails official document about Cach here
Hope it helps
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
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.
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
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
Add:
before(:all) do
Rails.cache.clear
end
to have the cache cleared before each spec file is run.
Add:
before(:each) do
Rails.cache.clear
end
to have the cache cleared before each spec.
You can put this inside spec/spec_helper.rb within the RSpec.configure block to have it applied globally (recommended over scattering it per spec file or case).
RSpec by default does not clear that cache automatically.
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!