Class: RedisLock
- Inherits:
-
Object
show all
- Defined in:
- lib/redis_lock.rb,
lib/redis_lock/if_open.rb,
lib/redis_lock/version.rb,
lib/redis_lock/strategy.rb,
lib/redis_lock/if_locked.rb,
lib/redis_lock/semaphore.rb,
lib/redis_lock/multi_lock.rb,
lib/redis_lock/configuration.rb
Defined Under Namespace
Classes: Configuration, IfLocked, IfOpen, MultiLock, Semaphore, Strategy
Constant Summary
collapse
- VERSION =
"0.4.4"
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(key, opts = {}) ⇒ RedisLock
Returns a new instance of RedisLock.
32
33
34
35
|
# File 'lib/redis_lock.rb', line 32
def initialize(key, opts = {})
@key = "REDISLOCK::#{key}"
@redis = opts[:redis]
end
|
Instance Attribute Details
#key ⇒ Object
Returns the value of attribute key.
5
6
7
|
# File 'lib/redis_lock.rb', line 5
def key
@key
end
|
Class Method Details
.config ⇒ Object
7
8
9
|
# File 'lib/redis_lock.rb', line 7
def self.config
@config ||= Configuration.new
end
|
95
96
97
|
# File 'lib/redis_lock.rb', line 95
def self.(args)
args.last.is_a?(::Hash) ? args.pop : {}
end
|
.if_locked(*args, &block) ⇒ Object
25
26
27
28
|
# File 'lib/redis_lock.rb', line 25
def self.if_locked(*args, &block)
opts = (args)
IfLocked.new(MultiLock.new(*args, opts), opts).call(&block)
end
|
.if_open(*args, &block) ⇒ Object
20
21
22
23
|
# File 'lib/redis_lock.rb', line 20
def self.if_open(*args, &block)
opts = (args)
IfOpen.new(MultiLock.new(*args, opts), opts).call(&block)
end
|
.semaphore(*args, &block) ⇒ Object
15
16
17
18
|
# File 'lib/redis_lock.rb', line 15
def self.semaphore(*args, &block)
opts = (args)
Semaphore.new(MultiLock.new(*args, opts), opts).call(&block)
end
|
.setup {|config| ... } ⇒ Object
11
12
13
|
# File 'lib/redis_lock.rb', line 11
def self.setup
yield config
end
|
Instance Method Details
#config ⇒ Object
30
|
# File 'lib/redis_lock.rb', line 30
def config; self.class.config; end
|
#delete ⇒ Object
Also known as:
unlock!, open!, remove
84
85
86
|
# File 'lib/redis_lock.rb', line 84
def delete
redis.del(key) == 1 ? true : false
end
|
#if_locked(opts = {}, &block) ⇒ Object
65
66
67
|
# File 'lib/redis_lock.rb', line 65
def if_locked(opts = {}, &block)
IfLocked.new(self, opts).call(&block)
end
|
#if_open(opts = {}, &block) ⇒ Object
Also known as:
perform
60
61
62
|
# File 'lib/redis_lock.rb', line 60
def if_open(opts = {}, &block)
IfOpen.new(self, opts).call(&block)
end
|
#locked? ⇒ Boolean
Also known as:
exists?, in_use?
69
70
71
|
# File 'lib/redis_lock.rb', line 69
def locked?
ttl == -2 ? false : true
end
|
#open? ⇒ Boolean
Also known as:
unlocked?
79
80
81
|
# File 'lib/redis_lock.rb', line 79
def open?
!locked?
end
|
#redis ⇒ Object
37
38
39
|
# File 'lib/redis_lock.rb', line 37
def redis
@redis ||= config.redis
end
|
#semaphore(opts = {}, &block) ⇒ Object
56
57
58
|
# File 'lib/redis_lock.rb', line 56
def semaphore(opts = {}, &block)
Semaphore.new(self, opts).call(&block)
end
|
#set(expiration_time = 60, opts = {}) ⇒ Object
Redis SET options:
-
EX seconds – Set the specified expire time, in seconds.
-
PX milliseconds – Set the specified expire time, in milliseconds.
-
NX – Only set the key if it does not already exist.
-
XX – Only set the key if it already exist.
46
47
48
49
50
51
52
53
54
|
# File 'lib/redis_lock.rb', line 46
def set(expiration_time = 60, opts = {})
value = opts.delete(:value) || Time.now.strftime('%FT%T')
args = if opts[:px]
{ px: expiration_time }
else
{ ex: expiration_time }
end
redis.set(key, value, args.merge(opts)) == "OK" ? true : false
end
|
#ttl ⇒ Object
75
76
77
|
# File 'lib/redis_lock.rb', line 75
def ttl
redis.ttl(key)
end
|
#value ⇒ Object
91
92
93
|
# File 'lib/redis_lock.rb', line 91
def value
redis.get(key)
end
|