Module: LaserLemon::CacheFlow::InstanceMethods

Defined in:
lib/cache_flow.rb

Instance Method Summary collapse

Instance Method Details

#to_json_with_cache_flow(options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cache_flow.rb', line 51

def to_json_with_cache_flow(options = {})
  if cache_json?(options)
    key = json_cache_key(options)
    Rails.cache.fetch(key) do
      value = to_json_without_cache_flow(options)
      value.duplicable? ? value.dup : value
    end
  else
    to_json_without_cache_flow(options)
  end
end

#to_xml_with_cache_flow(options = {}, &block) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cache_flow.rb', line 33

def to_xml_with_cache_flow(options = {}, &block)
  if cache_xml?(options, &block)
    serializer = ActiveRecord::XmlSerializer.new(self, options)
    builder = serializer.builder
    key = xml_cache_key(serializer.options)
    fetch = Rails.cache.fetch(key) do
      target = builder.target!
      builder.instance_eval{ @target = '' }
      value = block_given? ? serializer.to_s(&block) : serializer.to_s
      builder.instance_eval{ @target = target }
      value
    end
    builder.instance_eval{ @target << fetch }
  else
    to_xml_without_cache_flow(options, &block)
  end
end