Are the two commands equivalent? If not, what's the difference?
1 Answer
The rake task only clears out files that are stored on the filesystem in "#{Rails.root}/tmp/cache"
. Here's the code for that task.
namespace :cache do
# desc "Clears all files and directories in tmp/cache"
task :clear do
FileUtils.rm_rf(Dir['tmp/cache/[^.]*'])
end
end
Rails.cache.clear
will do different things depending on your apps setting for config.cache_store
. http://guides.rubyonrails.org/caching_with_rails.html#cache-stores
If you are using config.cache_store = :file_store
then Rails.cache.clear
will be functionally identical to rake tmp:cache:clear
. However, if you're using some other cache_store
, like :memory_store
or :mem_cache_store
, then only Rails.cache.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.
-
2When we changed the cache store to :mem_cache_store, I noticed that Rails.cache.clear started clearing all user sessions as well, logging out all users. Is that the intended behavior? Is there any way to clear the cache without touching the sessions? (We are upgrading from Rails 3 to Rails 4)– sandre89Oct 2, 2016 at 15:48