Module: LaserLemon::CacheFlow::InstanceMethods
- Defined in:
- lib/cache_flow.rb
Instance Method Summary collapse
- #to_json_with_cache_flow(options = {}) ⇒ Object
- #to_xml_with_cache_flow(options = {}, &block) ⇒ Object
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( = {}) if cache_json?() key = json_cache_key() Rails.cache.fetch(key) do value = to_json_without_cache_flow() value.duplicable? ? value.dup : value end else to_json_without_cache_flow() 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( = {}, &block) if cache_xml?(, &block) serializer = ActiveRecord::XmlSerializer.new(self, ) builder = serializer.builder key = xml_cache_key(serializer.) 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(, &block) end end |