BUGFIX: cache did not support non strings

also reduced mocking in tests
This commit is contained in:
Sam 2014-01-07 17:36:47 +11:00
parent e0284dfef0
commit 660d87a108
2 changed files with 27 additions and 14 deletions

View file

@ -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)

View file

@ -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"