Class: Rack::Ratelimit::RedisCounter

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/ratelimit.rb

Instance Method Summary collapse

Constructor Details

#initialize(redis, name, period) ⇒ RedisCounter

Returns a new instance of RedisCounter.



182
183
184
# File 'lib/rack/ratelimit.rb', line 182

def initialize(redis, name, period)
  @redis, @name, @period = redis, name, period
end

Instance Method Details

#increment(classification, timestamp) ⇒ Object

Increment the request counter and return the current count.



187
188
189
190
191
192
193
194
195
196
# File 'lib/rack/ratelimit.rb', line 187

def increment(classification, timestamp)
  key = 'rack-ratelimit/%s/%s/%i' % [@name, classification, timestamp]

  # Returns [count, expire_ok] response for each multi command.
  # Return the first, the count.
  @redis.multi do |redis|
    redis.incr key
    redis.expire key, @period
  end.first
end