Class: RedisThrottle

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.redisObject

Returns the value of attribute redis.



4
5
6
# File 'lib/redis_throttle.rb', line 4

def redis
  @redis
end

Class Method Details

.block_id(&block) ⇒ Object

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



14
15
16
17
# File 'lib/redis_throttle.rb', line 14

def block_id &block
  block.inspect =~ /(\w+\.rb:\d+)/
  $1
end

.limit(time_in_secs, *args, &block) ⇒ Object



6
7
8
9
10
11
# 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