From 853c9ceef3acf6e619eb8179d828ef8eded966d3 Mon Sep 17 00:00:00 2001 From: Aaron Suggs Date: Thu, 9 Aug 2012 10:58:25 -0400 Subject: [PATCH] Set :expires_in on throttle cache counters Should reduce memcached evictions --- lib/rack/attack/cache.rb | 8 +++++--- lib/rack/attack/version.rb | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/rack/attack/cache.rb b/lib/rack/attack/cache.rb index 2bedd1f..46d7ad2 100644 --- a/lib/rack/attack/cache.rb +++ b/lib/rack/attack/cache.rb @@ -9,11 +9,13 @@ module Rack end def count(unprefixed_key, period) - key = "#{prefix}:#{Time.now.to_i/period}:#{unprefixed_key}" - result = store.increment(key, 1) + epoch_time = Time.now.to_i + expires_in = period - (epoch_time % period) + key = "#{prefix}:#{epoch_time/period}:#{unprefixed_key}" + result = store.increment(key, 1, :expires_in => expires_in) # NB: Some stores return nil when incrementing uninitialized values if result.nil? - store.write(key, 1) + store.write(key, 1, :expires_in => expires_in) end result || 1 end diff --git a/lib/rack/attack/version.rb b/lib/rack/attack/version.rb index 91ccab8..d2472cf 100644 --- a/lib/rack/attack/version.rb +++ b/lib/rack/attack/version.rb @@ -1,5 +1,5 @@ module Rack module Attack - VERSION = '1.3.0' + VERSION = '1.3.1' end end