Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add skip_digest option key to allow bypassing cache digestion #16

Merged
merged 1 commit into from

3 participants

@latortuga

In addition to the already present bypass method of passing an explicitly version numbered cache key (e.g. ['v1','cache-key']), this adds an option to the cache in order to give control in bypassing the digest append step on a case-by-case basis.

Discussion in #12.

@latortuga latortuga Add skip_digest option key to allow bypassing cache digestion
In addition to the already present bypass method of passing an explicitly
version numbered cache key (e.g. ['v1','cache-key']), this adds an option
to the cache in order to give control in bypassing the digest append step
on a case-by-case basis.
fc45854
@dhh dhh merged commit 4e679af into from
@dhh
Owner

Great work. Can you get this fix into rails/master as well?

@latortuga
@phuongnd08

Fantastic. Thanks.

@latortuga

@dhh @phuongnd08 I have a unit test and an implementation for skip_cache: true in rails master however there is currently no support for cache_digests' explicitly_versioned_cache_key? check. Would you like me to add that implementation as well?

@phuongnd08

@latortuga: I don't understand cache_digests that well to know if such method is necessary or not :) Let's hear from @dhh then.

@sgerrand sgerrand referenced this pull request from a commit in sgerrand/rails
@latortuga latortuga Add explicit opt-out for fragment cache digesting
This add support for sending an explicit opt-out of the "Russian-doll"
cache digest feature on a case-by-case basis. This is useful when cache-
expiration needs to be performed manually and it would be otherwise
difficult to know the exact name of a digested cache key.

More information: rails/cache_digests#16
7fb8c67
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 24, 2012
  1. @latortuga

    Add skip_digest option key to allow bypassing cache digestion

    latortuga authored
    In addition to the already present bypass method of passing an explicitly
    version numbered cache key (e.g. ['v1','cache-key']), this adds an option
    to the cache in order to give control in bypassing the digest append step
    on a case-by-case basis.
This page is out of date. Refresh to see the latest.
View
11 lib/cache_digests/fragment_helper.rb
@@ -5,9 +5,16 @@ def fragment_name_with_digest(name)
end
private
- # Automatically include this template's digest -- and its childrens' -- in the cache key.
+ # Automatically include this template's digest -- and its childrens' --
+ # in the cache key. Cache digests can be skipped by either providing an
+ # explicitly versioned key (an Array-based key with the first key
+ # matching "v#" e.g. ['v3','my-key']) or by passing skip_digest: true to
+ # the options hash.
def fragment_for(key, options = nil, &block)
- if !explicitly_versioned_cache_key?(key)
+ skip_digest = explicitly_versioned_cache_key?(key) ||
+ (options && options.delete(:skip_digest))
+
+ if !skip_digest
super fragment_name_with_digest(key), options, &block
else
super
View
39 test/fragment_helper_test.rb
@@ -9,6 +9,25 @@ def initialize
end
end
+class BaseFragmenter
+ attr_accessor :virtual_path, :formats, :lookup_context
+ def initialize
+ @virtual_path = ''
+ @formats = [:html]
+ end
+
+ private
+ # Give a base implementation of fragment_for so super calls from
+ # ChildFragmenter have a method to delegate to.
+ def fragment_for(key,opts=nil,&blk)
+ key
+ end
+end
+
+class ChildFragmenter < BaseFragmenter
+ include CacheDigests::FragmentHelper
+end
+
class FragmentHelperTest < MiniTest::Unit::TestCase
def setup
# would love some mocha here
@@ -46,8 +65,28 @@ def test_appends_the_array_key_with_digest
assert_equal ['key1', 'key2', 'digest'], key_with_digest
end
+ def test_digest_skipped_when_opted_out
+ key = child_fragmenter.send(:fragment_for, 'key1', {skip_digest: true})
+ # 'key1' key derived from super call to BaseFragmenter above
+ assert_equal 'key1', key
+ end
+
+ def test_digest_skipped_when_v_number_keyed
+ key = child_fragmenter.send(:fragment_for, ['v1','key1'])
+ assert_equal ['v1','key1'], key
+ end
+
+ def test_digest_not_skipped_otherwise
+ key_with_digest = child_fragmenter.send(:fragment_for, 'key1', {skip_digest: false})
+ assert_equal ['key1','digest'], key_with_digest
+ end
+
private
def fragmenter
@fragmenter ||= Fragmenter.new
end
+
+ def child_fragmenter
+ @child_fragmenter ||= ChildFragmenter.new
+ end
end
Something went wrong with that request. Please try again.