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.
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
How can I fix this problem. Please teach me how to solve this....
First, I had tried to use page cache on my web app. But, it doesn't run well. That was not so good. So, I updated to get rid of page cache. After that, my app's view page that had page cache could not be updated.
below is a my config/environment/development.rb
KaguShop::Application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false
# Do not eager load code on boot.
config.eager_load = false
# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false
# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
# Raise an error on page load if there are pending migrations
config.active_record.migration_error = :page_load
# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large
# number of complex assets.
config.assets.debug = true
#caching for objects
#config.cache_store = :mem_cache_store
end
And, after I edited my cache setting, I tried this command.
※and of course, I reopen the browser.
rails console
Rails.cache.clear
try ctrl+shift+delete on browser to clear cache.
To perform caching config.action_controller.perform_caching should be set to true
before update you must do something like expire_page :action => action_name
Ex:
def update
expire_page :action => profile
...........
end
I gathered information about caching at 2 places other day. Check them here and here
OMG....
This is the answer of this question.
I didnt know this rails's structure...
After removing caches_page cached sites are still not updated
I'm using Rails 3.2.13 and the Rails Asset Pipeline. I want to use the Asset Pipeline so I can use SASS and CoffeeScript and ERB for my assets and have the Pipeline automatically compile them, so I cannot turn off the pipeline in development. I am not precompiling assets in development ever and there is not even a public/assets/ directory.
However, when I make changes to an included file, such as to a _partial.html.erb file that is included (rendered) in a layout.html.erb file, without changing the file doing the including itself (in this example layout.html.erb), Sprockets doesn't detect the change and invalidate the cache, so I keep getting the same stale file. When I'm doing this in active development, I want to disable any caching of assets so I can get the changes on every request but I cannot figure out how to do this. I have set all of the following in my development.rb:
config.action_controller.perform_caching = false
config.action_dispatch.rack_cache = nil
config.middleware.delete Rack::Cache
config.assets.debug = true
config.assets.compress = false
config.cache_classes = false
Still, even with this, files show up under tmp/cache/assets/ and tmp/cache/sass/ and changes are not available on future requests. Right now I have to manually delete those directories every time I want to see a change.
Unfortunately, the entire contents of the How Caching Works section of the RoR Guide for the Asset Pipeline is:
Sprockets uses the default Rails cache store to cache assets in
development and production.
TODO: Add more about changing the default store.
So, how can I get Sprockets to compile assets on demand but not cache the results?
Here's the magic incantation:
config.assets.cache_store = :null_store # Disables the Asset cache
config.sass.cache = false # Disable the SASS compiler cache
The asset pipeline has it's own instance of a cache and setting config.assets.cache = false does nothing, so you have to set its cache to be the null_store to disable it.
Even then, the SASS compiler has it's own cache, and if you need to disable it, you have to disable it separately.
I created the following gist (https://gist.github.com/metaskills/9028312) that does just this and found it is the only way that works for me.
# In config/initializers/sprockets.rb
require 'sprockets'
require 'sprockets/server'
Sprockets::Server.class_eval do
private
def headers_with_rails_env_check(*args)
headers_without_rails_env_check(*args).tap do |headers|
if Rails.env.development?
headers["Cache-Control"] = "no-cache"
headers.delete "Last-Modified"
headers.delete "ETag"
end
end
end
alias_method_chain :headers, :rails_env_check
end
The accepted answer is not doing it correctly and it degrades the performance in development by disabling cache totally.
Answering your original question, you want changes to referenced files to invalidate the asset cache even if not included directly.
The solution is by simply declaring such dependency such that sprockets knows that the cache should be invalidated:
# layout.html.erb
<% depend_on Rails.root.join('app').join('views').join('_partial.html.erb') %>
# replace the above with the correct path, could also be relative but didn't try
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!