Rails.cache.clear和rake tmp:cache:clear有什么区别?

59

这两个命令是否等价?如果不是,它们有什么区别?

1个回答

86

这个 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

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" 中删除文件,但可能实际上什么也不做,因为文件系统上可能没有缓存任何内容。


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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接