From 660d87a108c4d668373066e2e06c36c5edec3214 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 7 Jan 2014 17:36:47 +1100 Subject: [PATCH] BUGFIX: cache did not support non strings also reduced mocking in tests --- lib/cache.rb | 9 +++++++-- spec/components/cache_spec.rb | 32 ++++++++++++++++++++------------ 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/lib/cache.rb b/lib/cache.rb index 206ef5230d7..819bddfb72e 100644 --- a/lib/cache.rb +++ b/lib/cache.rb @@ -38,15 +38,20 @@ class Cache < ActiveSupport::Cache::Store 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) diff --git a/spec/components/cache_spec.rb b/spec/components/cache_spec.rb index 4fdf5963eb2..0c9b14c5460 100644 --- a/spec/components/cache_spec.rb +++ b/spec/components/cache_spec.rb @@ -7,6 +7,17 @@ describe Cache do 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") @@ -35,21 +46,20 @@ describe Cache do cache.fetch("key").should be_nil end - it "can store with expiry correctly" do - key = cache.namespaced_key("key") - $redis.expects(:get).with(key).returns nil - $redis.expects(:setex).with(key, 60 , "bob") + #TODO yuck on this mock + it "calls setex in redis" do + cache.delete("key") - r = cache.fetch("key", expires_in: 1.minute) do + key = cache.namespaced_key("key") + $redis.expects(:setex).with(key, 60 , Marshal.dump("bob")) + + 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" @@ -58,9 +68,7 @@ describe Cache do 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"