Skip to content

Commit

Permalink
return the resource with the same persistence in cache
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonw committed Apr 9, 2012
1 parent 11e743a commit 7f38765
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lib/cached_resource/caching.rb
Expand Up @@ -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
Expand All @@ -102,4 +104,4 @@ def cache_key(*arguments)

end
end
end
end
8 changes: 7 additions & 1 deletion spec/cached_resource/caching_spec.rb
Expand Up @@ -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)
Expand Down Expand Up @@ -266,4 +272,4 @@ class Thing < ActiveResource::Base
new_result.name.should_not == old_result.name
end
end
end
end

0 comments on commit 7f38765

Please sign in to comment.