lib/cached_resource/caching.rb in cached_resource-5.1.0 vs lib/cached_resource/caching.rb in cached_resource-5.1.1
- old
+ new
@@ -83,11 +83,11 @@
end
# Read a entry from the cache for the given key.
def cache_read(key)
object = cached_resource.cache.read(key).try do |json_cache|
- cache = json_to_object(JSON.parse(json_cache))
+ cache = json_to_object(JSON.parse(json_cache, :symbolize_names => true))
if cache.is_a? Enumerable
restored = cache.map { |record| full_dup(record) }
next restored unless respond_to?(:collection_parser)
collection_parser.new(restored)
else
@@ -98,11 +98,11 @@
object
end
# Write an entry to the cache for the given key and value.
def cache_write(key, object)
- result = cached_resource.cache.write(key, object.to_json, :race_condition_ttl => cached_resource.race_condition_ttl, :expires_in => cached_resource.generate_ttl)
+ result = cached_resource.cache.write(key, object_to_json(object), :race_condition_ttl => cached_resource.race_condition_ttl, :expires_in => cached_resource.generate_ttl)
result && cached_resource.logger.info("#{CachedResource::Configuration::LOGGER_PREFIX} WRITE #{key}")
result
end
# Clear the cache.
@@ -125,14 +125,22 @@
end
end
def json_to_object(json)
if json.is_a? Array
- json.map { |attrs| self.new(attrs) }
+ json.map { |attrs|
+ self.new(attrs[:object], attrs[:persistence]) }
else
- self.new(json)
+ self.new(json[:object], json[:persistence])
end
end
+ def object_to_json(object)
+ if object.is_a? Enumerable
+ object.map { |o| { :object => o, :persistence => o.persisted? } }.to_json
+ else
+ { :object => object, :persistence => object.persisted? }.to_json
+ end
+ end
end
end
end