lib/cached_resource/caching.rb in cached_resource-5.1.1 vs lib/cached_resource/caching.rb in cached_resource-5.1.2
- old
+ new
@@ -83,17 +83,23 @@
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, :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
- full_dup(cache)
+
+ # In older version JSON can't deserialize 'null' to nil
+ json = json_cache == 'null' ? nil : JSON.parse(json_cache, :symbolize_names => true)
+
+ unless json.nil?
+ cache = json_to_object(json)
+ if cache.is_a? Enumerable
+ restored = cache.map { |record| full_dup(record) }
+ next restored unless respond_to?(:collection_parser)
+ collection_parser.new(restored)
+ else
+ full_dup(cache)
+ end
end
end
object && cached_resource.logger.info("#{CachedResource::Configuration::LOGGER_PREFIX} READ #{key}")
object
end
@@ -135,9 +141,11 @@
end
def object_to_json(object)
if object.is_a? Enumerable
object.map { |o| { :object => o, :persistence => o.persisted? } }.to_json
+ elsif object.nil?
+ nil.to_json
else
{ :object => object, :persistence => object.persisted? }.to_json
end
end
end