Class: RedisKnock::Control

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

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Control

Returns a new instance of Control.



5
6
7
8
9
10
11
12
# File 'lib/redis_knock/control.rb', line 5

def initialize(args)
  options = convert_keys_to_symbols args
  check! options

  @client = get_connection options[:redis]
  @limit = options[:limit]
  @interval = options[:interval]
end

Instance Method Details

#allowed?(request) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
# File 'lib/redis_knock/control.rb', line 14

def allowed?(request)
  cache_key = get_cache_key request

  count = @client.incr cache_key
  @client.expire(cache_key, @interval) if count == 1

  count <= @limit
end