Permalink
Browse files

Add a cache for the digests

  • Loading branch information...
1 parent 0a42cfb commit e31a5a39297b7cbea854a207b7ebf596d835c57d @dhh dhh committed Aug 14, 2012
Showing with 7 additions and 2 deletions.
  1. +6 −2 lib/cache_digests/template_digestor.rb
  2. +1 −0 test/template_digestor_test.rb
@@ -1,4 +1,5 @@
require 'active_support/core_ext/array/access'
+require 'active_support/core_ext/class/attribute_accessors'
module CacheDigests
class TemplateDigestor
@@ -11,15 +12,18 @@ class TemplateDigestor
# render('comments/comments')
RENDER_DEPENDENCY = /
render\s? # render, followed by an optional space
- (partial:)?\s? # naming the partial, used with collection -- 1st capture
\(? # start a optional parenthesis for the render call
+ (partial:)?\s? # naming the partial, used with collection -- 1st capture
(\"|\'){1} # need starting quote of some kind to signify string-based template -- 2nd capture
([^'"]+) # the template name itself -- 3rd capture
(\"|\'){1} # need closing quote of some kind to signify string-based template -- 4th capture
/x
+ # Class-level cache for the digests. To clear the cache, just do TemplateDigestor.cache.clear
+ cattr_accessor(:cache) { Hash.new }
+
def self.digest(name, format, finder, options = {})
- new(name, format, finder, options).digest
+ cache["#{name}.#{format}"] ||= new(name, format, finder, options).digest
end
attr_reader :name, :format, :finder, :options
@@ -55,6 +55,7 @@ def test_third_level_dependency
private
def assert_digest_difference(template_name)
previous_digest = digest(template_name)
+ CacheDigests::TemplateDigestor.cache.clear
yield
assert previous_digest != digest(template_name)
end

0 comments on commit e31a5a3

Please sign in to comment.