这两个命令是否等价?如果不是,它们有什么区别?
这两个命令是否等价?如果不是,它们有什么区别?
这个 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
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"
中删除文件,但可能实际上什么也不做,因为文件系统上可能没有缓存任何内容。