redis_lock 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3ad868ca731e9080d8c65e079d50f14718f696e7
4
- data.tar.gz: 5c6e6dc414ed41926ffd3544db3f4f506971e225
3
+ metadata.gz: 61c884d3d8d49471ef1e435f294296691d7c093c
4
+ data.tar.gz: d6de74dedf9bc0b18a4711948dc4a3b029526bfa
5
5
  SHA512:
6
- metadata.gz: cd7fc6334ce0b05e4cc2d60d3bf275703e3207b84b659e3c55e222e6b1ef9ef6d630e239d5a58f70aa7dcf2d17494318f8e54bffbd6f2a406825cf136e395031
7
- data.tar.gz: eaf6171599bc5688c6a00e8bc926b2654f4a52c514753f83eae3cd5a5a370153c660cc2879687064531e47b0c91cb033e930f0ab6fc35a4d0a5ad466301d3086
6
+ metadata.gz: 1b09609658479ead559fd9e868cfdc3ec472a3284708a880a52a5db57179e48ba1cd04ea3516c4d4ef39fe5de8e404c5154df1ffce6e050f24951a02cbe17f3a
7
+ data.tar.gz: a8f50a8a1dab96d244ee8a223270d22bfe6d48477bb8f3980f885bc6920b4b0b64b9769706919e0708e0b553fb49f1d4e7166dcf70ab4d750fee8ecfb81233ef
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ vendor
data/Gemfile CHANGED
@@ -6,3 +6,4 @@ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
6
6
  gemspec
7
7
 
8
8
  gem 'dystruct'
9
+ gem 'pry'
data/README.md CHANGED
@@ -58,6 +58,11 @@ RedisLock.setup do |config|
58
58
  # logger
59
59
  # default: Logger.new(STDOUT)
60
60
  config.logger = Rails.logger
61
+
62
+ # Default ttl for all your locks
63
+ # default: 60
64
+ #
65
+ # config.default_ttl = 120
61
66
  end
62
67
  ```
63
68
 
@@ -73,28 +78,37 @@ lock.locked? #=> true
73
78
  lock.remove #=> true
74
79
  lock.locked? #=> false
75
80
  ```
76
- __as Mutex__
81
+
82
+
83
+ __semaphore:__
84
+ No one can perform the same operation while this is running the rest of the processes
85
+ are waiting while the lock is in use, When lock is released another one takes the lock.
86
+
87
+ args:
88
+ - key [string]
89
+ - opts: `{}`
90
+ * :redis
91
+ * :ttl, time to leave
92
+ * :set_opts, check `set` documentation
93
+ * :wait, time waiting for the next check if the lock is in use
94
+
77
95
  ```ruby
78
- lock = RedisLock.new('my_key')
79
- out = lock.if_open do |l|
80
- # no one can perform the same operation while this is running
81
- l.set(30) # place the lock so no one else can perform this tasks
96
+ out = RedisLock.semaphore('my_key') do |l|
82
97
  sleep 3 # Do something
83
- l.unlock! # release the lock
84
98
  :hello
85
99
  end
86
100
  out #=> :hello
87
- lock.locked? #=> false
101
+ RedisLock.new('my_key').locked? #=> false
88
102
  ```
89
103
 
90
- __blocking for a time__
104
+ __if_open:__
91
105
 
106
+ **Use case:**
92
107
  Send email to user. The User should receive only 1 email per day
93
108
 
94
109
  ```ruby
95
110
  ttl = (24 * 3600) # one day
96
- lock = RedisLock.new("User:1-sales-products")
97
- lock.if_open do |l|
111
+ RedisLock.if_open("User:1-sales-products", ttl: ttl) do |l|
98
112
  # Send Email
99
113
  l.set(ttl)
100
114
  end
@@ -102,15 +116,16 @@ end
102
116
 
103
117
  ## Methods:
104
118
 
105
- __set__
119
+ ### set
120
+
106
121
  Will store the key to redis with a ttl (time to live).
107
122
  args:
108
- - ttl # default: 60
109
- - opts # default: {}
110
- * value (String) - default: time now
111
- * px (true) - miliseconds instead of seconds default: false
112
- * nk (true) - Only set the key if it does not already exist.
113
- * xx (true) - Only set the key if it already exist.
123
+ - __ttl__ | default: 60
124
+ - __opts__ | default: {}
125
+ * __value__ (String) - default: time now
126
+ * __px__ - miliseconds instead of seconds | default: false
127
+ * __nk__ - Only set the key if it does not already exist. | default: false
128
+ * __xx__ - Only set the key if it already exist. | default: false
114
129
  ```ruby
115
130
  lock = RedisLock.new('my_key')
116
131
 
@@ -119,7 +134,7 @@ lock.ttl #=> 60
119
134
  lock.open? # => false
120
135
  ```
121
136
 
122
- _with options_
137
+ __with options:__
123
138
 
124
139
  ```ruby
125
140
  lock = RedisLock.new('my_key')
@@ -131,16 +146,17 @@ lock.open? # => false
131
146
  ```
132
147
 
133
148
  Redis documentation: https://redis.io/commands/set
134
-
149
+ ```
135
150
  Set key to hold the string value. If key already holds a value, it is overwritten, regardless of its type. Any previous time to live associated with the key is discarded on successful SET operation.
136
151
 
137
152
  EX seconds -- Set the specified expire time, in seconds.
138
153
  PX milliseconds -- Set the specified expire time, in milliseconds.
139
154
  NX -- Only set the key if it does not already exist.
140
155
  XX -- Only set the key if it already exist.
156
+ ```
141
157
 
158
+ ### locked?
142
159
 
143
- __locked?__
144
160
  Returns `true` if lock is set
145
161
 
146
162
  ```ruby
@@ -152,7 +168,8 @@ lock.locked? # => false
152
168
  ```
153
169
  _alias method:_ `exists?`
154
170
 
155
- __open?__
171
+ ### open?
172
+
156
173
  Returns `true` if NO lock is set
157
174
 
158
175
  ```ruby
@@ -163,7 +180,8 @@ lock.open? # => false
163
180
  ```
164
181
  _alias method:_ `unlocked?`
165
182
 
166
- __delete__
183
+ ### delete
184
+
167
185
  Removes the key from the Redis store
168
186
 
169
187
  ```ruby
@@ -175,7 +193,8 @@ lock.locked? # => false
175
193
  ```
176
194
  _alias methods:_ `unlock!`,`open!`
177
195
 
178
- __value__
196
+ ### value
197
+
179
198
  Returns the value stored in redis
180
199
 
181
200
  ```ruby
@@ -183,7 +202,8 @@ lock = RedisLock.new('my_key')
183
202
  lock.set(60, value: 'hi there!')
184
203
  lock.value # => 'hi there!'
185
204
  ```
186
- __ttl__
205
+ ### ttl
206
+
187
207
  Returns the pending ttl (time to live)
188
208
 
189
209
  ```ruby
@@ -22,7 +22,15 @@ class RedisLock
22
22
  end
23
23
 
24
24
  def logger
25
- @logger ? @logger : Logger.new(STDOUT)
25
+ @logger || Logger.new(STDOUT)
26
+ end
27
+
28
+ def default_ttl=(val)
29
+ @default_ttl = val
30
+ end
31
+
32
+ def default_ttl
33
+ @default_ttl || 60
26
34
  end
27
35
 
28
36
  private
@@ -1,3 +1,3 @@
1
1
  class RedisLock
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/redis_lock.rb CHANGED
@@ -13,10 +13,22 @@ class RedisLock
13
13
  yield config
14
14
  end
15
15
 
16
+ def self.semaphore(key, args = {}, &block)
17
+ setup_instance(key, args).semaphore(args, &block)
18
+ end
19
+
20
+ def self.if_open(key, args = {}, &block)
21
+ setup_instance(key, args).if_open(args, &block)
22
+ end
23
+
24
+ def self.if_locked(key, args = {}, &block)
25
+ setup_instance(key, args).if_locked(args, &block)
26
+ end
27
+
16
28
  def config; self.class.config; end
17
29
 
18
30
  def initialize(key, opts = {})
19
- @key = key
31
+ @key = "REDISLOCK::#{key}"
20
32
  @redis = opts[:redis]
21
33
  end
22
34
 
@@ -39,6 +51,18 @@ class RedisLock
39
51
  redis.set(key, value, args.merge(opts)) == "OK" ? true : false
40
52
  end
41
53
 
54
+ def semaphore(args = {}, &block)
55
+ ttl = args[:ttl] || config.default_ttl
56
+ set_opts = args[:set_opts] || {}
57
+ while locked?
58
+ sleep (args[:wait] || 3)
59
+ end
60
+ set(ttl, set_opts)
61
+ out = _perform(&block)
62
+ unlock!
63
+ out
64
+ end
65
+
42
66
  def if_open(args = {}, &block)
43
67
  return if locked?
44
68
  _perform(&block)
@@ -54,6 +78,7 @@ class RedisLock
54
78
  ttl == -2 ? false : true
55
79
  end
56
80
  alias_method :exists?, :locked?
81
+ alias_method :in_use?, :locked?
57
82
 
58
83
  def ttl
59
84
  redis.ttl(key)
@@ -77,6 +102,11 @@ class RedisLock
77
102
 
78
103
  private
79
104
 
105
+ def self.setup_instance(key, args)
106
+ inst_opts = { redis: args.delete(:redis) }.reject{ |_, v| v.nil? }
107
+ new(key, inst_opts)
108
+ end
109
+
80
110
  def _perform(&block)
81
111
  yield self
82
112
  rescue => e
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis_lock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artur Pañach
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-11-12 00:00:00.000000000 Z
11
+ date: 2018-03-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -117,7 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
117
117
  version: '0'
118
118
  requirements: []
119
119
  rubyforge_project:
120
- rubygems_version: 2.2.3
120
+ rubygems_version: 2.5.2.2
121
121
  signing_key:
122
122
  specification_version: 4
123
123
  summary: Lock with redis