redis-mutex 2.0.0 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +3 -2
- data/lib/redis/mutex.rb +5 -0
- data/redis-mutex.gemspec +1 -1
- data/spec/redis_mutex_spec.rb +19 -0
- metadata +28 -28
data/README.md
CHANGED
@@ -70,10 +70,11 @@ There are a number of methods:
|
|
70
70
|
|
71
71
|
```ruby
|
72
72
|
mutex = Redis::Mutex.new(key, options) # Configure a mutex lock
|
73
|
-
mutex.lock # Try to acquire the lock
|
74
|
-
mutex.unlock # Try to release the lock
|
73
|
+
mutex.lock # Try to acquire the lock, returns false when failed
|
75
74
|
mutex.lock! # Try to acquire the lock, raises exception when failed
|
75
|
+
mutex.unlock # Try to release the lock, returns false when failed
|
76
76
|
mutex.unlock! # Try to release the lock, raises exception when failed
|
77
|
+
mutex.locked? # Find out if resource already locked
|
77
78
|
mutex.with_lock # Try to acquire the lock, execute the block, then return the value of the block.
|
78
79
|
# Raises exception when failed to acquire the lock.
|
79
80
|
|
data/lib/redis/mutex.rb
CHANGED
@@ -53,6 +53,11 @@ class Redis
|
|
53
53
|
return false # Dammit, it seems that someone else was even faster than us to remove the expired lock!
|
54
54
|
end
|
55
55
|
|
56
|
+
# Returns true if resource is locked. Note that nil.to_f returns 0.0
|
57
|
+
def locked?
|
58
|
+
get.to_f > Time.now.to_f
|
59
|
+
end
|
60
|
+
|
56
61
|
def unlock(force = false)
|
57
62
|
# Since it's possible that the operations in the critical section took a long time,
|
58
63
|
# we can't just simply release the lock. The unlock method checks if @expires_at
|
data/redis-mutex.gemspec
CHANGED
@@ -12,7 +12,7 @@ Gem::Specification.new do |gem|
|
|
12
12
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
13
13
|
gem.name = "redis-mutex"
|
14
14
|
gem.require_paths = ["lib"]
|
15
|
-
gem.version = '2.
|
15
|
+
gem.version = '2.1.0' # retrieve this value by: Gem.loaded_specs['redis-mutex'].version.to_s
|
16
16
|
|
17
17
|
gem.add_runtime_dependency "redis-classy", "~> 1.2"
|
18
18
|
gem.add_development_dependency "rspec"
|
data/spec/redis_mutex_spec.rb
CHANGED
@@ -103,6 +103,25 @@ describe Redis::Mutex do
|
|
103
103
|
mutex.lock.should be_false
|
104
104
|
end
|
105
105
|
|
106
|
+
it 'tells about lock\'s state' do
|
107
|
+
mutex = Redis::Mutex.new(:test_lock, SHORT_MUTEX_OPTIONS)
|
108
|
+
mutex.lock
|
109
|
+
|
110
|
+
mutex.should be_locked
|
111
|
+
|
112
|
+
mutex.unlock
|
113
|
+
mutex.should_not be_locked
|
114
|
+
end
|
115
|
+
|
116
|
+
it 'tells that resource is not locked when lock is expired' do
|
117
|
+
mutex = Redis::Mutex.new(:test_lock, :expire => 0.1)
|
118
|
+
mutex.lock
|
119
|
+
|
120
|
+
sleep 0.2 # lock expired now
|
121
|
+
|
122
|
+
mutex.should_not be_locked
|
123
|
+
end
|
124
|
+
|
106
125
|
it 'returns value of block' do
|
107
126
|
Redis::Mutex.with_lock(:test_lock) { :test_result }.should == :test_result
|
108
127
|
end
|
metadata
CHANGED
@@ -1,80 +1,80 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redis-mutex
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.0
|
5
4
|
prerelease:
|
5
|
+
version: 2.1.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Kenn Ejima
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-01-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
|
-
|
16
|
-
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
15
|
+
version_requirements: !ruby/object:Gem::Requirement
|
18
16
|
requirements:
|
19
17
|
- - ~>
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '1.2'
|
22
|
-
type: :runtime
|
23
|
-
prerelease: false
|
24
|
-
version_requirements: !ruby/object:Gem::Requirement
|
25
20
|
none: false
|
21
|
+
prerelease: false
|
22
|
+
name: redis-classy
|
23
|
+
requirement: !ruby/object:Gem::Requirement
|
26
24
|
requirements:
|
27
25
|
- - ~>
|
28
26
|
- !ruby/object:Gem::Version
|
29
27
|
version: '1.2'
|
28
|
+
none: false
|
29
|
+
type: :runtime
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
|
+
version_requirements: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - ! '>='
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
none: false
|
37
|
+
prerelease: false
|
31
38
|
name: rspec
|
32
39
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
40
|
requirements:
|
35
41
|
- - ! '>='
|
36
42
|
- !ruby/object:Gem::Version
|
37
43
|
version: '0'
|
44
|
+
none: false
|
38
45
|
type: :development
|
39
|
-
|
46
|
+
- !ruby/object:Gem::Dependency
|
40
47
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
48
|
requirements:
|
43
49
|
- - ! '>='
|
44
50
|
- !ruby/object:Gem::Version
|
45
51
|
version: '0'
|
46
|
-
|
52
|
+
none: false
|
53
|
+
prerelease: false
|
47
54
|
name: bundler
|
48
55
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
56
|
requirements:
|
51
57
|
- - ! '>='
|
52
58
|
- !ruby/object:Gem::Version
|
53
59
|
version: '0'
|
60
|
+
none: false
|
54
61
|
type: :development
|
55
|
-
|
62
|
+
- !ruby/object:Gem::Dependency
|
56
63
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
64
|
requirements:
|
59
65
|
- - ! '>='
|
60
66
|
- !ruby/object:Gem::Version
|
61
67
|
version: '0'
|
62
|
-
|
68
|
+
none: false
|
69
|
+
prerelease: false
|
63
70
|
name: rake
|
64
71
|
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
72
|
requirements:
|
67
73
|
- - ! '>='
|
68
74
|
- !ruby/object:Gem::Version
|
69
75
|
version: '0'
|
70
|
-
type: :development
|
71
|
-
prerelease: false
|
72
|
-
version_requirements: !ruby/object:Gem::Requirement
|
73
76
|
none: false
|
74
|
-
|
75
|
-
- - ! '>='
|
76
|
-
- !ruby/object:Gem::Version
|
77
|
-
version: '0'
|
77
|
+
type: :development
|
78
78
|
description: Distrubuted mutex using Redis
|
79
79
|
email:
|
80
80
|
- kenn.ejima@gmail.com
|
@@ -102,23 +102,23 @@ rdoc_options: []
|
|
102
102
|
require_paths:
|
103
103
|
- lib
|
104
104
|
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
-
none: false
|
106
105
|
requirements:
|
107
106
|
- - ! '>='
|
108
107
|
- !ruby/object:Gem::Version
|
109
108
|
version: '0'
|
110
109
|
segments:
|
111
110
|
- 0
|
112
|
-
hash: -
|
113
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
hash: -4158529389064232866
|
114
112
|
none: false
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
114
|
requirements:
|
116
115
|
- - ! '>='
|
117
116
|
- !ruby/object:Gem::Version
|
118
117
|
version: '0'
|
119
118
|
segments:
|
120
119
|
- 0
|
121
|
-
hash: -
|
120
|
+
hash: -4158529389064232866
|
121
|
+
none: false
|
122
122
|
requirements: []
|
123
123
|
rubyforge_project:
|
124
124
|
rubygems_version: 1.8.24
|