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) def read_entry(key, options)
if data = redis.get(key) if data = redis.get(key)
data = Marshal.load(data)
ActiveSupport::Cache::Entry.new data ActiveSupport::Cache::Entry.new data
end end
rescue
# corrupt cache, fail silently for now, remove rescue later
end end
def write_entry(key, entry, options) def write_entry(key, entry, options)
dumped = Marshal.dump(entry.value)
if expiry = options[:expires_in] if expiry = options[:expires_in]
redis.setex(key, expiry, entry.value) redis.setex(key, expiry, dumped)
else else
redis.set(key, entry.value) redis.set(key, dumped)
end end
if family = family_key(options[:family], options) if family = family_key(options[:family], options)

View File

@ -7,6 +7,17 @@ describe Cache do
Cache.new Cache.new
end 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 it "can be cleared" do
cache.write("hello0", "world") cache.write("hello0", "world")
cache.write("hello1", "world") cache.write("hello1", "world")
@ -35,21 +46,20 @@ describe Cache do
cache.fetch("key").should be_nil cache.fetch("key").should be_nil
end end
it "can store with expiry correctly" do #TODO yuck on this mock
key = cache.namespaced_key("key") it "calls setex in redis" do
$redis.expects(:get).with(key).returns nil cache.delete("key")
$redis.expects(:setex).with(key, 60 , "bob")
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" "bob"
end end
r.should == "bob"
end end
it "can store and fetch correctly" do it "can store and fetch correctly" do
key = cache.namespaced_key("key") cache.delete "key"
$redis.expects(:get).with(key).returns nil
$redis.expects(:set).with(key, "bob")
r = cache.fetch "key" do r = cache.fetch "key" do
"bob" "bob"
@ -58,9 +68,7 @@ describe Cache do
end end
it "can fetch existing correctly" do it "can fetch existing correctly" do
key = cache.namespaced_key("key") cache.write "key", "bill"
$redis.expects(:get).with(key).returns "bill"
r = cache.fetch "key" do r = cache.fetch "key" do
"bob" "bob"