add read/write methods to cache

This commit is contained in:
madlep 2013-06-06 15:48:53 +10:00
parent f73fd1ab4e
commit 22fc386bad
2 changed files with 30 additions and 0 deletions

View file

@ -29,6 +29,14 @@ module Rack
do_count(key, expires_in)
end
def read(unprefixed_key)
store.read("#{prefix}:#{unprefixed_key}")
end
def write(unprefixed_key, value, expires_in)
store.write("#{prefix}:#{unprefixed_key}", value, :expires_in => expires_in)
end
private
def do_count(key, expires_in)
# Workaround Redis::Store's interface

View file

@ -51,6 +51,28 @@ if ENV['TEST_INTEGRATION']
@cache.send(:do_count, @key, @expires_in).must_equal 1
end
end
describe "write" do
it "should write a value to the store with prefix" do
@cache.write("cache-test-key", "foobar", 1)
store.read(@key).must_equal "foobar"
end
end
describe "write after expiry" do
it "must not have a value" do
@cache.write("cache-test-key", "foobar", @expires_in)
sleep @expires_in # tick... tick... tick...
store.read(@key).must_be :nil?
end
end
describe "read" do
it "must read the value with a prefix" do
store.write(@key, "foobar", :expires_in => @expires_in)
@cache.read("cache-test-key").must_equal "foobar"
end
end
end
end