Class: Rack::Ratelimit::MemcachedCounter
- Inherits:
-
Object
- Object
- Rack::Ratelimit::MemcachedCounter
- Defined in:
- lib/rack/ratelimit.rb
Instance Method Summary collapse
-
#increment(classification, timestamp) ⇒ Object
Increment the request counter and return the current count.
-
#initialize(cache, name, period) ⇒ MemcachedCounter
constructor
A new instance of MemcachedCounter.
Constructor Details
#initialize(cache, name, period) ⇒ MemcachedCounter
Returns a new instance of MemcachedCounter.
158 159 160 |
# File 'lib/rack/ratelimit.rb', line 158 def initialize(cache, name, period) @cache, @name, @period = cache, name, period end |
Instance Method Details
#increment(classification, timestamp) ⇒ Object
Increment the request counter and return the current count.
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/rack/ratelimit.rb', line 163 def increment(classification, ) key = 'rack-ratelimit/%s/%s/%i' % [@name, classification, ] # Try to increment the counter if it's present. if count = @cache.incr(key, 1) count.to_i # If not, add the counter and set expiry. elsif @cache.add(key, 1, @period, :raw => true) 1 # If adding failed, someone else added it concurrently. Increment. else @cache.incr(key, 1).to_i end end |