在Rails 4中禁用缓存摘要 [英] Disable cache digests in Rails 4

查看:55
本文介绍了在Rails 4中禁用缓存摘要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Rails 3应用程序迁移到Rails4.迁移过程相当顺利,但是我遇到的一个大问题是,使缓存失效的旧的Rails 3代码无法正常工作.我收到类似的日志:

I'm in the process of migrating a Rails 3 app to Rails 4. The migration was mostly fairly smooth, but one big problem I'm encountering is that my old Rails 3 code to expire my caches isn't working. I'm getting logs like:

Expire fragment views/localhost:3000/cardsets/36?action_suffix=edityes (0.0ms)
...
Read fragment   views/localhost:3000/cardsets/36?action_suffix=edityes/d8034b6e68ba30b5916a2ebb73b68ffe (0.0ms)

事实证明是因为Rails 4带来了一种新的时髦的缓存,缓存摘要.最后的一串长长的十六进制是Rails想要与此缓存片段关联的某些视图的md5摘要.

This turns out to be because Rails 4 brings a new funky kind of caching, cache digests. That long string of hex on the end is the md5 digest of some view that Rails wants to associate with this cache fragment.

我相信我不需要缓存摘要.我的应用程序很少得到更新,通常我可以在更新时清除缓存,因此,引用我的视图代码的先前部署版本的缓存片段的概念是无关紧要的.

I believe I have no need for cache digests. My application gets updated pretty infrequently, and generally I can clear the cache when it's updated, so the concept of a cache fragment that refers to a previous deployment's version of a piece of my view code is irrelevant.

我看到我可以使用:skip_digest =>修改对 cache 的任何给定调用.true 标志.此博客文章是指修改大量的 cache 调用以添加:skip_digest .但是我相信我想将此标志应用于我的应用程序中对 cache 的每次调用.当然,必须有某种方法可以普遍禁用缓存摘要吗?

I see that I can modify any given call to cache with the :skip_digest => true flag. This blog post refers to modifying a large number of their cache calls to add :skip_digest. But I believe I want to apply this flag to every single call to cache in my application. Surely there must be some way to just disable cache digests universally?

推荐答案

最简单的方法是猴子修补 cache_fragment_name 方法,以使 skip_digest true 默认情况下.为了在需要时使用md5摘要,您只需将 skip_digest 设置为 false

Easiest way to do this is to monkey patch the cache_fragment_name method so that skip_digest is true by default. In order to use the md5 digest when you need it, you'd just need to set skip_digest to false

module ActionView
  module Helpers
    module CacheHelper
      def cache_fragment_name(name = {}, options = nil)
        skip_digest = options && !options[:skip_digest].nil? ? options[:skip_digest] : true

        if skip_digest
          name
        else
          fragment_name_with_digest(name)
        end
      end
    end
  end
end

这篇关于在Rails 4中禁用缓存摘要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆