Skip to content

Commit ddb2985

Browse files
committed
Split cache read/write methods and add instrumentation following ActionView's cache helpers
1 parent fc6fa81 commit ddb2985

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

lib/jbuilder/jbuilder_template.rb

Lines changed: 20 additions & 1 deletion
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,6 +102,25 @@ 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)
107126

0 commit comments

Comments
 (0)