diff --git a/lib/rack/attack/cache.rb b/lib/rack/attack/cache.rb index e5b3fc1..2388283 100644 --- a/lib/rack/attack/cache.rb +++ b/lib/rack/attack/cache.rb @@ -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 diff --git a/spec/rack_attack_cache_spec.rb b/spec/rack_attack_cache_spec.rb index 1419688..26287bc 100644 --- a/spec/rack_attack_cache_spec.rb +++ b/spec/rack_attack_cache_spec.rb @@ -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