Class: Grape::Throttling::Counter
- Inherits:
-
Object
- Object
- Grape::Throttling::Counter
- Defined in:
- lib/grape/throttling/counter.rb
Overview
:nodoc:
Instance Method Summary collapse
- #get(key) ⇒ Object
- #incr(key) ⇒ Object
-
#initialize(expire) ⇒ Counter
constructor
A new instance of Counter.
Constructor Details
#initialize(expire) ⇒ Counter
Returns a new instance of Counter.
8 9 10 11 12 13 14 15 |
# File 'lib/grape/throttling/counter.rb', line 8 def initialize(expire) @expire = expire.try(:to_i).to_i env = ENV['RACK_ENV'].presence || 'unknow' @redis = Redis::Namespace.new( "grape-throttling:#{env}:counter", redis: Throttling.config.redis ) end |
Instance Method Details
#get(key) ⇒ Object
17 18 19 |
# File 'lib/grape/throttling/counter.rb', line 17 def get(key) @redis.get(key).to_i end |
#incr(key) ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/grape/throttling/counter.rb', line 21 def incr(key) response = begin @redis.incr(key) rescue Redis::CommandError => e del(key) == 1 ? @redis.incr(key) : (raise e) end response.tap { expire(key, @expire) if response == 1 && @expire.positive? } end |