Class: Gitlab::RackAttack::Store
- Inherits:
-
Object
- Object
- Gitlab::RackAttack::Store
- Defined in:
- lib/gitlab/rack_attack/store.rb
Constant Summary collapse
- InvalidAmount =
Class.new(StandardError)
Instance Method Summary collapse
- #delete(key, _options = {}) ⇒ Object
-
#increment(key, amount = 1, options = {}) ⇒ Object
The increment method gets called very often.
- #read(key, _options = {}) ⇒ Object
- #write(key, value, options = {}) ⇒ Object
Instance Method Details
#delete(key, _options = {}) ⇒ Object
34 35 36 |
# File 'lib/gitlab/rack_attack/store.rb', line 34 def delete(key, = {}) with { |redis| redis.del(namespace(key)) } end |
#increment(key, amount = 1, options = {}) ⇒ Object
The increment method gets called very often. The implementation below aims to minimize the number of Redis calls we make.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/gitlab/rack_attack/store.rb', line 10 def increment(key, amount = 1, = {}) # Our code below that prevents calling EXPIRE after every INCR assumes # we always increment by 1. This is true in Rack::Attack as of v6.6.1. # This guard should alert us if Rack::Attack changes its behavior in a # future version. raise InvalidAmount unless amount == 1 with do |redis| key = namespace(key) new_value = redis.incr(key) expires_in = [:expires_in] redis.expire(key, expires_in) if new_value == 1 && expires_in new_value end end |
#read(key, _options = {}) ⇒ Object
26 27 28 |
# File 'lib/gitlab/rack_attack/store.rb', line 26 def read(key, = {}) with { |redis| redis.get(namespace(key)) } end |
#write(key, value, options = {}) ⇒ Object
30 31 32 |
# File 'lib/gitlab/rack_attack/store.rb', line 30 def write(key, value, = {}) with { |redis| redis.set(namespace(key), value, ex: [:expires_in]) } end |