Class: Rack::Ratelimit::Counter
- Inherits:
-
Object
- Object
- Rack::Ratelimit::Counter
- Defined in:
- lib/rack/ratelimit.rb
Instance Method Summary collapse
-
#increment(classification, timestamp) ⇒ Object
Increment the request counter and return the current count.
-
#initialize(cache, name, period) ⇒ Counter
constructor
A new instance of Counter.
Constructor Details
#initialize(cache, name, period) ⇒ Counter
Returns a new instance of Counter.
144 145 146 |
# File 'lib/rack/ratelimit.rb', line 144 def initialize(cache, name, period) @cache, @name, @period = cache, name, period end |
Instance Method Details
#increment(classification, timestamp) ⇒ Object
Increment the request counter and return the current count.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/rack/ratelimit.rb', line 149 def increment(classification, ) key = 'rack-ratelimit/%s/%s/%i' % [@name, classification, ] # Try to increment the counter if it's present. if count = @cache.incr(key, 1) count.to_i # If not, add the counter and set expiry. elsif @cache.add(key, 1, @period, :raw => true) 1 # If adding failed, someone else added it concurrently. Increment. else @cache.incr(key, 1).to_i end end |