Class: RedisLock

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_lock.rb,
lib/redis_lock/version.rb,
lib/redis_lock/configuration.rb

Defined Under Namespace

Classes: Configuration

Constant Summary collapse

VERSION =
"0.3.1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, opts = {}) ⇒ RedisLock

Returns a new instance of RedisLock.



30
31
32
33
# File 'lib/redis_lock.rb', line 30

def initialize(key, opts = {})
  @key = "REDISLOCK::#{key}"
  @redis = opts[:redis]
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



6
7
8
# File 'lib/redis_lock.rb', line 6

def key
  @key
end

Class Method Details

.configObject



8
9
10
# File 'lib/redis_lock.rb', line 8

def self.config
  @config ||= Configuration.new
end

.if_locked(key, args = {}, &block) ⇒ Object



24
25
26
# File 'lib/redis_lock.rb', line 24

def self.if_locked(key, args = {}, &block)
  setup_instance(key, args).if_locked(args, &block)
end

.if_open(key, args = {}, &block) ⇒ Object



20
21
22
# File 'lib/redis_lock.rb', line 20

def self.if_open(key, args = {}, &block)
  setup_instance(key, args).if_open(args, &block)
end

.semaphore(key, args = {}, &block) ⇒ Object



16
17
18
# File 'lib/redis_lock.rb', line 16

def self.semaphore(key, args = {}, &block)
  setup_instance(key, args).semaphore(args, &block)
end

.setup {|config| ... } ⇒ Object

Yields:



12
13
14
# File 'lib/redis_lock.rb', line 12

def self.setup
  yield config
end

Instance Method Details

#configObject



28
# File 'lib/redis_lock.rb', line 28

def config; self.class.config; end

#deleteObject Also known as: unlock!, open!, remove



90
91
92
# File 'lib/redis_lock.rb', line 90

def delete
  redis.del(key) == 1 ? true : false
end

#if_locked(args = {}, &block) ⇒ Object



70
71
72
73
# File 'lib/redis_lock.rb', line 70

def if_locked(args = {}, &block)
  return if open?
  _perform(&block)
end

#if_open(args = {}, &block) ⇒ Object Also known as: perform



64
65
66
67
# File 'lib/redis_lock.rb', line 64

def if_open(args = {}, &block)
  return if locked?
  _perform(&block)
end

#locked?Boolean Also known as: exists?, in_use?

Returns:

  • (Boolean)


75
76
77
# File 'lib/redis_lock.rb', line 75

def locked?
  ttl == -2 ? false : true
end

#open?Boolean Also known as: unlocked?

Returns:

  • (Boolean)


85
86
87
# File 'lib/redis_lock.rb', line 85

def open?
  !locked?
end

#redisObject



35
36
37
# File 'lib/redis_lock.rb', line 35

def redis
  @redis ||= config.redis
end

#semaphore(args = {}, &block) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/redis_lock.rb', line 54

def semaphore(args = {}, &block)
  ttl = args[:ttl] || config.default_ttl
  set_opts = args[:set_opts] || {}
  while locked?
    sleep (args[:wait] || 3)
  end
  set(ttl, set_opts)
  _semaphore_perform(&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.



44
45
46
47
48
49
50
51
52
# File 'lib/redis_lock.rb', line 44

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

#ttlObject



81
82
83
# File 'lib/redis_lock.rb', line 81

def ttl
  redis.ttl(key)
end

#valueObject



97
98
99
# File 'lib/redis_lock.rb', line 97

def value
  redis.get(key)
end