What is the difference between Rails.cache.clear and rake tmp:cache:clear?

若如初见. 提交于 2020-11-30 02:45:08

问题


Are the two commands equivalent? If not, what's the difference?


回答1:


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

https://github.com/rails/rails/blob/ef5d85709d346e55827e88f53430a2cbe1e5fb9e/railties/lib/rails/tasks/tmp.rake#L25-L30

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.



来源:https://stackoverflow.com/questions/19017983/what-is-the-difference-between-rails-cache-clear-and-rake-tmpcacheclear

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!