Ruby-on-rails Rails.cache.clear 和 rake tmp:cache:clear 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19017983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:45:23  来源:igfitidea点击:

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

ruby-on-railsruby-on-rails-3ruby-on-rails-3.2rake

提问by Crashalot

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

这两个命令是等价的吗?如果不是,有什么区别?

回答by Jeremy Green

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.

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

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

Rails.cache.clearwill do different things depending on your apps setting for config.cache_store. http://guides.rubyonrails.org/caching_with_rails.html#cache-stores

Rails.cache.clear将根据您的应用程序设置做不同的事情config.cache_storehttp://guides.rubyonrails.org/caching_with_rails.html#cache-stores

If you are using config.cache_store = :file_storethen Rails.cache.clearwill be functionally identical to rake tmp:cache:clear. However, if you're using some other cache_store, like :memory_storeor :mem_cache_store, then only Rails.cache.clearwill clear your app cache. In that case rake tmp:cache:clearwill 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.

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