Skip to content

Commit 65dfb33

Browse files
author
David Heinemeier Hansson
authored
Merge pull request #341 from javan/respect-fragment-cache-keys
Fix fragment_cache_key cache invalidation
2 parents b56455b + f39e8b8 commit 65dfb33

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

lib/jbuilder/jbuilder_template.rb

+27-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def partial!(*args)
3232
# end
3333
def cache!(key=nil, options={})
3434
if @context.controller.perform_caching
35-
value = ::Rails.cache.fetch(_cache_key(key, options), options) do
35+
value = _cache_fragment_for(key, options) do
3636
_scope { yield self }
3737
end
3838

@@ -102,9 +102,34 @@ def _render_partial(options)
102102
@context.render options
103103
end
104104

105+
def _cache_fragment_for(key, options, &block)
106+
key = _cache_key(key, options)
107+
_read_fragment_cache(key, options) || _write_fragment_cache(key, options, &block)
108+
end
109+
110+
def _read_fragment_cache(key, options = nil)
111+
@context.controller.instrument_fragment_cache :read_fragment, key do
112+
::Rails.cache.read(key, options)
113+
end
114+
end
115+
116+
def _write_fragment_cache(key, options = nil)
117+
@context.controller.instrument_fragment_cache :write_fragment, key do
118+
yield.tap do |value|
119+
::Rails.cache.write(key, value, options)
120+
end
121+
end
122+
end
123+
105124
def _cache_key(key, options)
106125
key = _fragment_name_with_digest(key, options)
107-
key = url_for(key).split('://', 2).last if ::Hash === key
126+
127+
if @context.respond_to?(:fragment_cache_key)
128+
key = @context.fragment_cache_key(key)
129+
else
130+
key = url_for(key).split('://', 2).last if ::Hash === key
131+
end
132+
108133
::ActiveSupport::Cache.expand_cache_key(key, :jbuilder)
109134
end
110135

test/jbuilder_template_test.rb

+29
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,35 @@ def assert_collection_rendered(result, context = nil)
332332
JBUILDER
333333
end
334334

335+
test "fragment caching uses fragment_cache_key" do
336+
undef_context_methods :fragment_name_with_digest, :cache_fragment_name
337+
338+
@context.expects(:fragment_cache_key).with("cachekey")
339+
340+
jbuild <<-JBUILDER
341+
json.cache! "cachekey" do
342+
json.name "Cache"
343+
end
344+
JBUILDER
345+
end
346+
347+
test "fragment caching instrumentation" do
348+
undef_context_methods :fragment_name_with_digest, :cache_fragment_name
349+
350+
payloads = {}
351+
ActiveSupport::Notifications.subscribe("read_fragment.action_controller") { |*args| payloads[:read_fragment] = args.last }
352+
ActiveSupport::Notifications.subscribe("write_fragment.action_controller") { |*args| payloads[:write_fragment] = args.last }
353+
354+
jbuild <<-JBUILDER
355+
json.cache! "cachekey" do
356+
json.name "Cache"
357+
end
358+
JBUILDER
359+
360+
assert_equal "jbuilder/cachekey", payloads[:read_fragment][:key]
361+
assert_equal "jbuilder/cachekey", payloads[:write_fragment][:key]
362+
end
363+
335364
test "current cache digest option accepts options" do
336365
undef_context_methods :fragment_name_with_digest
337366

0 commit comments

Comments
 (0)