lib/jbuilder/jbuilder_template.rb in jbuilder-2.5.0 vs lib/jbuilder/jbuilder_template.rb in jbuilder-2.6.0

- old
+ new

@@ -30,11 +30,11 @@ # json.cache! ['v1', @person], expires_in: 10.minutes do # json.extract! @person, :name, :age # end def cache!(key=nil, options={}) if @context.controller.perform_caching - value = ::Rails.cache.fetch(_cache_key(key, options), options) do + value = _cache_fragment_for(key, options) do _scope { yield self } end merge! value else @@ -100,12 +100,38 @@ def _render_partial(options) options[:locals].merge! json: self @context.render options end + def _cache_fragment_for(key, options, &block) + key = _cache_key(key, options) + _read_fragment_cache(key, options) || _write_fragment_cache(key, options, &block) + end + + def _read_fragment_cache(key, options = nil) + @context.controller.instrument_fragment_cache :read_fragment, key do + ::Rails.cache.read(key, options) + end + end + + def _write_fragment_cache(key, options = nil) + @context.controller.instrument_fragment_cache :write_fragment, key do + yield.tap do |value| + ::Rails.cache.write(key, value, options) + end + end + end + def _cache_key(key, options) - key = _fragment_name_with_digest(key, options) - key = url_for(key).split('://', 2).last if ::Hash === key + name_options = options.slice(:skip_digest, :virtual_path) + key = _fragment_name_with_digest(key, name_options) + + if @context.respond_to?(:fragment_cache_key) + key = @context.fragment_cache_key(key) + else + key = url_for(key).split('://', 2).last if ::Hash === key + end + ::ActiveSupport::Cache.expand_cache_key(key, :jbuilder) end def _fragment_name_with_digest(key, options) if @context.respond_to?(:cache_fragment_name)