mirror of
https://github.com/samsonjs/rack-attack.git
synced 2026-03-25 09:25:49 +00:00
add read/write methods to cache
This commit is contained in:
parent
f73fd1ab4e
commit
22fc386bad
2 changed files with 30 additions and 0 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue