class Rack::Ratelimit::MemcachedCounter

Public Class Methods

new(cache, name, period) click to toggle source
# File lib/rack/ratelimit.rb, line 187
def initialize(cache, name, period)
  @cache, @name, @period = cache, name, period
end

Public Instance Methods

increment(classification, epoch) click to toggle source

Increment the request counter and return the current count.

# File lib/rack/ratelimit.rb, line 192
def increment(classification, epoch)
  key = 'rack-ratelimit/%s/%s/%i' % [@name, classification, epoch]

  # 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
rescue Dalli::DalliError
  0
end