Skip to content

Commit

Permalink
BUGFIX: cache did not support non strings
Browse files Browse the repository at this point in the history
also reduced mocking in tests
  • Loading branch information
SamSaffron committed Jan 7, 2014
1 parent e0284df commit 660d87a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
9 changes: 7 additions & 2 deletions lib/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,20 @@ def namespaced_key(key, opts=nil)

def read_entry(key, options)
if data = redis.get(key)
data = Marshal.load(data)
ActiveSupport::Cache::Entry.new data
end
rescue
# corrupt cache, fail silently for now, remove rescue later
end

def write_entry(key, entry, options)
dumped = Marshal.dump(entry.value)

if expiry = options[:expires_in]
redis.setex(key, expiry, entry.value)
redis.setex(key, expiry, dumped)
else
redis.set(key, entry.value)
redis.set(key, dumped)
end

if family = family_key(options[:family], options)
Expand Down
30 changes: 19 additions & 11 deletions spec/components/cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@
Cache.new
end

it "supports fixnum" do
cache.write("num", 1)
cache.read("num").should == 1
end

it "supports hash" do
hash = {a: 1, b: [1,2,3]}
cache.write("hash", hash)
cache.read("hash").should == hash
end

it "can be cleared" do
cache.write("hello0", "world")
cache.write("hello1", "world")
Expand Down Expand Up @@ -35,21 +46,20 @@
cache.fetch("key").should be_nil
end

it "can store with expiry correctly" do
#TODO yuck on this mock
it "calls setex in redis" do
cache.delete("key")

key = cache.namespaced_key("key")
$redis.expects(:get).with(key).returns nil
$redis.expects(:setex).with(key, 60 , "bob")
$redis.expects(:setex).with(key, 60 , Marshal.dump("bob"))

r = cache.fetch("key", expires_in: 1.minute) do
cache.fetch("key", expires_in: 1.minute) do
"bob"
end
r.should == "bob"
end

it "can store and fetch correctly" do
key = cache.namespaced_key("key")
$redis.expects(:get).with(key).returns nil
$redis.expects(:set).with(key, "bob")
cache.delete "key"

r = cache.fetch "key" do
"bob"
Expand All @@ -58,9 +68,7 @@
end

it "can fetch existing correctly" do
key = cache.namespaced_key("key")

$redis.expects(:get).with(key).returns "bill"
cache.write "key", "bill"

r = cache.fetch "key" do
"bob"
Expand Down

0 comments on commit 660d87a

Please sign in to comment.