lib/cached_resource/caching.rb in cached_resource-5.0.1 vs lib/cached_resource/caching.rb in cached_resource-5.1.0
- old
+ new
@@ -5,11 +5,11 @@
extend ActiveSupport::Concern
included do
class << self
alias_method :find_without_cache, :find
- alias_method :find, :find_with_cache
+ alias_method :find, :find_with_cache
end
end
module ClassMethods
# Find a resource using the cache or resend the request
@@ -82,11 +82,12 @@
arguments == cached_resource.collection_arguments
end
# Read a entry from the cache for the given key.
def cache_read(key)
- object = cached_resource.cache.read(key).try do |cache|
+ object = cached_resource.cache.read(key).try do |json_cache|
+ cache = json_to_object(JSON.parse(json_cache))
if cache.is_a? Enumerable
restored = cache.map { |record| full_dup(record) }
next restored unless respond_to?(:collection_parser)
collection_parser.new(restored)
else
@@ -97,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, :race_condition_ttl => cached_resource.race_condition_ttl, :expires_in => cached_resource.generate_ttl)
+ 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.logger.info("#{CachedResource::Configuration::LOGGER_PREFIX} WRITE #{key}")
result
end
# Clear the cache.
@@ -119,9 +120,17 @@
# Make a full duplicate of an ActiveResource record.
# Currently just dups the record then copies the persisted state.
def full_dup(record)
record.dup.tap do |o|
o.instance_variable_set(:@persisted, record.persisted?)
+ end
+ end
+
+ def json_to_object(json)
+ if json.is_a? Array
+ json.map { |attrs| self.new(attrs) }
+ else
+ self.new(json)
end
end
end
end