Jer*_*een 77

rake任务只清除存储在文件系统中的文件"#{Rails.root}/tmp/cache".这是该任务的代码.

namespace :cache do
  # desc "Clears all files and directories in tmp/cache"
  task :clear do
    FileUtils.rm_rf(Dir['tmp/cache/[^.]*'])
  end
end
Run Code Online (Sandbox Code Playgroud)

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

Rails.cache.clear将根据您的应用设置执行不同的操作config.cache_store. http://guides.rubyonrails.org/caching_with_rails.html#cache-stores

如果您正在使用config.cache_store = :file_store那么Rails.cache.clear将在功能上相同rake tmp:cache:clear.但是,如果您正在使用其他内容cache_store,例如:memory_store:mem_cache_store,那么只会Rails.cache.clear清除您的应用缓存.在这种情况下,rake tmp:cache:clear只会尝试从中删除文件,"#{Rails.root}/tmp/cache"但可能实际上不会执行任何操作,因为文件系统上可能没有任何缓存.

  • 当我们将缓存存储更改为 :mem_cache_store 时,我注意到 Rails.cache.clear 也开始清除所有用户会话,注销所有用户。这是预期的行为吗?有没有办法在不接触会话的情况下清除缓存?(我们正在从 Rails 3 升级到 Rails 4) (2认同)