【问题标题】:What is the difference between Rails.cache.clear and rake tmp:cache:clear?Rails.cache.clear 和 rake tmp:cache:clear 有什么区别?
【发布时间】:2013-09-26 01:43:55
【问题描述】:

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

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2 rake


    【解决方案1】:

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

    【讨论】:

    • 当我们将缓存存储更改为 :mem_cache_store 时,我注意到 Rails.cache.clear 也开始清除所有用户会话,注销所有用户。这是预期的行为吗?有没有办法在不接触会话的情况下清除缓存? (我们正在从 Rails 3 升级到 Rails 4)
    猜你喜欢
    • 2014-11-07
    • 2015-05-25
    • 2012-04-30
    • 2011-04-27
    • 2015-04-26
    • 1970-01-01
    • 2010-10-02
    • 2011-12-12
    • 2010-09-16
    相关资源
    最近更新 更多