class RedisThrottle

Attributes

redis[RW]

Public Class Methods

block_id(&block) click to toggle source

uses filename / line no. as identifier for block. cheap, but should generally work

# File lib/redis_throttle.rb, line 14
def block_id &block
  block.inspect =~ /(\w+\.rb:\d+)/
  $1
end
limit(time_in_secs, *args) { || ... } click to toggle source
# File lib/redis_throttle.rb, line 6
def limit(time_in_secs, *args, &block)
  key = ["redis-throttle", block_id(&block), *args].join('-')
  return if @redis.get(key)
  yield
  @redis.setex(key, time_in_secs, Time.now)
end