kredis 1.6.1 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f90d8ba8eadeb9cc6dbd2ca4bbb8532629281595e5a1feccd42487b98f248e67
4
- data.tar.gz: 3d5e2768e7647d27bb004bc7167f9b35b97702bdcc3e3f6f76032abd3b85bb5d
3
+ metadata.gz: 4b979e9602301ecfbea9a89c99cc1b0f3763b26990c9264d7cb767aa258aaac5
4
+ data.tar.gz: cdd19bdd88263e1cbeae92cf40ad224cc7f84faa3965bd85f4e1f82487964ddd
5
5
  SHA512:
6
- metadata.gz: 590e5baac4b4a2c85c4867f6ee55d4598c3b21e9c9b22961556626f59e8b17524cc4e4e9040f9109d30671ba4b615b074edcad5723df459f75a10dd1da956a23
7
- data.tar.gz: 8ad76868f04ac0c0828485f2e03ce93fc8514eb32e2661028adcbc6ff9866f6016d26530b6af30f8df15009723e01797fc913084bb6bcb219f36558db7ed2768
6
+ metadata.gz: 26e3ce8f1580e01c7177ba79caf93b2a3e77276a03e4de30968633e8de675341ab12a16d08f113fe67631bd60c334631d33c6e41bfa70301778e3bf26d64bb70
7
+ data.tar.gz: 86dd839a25c227d18e04492d6d3a58526be5687fd4dcf20ff94eb127522b51e98b6acbfda4dc0059a56ab22b4a6ff2b4d81b59884636aed03f7ab735fe5b3de7
data/README.md CHANGED
@@ -161,6 +161,20 @@ sleep 0.5.seconds
161
161
  true == flag.marked? #=> EXISTS myflag
162
162
  sleep 0.6.seconds
163
163
  false == flag.marked? #=> EXISTS myflag
164
+
165
+ limiter = Kredis.limiter "mylimit", limit: 3, expires_in: 5.seconds
166
+ 0 == limiter.value # => GET "limiter"
167
+ limiter.poke # => SET limiter 0 NX + INCRBY limiter 1
168
+ limiter.poke # => SET limiter 0 NX + INCRBY limiter 1
169
+ limiter.poke # => SET limiter 0 NX + INCRBY limiter 1
170
+ false == limiter.exceeded? # => GET "limiter"
171
+ limiter.poke # => SET limiter 0 NX + INCRBY limiter 1
172
+ true == limiter.exceeded? # => GET "limiter"
173
+ sleep 6
174
+ limiter.poke # => SET limiter 0 NX + INCRBY limiter 1
175
+ limiter.poke # => SET limiter 0 NX + INCRBY limiter 1
176
+ limiter.poke # => SET limiter 0 NX + INCRBY limiter 1
177
+ false == limiter.exceeded? # => GET "limiter"
164
178
  ```
165
179
 
166
180
  ### Models
@@ -72,6 +72,10 @@ module Kredis::Attributes
72
72
  kredis_connection_with __method__, name, key, default: default, config: config, after_change: after_change, expires_in: expires_in
73
73
  end
74
74
 
75
+ def kredis_limiter(name, limit:, key: nil, config: :shared, after_change: nil, expires_in: nil)
76
+ kredis_connection_with __method__, name, key, limit: limit, config: config, after_change: after_change, expires_in: expires_in
77
+ end
78
+
75
79
  def kredis_hash(name, key: nil, default: nil, typed: :string, config: :shared, after_change: nil)
76
80
  kredis_connection_with __method__, name, key, default: default, typed: typed, config: config, after_change: after_change
77
81
  end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ # A limiter is a specialized form of a counter that can be checked whether it has been exceeded and is provided fail safe. This means it can be used to guard login screens from brute force attacks without denying access in case Redis is offline.
4
+ #
5
+ # It will usually be used as an expiring limiter. Note that the limiter expires in total after the `expires_in` time used upon the first poke.
6
+ #
7
+ # It offers no guarentee that you can't poke yourself above the limit. You're responsible for checking `#exceeded?` yourself first, and this may produce a race condition. So only use this when the exact number of pokes is not critical.
8
+ class Kredis::Types::Limiter < Kredis::Types::Counter
9
+ class LimitExceeded < StandardError; end
10
+
11
+ attr_accessor :limit
12
+
13
+ def poke
14
+ failsafe returning: true do
15
+ increment
16
+ end
17
+ end
18
+
19
+ def exceeded?
20
+ failsafe returning: false do
21
+ value >= limit
22
+ end
23
+ end
24
+ end
data/lib/kredis/types.rb CHANGED
@@ -85,6 +85,10 @@ module Kredis::Types
85
85
  type_from(Slots, config, key, after_change: after_change, available: available)
86
86
  end
87
87
 
88
+ def limiter(key, limit:, expires_in: nil, config: :shared, after_change: nil)
89
+ type_from(Limiter, config, key, after_change: after_change, expires_in: expires_in, limit: limit)
90
+ end
91
+
88
92
  private
89
93
  def type_from(type_klass, config, key, after_change: nil, **options)
90
94
  type_klass.new(configured_for(config), namespaced_key(key), **options).then do |type|
@@ -107,3 +111,4 @@ require "kredis/types/unique_list"
107
111
  require "kredis/types/set"
108
112
  require "kredis/types/ordered_set"
109
113
  require "kredis/types/slots"
114
+ require "kredis/types/limiter"
@@ -1,3 +1,3 @@
1
1
  module Kredis
2
- VERSION = "1.6.1"
2
+ VERSION = "1.7.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kredis
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.1
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kasper Timm Hansen
@@ -102,6 +102,7 @@ files:
102
102
  - lib/kredis/types/enum.rb
103
103
  - lib/kredis/types/flag.rb
104
104
  - lib/kredis/types/hash.rb
105
+ - lib/kredis/types/limiter.rb
105
106
  - lib/kredis/types/list.rb
106
107
  - lib/kredis/types/ordered_set.rb
107
108
  - lib/kredis/types/proxy.rb