2013-05-13 16:04:03 +08:00
|
|
|
require 'spec_helper'
|
|
|
|
require 'cache'
|
|
|
|
|
|
|
|
describe Cache do
|
|
|
|
|
|
|
|
let :cache do
|
|
|
|
Cache.new
|
|
|
|
end
|
|
|
|
|
2014-01-07 14:36:47 +08:00
|
|
|
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
|
|
|
|
|
2014-01-06 13:50:04 +08:00
|
|
|
it "can be cleared" do
|
|
|
|
cache.write("hello0", "world")
|
|
|
|
cache.write("hello1", "world")
|
|
|
|
cache.clear
|
2013-05-13 16:04:03 +08:00
|
|
|
|
2014-09-25 23:44:48 +08:00
|
|
|
cache.read("hello0").should == nil
|
2014-01-06 13:50:04 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can delete by family" do
|
|
|
|
cache.write("key2", "test", family: "my_family")
|
|
|
|
cache.write("key", "test", expires_in: 1.minute, family: "my_family")
|
2013-05-13 16:04:03 +08:00
|
|
|
|
|
|
|
cache.delete_by_family("my_family")
|
2014-01-06 13:50:04 +08:00
|
|
|
|
2014-09-25 23:44:48 +08:00
|
|
|
cache.fetch("key").should == nil
|
|
|
|
cache.fetch("key2").should == nil
|
2013-05-13 16:04:03 +08:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can delete correctly" do
|
2014-01-06 13:50:04 +08:00
|
|
|
cache.fetch("key", expires_in: 1.minute) do
|
2013-05-13 16:04:03 +08:00
|
|
|
"test"
|
|
|
|
end
|
|
|
|
|
|
|
|
cache.delete("key")
|
2014-09-25 23:44:48 +08:00
|
|
|
cache.fetch("key").should == nil
|
2013-05-13 16:04:03 +08:00
|
|
|
end
|
|
|
|
|
2014-01-07 14:36:47 +08:00
|
|
|
#TODO yuck on this mock
|
|
|
|
it "calls setex in redis" do
|
|
|
|
cache.delete("key")
|
|
|
|
|
2014-01-06 13:50:04 +08:00
|
|
|
key = cache.namespaced_key("key")
|
2014-01-07 14:36:47 +08:00
|
|
|
$redis.expects(:setex).with(key, 60 , Marshal.dump("bob"))
|
2013-05-13 16:04:03 +08:00
|
|
|
|
2014-01-07 14:36:47 +08:00
|
|
|
cache.fetch("key", expires_in: 1.minute) do
|
2013-05-13 16:04:03 +08:00
|
|
|
"bob"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can store and fetch correctly" do
|
2014-01-07 14:36:47 +08:00
|
|
|
cache.delete "key"
|
2013-05-13 16:04:03 +08:00
|
|
|
|
|
|
|
r = cache.fetch "key" do
|
|
|
|
"bob"
|
|
|
|
end
|
|
|
|
r.should == "bob"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can fetch existing correctly" do
|
2014-01-07 14:36:47 +08:00
|
|
|
cache.write "key", "bill"
|
2013-05-13 16:04:03 +08:00
|
|
|
|
|
|
|
r = cache.fetch "key" do
|
|
|
|
"bob"
|
|
|
|
end
|
|
|
|
r.should == "bill"
|
|
|
|
end
|
|
|
|
end
|