diff --git a/lib/cached_resource/caching.rb b/lib/cached_resource/caching.rb index 6c96a4b..8190f6f 100644 --- a/lib/cached_resource/caching.rb +++ b/lib/cached_resource/caching.rb @@ -81,7 +81,9 @@ def is_collection?(*arguments) # The key is processed to make sure it is valid. def cache_read(key) key = cache_key(Array(key)) unless key.is_a? String - object = cached_resource.cache.read(key).try(:dup) + object = cached_resource.cache.read(key).try do |cache| + cache.dup.tap { |o| o.instance_variable_set(:@persisted, cache.persisted?) if cache.respond_to?(:persisted?)} + end object && cached_resource.logger.info("#{CachedResource::Configuration::LOGGER_PREFIX} READ #{key}") object end @@ -102,4 +104,4 @@ def cache_key(*arguments) end end -end \ No newline at end of file +end diff --git a/spec/cached_resource/caching_spec.rb b/spec/cached_resource/caching_spec.rb index fa3b984..ff312f7 100644 --- a/spec/cached_resource/caching_spec.rb +++ b/spec/cached_resource/caching_spec.rb @@ -40,6 +40,12 @@ class Thing < ActiveResource::Base Thing.cached_resource.cache.read("thing/1").should == result end + it "should cache a response with the same persistence" do + result1 = Thing.find(1) + result2 = Thing.find(1) + result1.persisted?.should == result2.persisted? + end + it "should read a response when the request is made again" do # make a request Thing.find(1) @@ -266,4 +272,4 @@ class Thing < ActiveResource::Base new_result.name.should_not == old_result.name end end -end \ No newline at end of file +end