lock_method 0.0.2 → 0.1.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.
- data/README.rdoc +7 -1
- data/lib/lock_method/config.rb +3 -1
- data/lib/lock_method/lock.rb +9 -5
- data/lib/lock_method/version.rb +1 -1
- data/lib/lock_method.rb +3 -3
- data/test/shared_tests.rb +2 -2
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -21,7 +21,13 @@ Then you can do
|
|
21
21
|
|
22
22
|
Just in case, you can clear them
|
23
23
|
|
24
|
-
my_blog.
|
24
|
+
my_blog.clear_method_lock :get_latest_entries
|
25
|
+
|
26
|
+
== One lock per method
|
27
|
+
|
28
|
+
lock_method ignores arguments when locking. So if you call Foo.bar(:a), calls to Foo.bar(:b) will be locked until the first call finishes.
|
29
|
+
|
30
|
+
Maybe future versions will support this.
|
25
31
|
|
26
32
|
== Configuration (and supported cache clients)
|
27
33
|
|
data/lib/lock_method/config.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'cache'
|
1
2
|
require 'singleton'
|
2
3
|
module LockMethod
|
3
4
|
# Here's where you set config options.
|
@@ -28,8 +29,9 @@ module LockMethod
|
|
28
29
|
def storage=(raw_client_or_nil)
|
29
30
|
if raw_client_or_nil.nil?
|
30
31
|
@storage = nil
|
32
|
+
elsif raw_client_or_nil.is_a?(::Cache)
|
33
|
+
@storage = raw_client_or_nil
|
31
34
|
else
|
32
|
-
require 'cache'
|
33
35
|
@storage = ::Cache.new raw_client_or_nil
|
34
36
|
end
|
35
37
|
end
|
data/lib/lock_method/lock.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
module LockMethod
|
2
2
|
class Lock #:nodoc: all
|
3
3
|
class << self
|
4
|
-
def find(
|
5
|
-
if hsh = Config.instance.storage.get(
|
4
|
+
def find(cache_key)
|
5
|
+
if hsh = Config.instance.storage.get(cache_key)
|
6
6
|
new hsh
|
7
7
|
end
|
8
8
|
end
|
@@ -59,7 +59,7 @@ module LockMethod
|
|
59
59
|
end
|
60
60
|
|
61
61
|
def delete
|
62
|
-
Config.instance.storage.delete
|
62
|
+
Config.instance.storage.delete cache_key
|
63
63
|
end
|
64
64
|
|
65
65
|
def save
|
@@ -68,7 +68,7 @@ module LockMethod
|
|
68
68
|
self.thread_object_id
|
69
69
|
self.expiry
|
70
70
|
# --
|
71
|
-
Config.instance.storage.set
|
71
|
+
Config.instance.storage.set cache_key, to_hash, ttl
|
72
72
|
end
|
73
73
|
|
74
74
|
def to_hash
|
@@ -79,11 +79,15 @@ module LockMethod
|
|
79
79
|
end
|
80
80
|
|
81
81
|
def locked?
|
82
|
-
if existing_lock = Lock.find(
|
82
|
+
if existing_lock = Lock.find(cache_key)
|
83
83
|
existing_lock.in_force?
|
84
84
|
end
|
85
85
|
end
|
86
86
|
|
87
|
+
def cache_key
|
88
|
+
[ 'LockMethod', 'Lock', method_signature ].join ','
|
89
|
+
end
|
90
|
+
|
87
91
|
def in_force?
|
88
92
|
not expired? and process_and_thread_still_exist?
|
89
93
|
end
|
data/lib/lock_method/version.rb
CHANGED
data/lib/lock_method.rb
CHANGED
@@ -13,13 +13,13 @@ module LockMethod
|
|
13
13
|
Config.instance
|
14
14
|
end
|
15
15
|
|
16
|
-
# All Objects, including instances and Classes, get the <tt>#
|
16
|
+
# All Objects, including instances and Classes, get the <tt>#clear_method_lock</tt> method.
|
17
17
|
module InstanceMethods
|
18
18
|
# Clear the lock for a particular method.
|
19
19
|
#
|
20
20
|
# Example:
|
21
|
-
# my_blog.
|
22
|
-
def
|
21
|
+
# my_blog.clear_method_lock :get_latest_entries
|
22
|
+
def clear_method_lock(method_id)
|
23
23
|
lock = ::LockMethod::Lock.new :obj => self, :method_id => method_id
|
24
24
|
lock.delete
|
25
25
|
end
|
data/test/shared_tests.rb
CHANGED
@@ -116,7 +116,7 @@ module SharedTests
|
|
116
116
|
end
|
117
117
|
|
118
118
|
# but now we clear the lock
|
119
|
-
new_instance_of_my_blog.
|
119
|
+
new_instance_of_my_blog.clear_method_lock :get_latest_entries
|
120
120
|
assert_nothing_raised do
|
121
121
|
new_instance_of_my_blog.get_latest_entries
|
122
122
|
end
|
@@ -136,7 +136,7 @@ module SharedTests
|
|
136
136
|
end
|
137
137
|
|
138
138
|
# but now we clear the lock
|
139
|
-
Blog2.
|
139
|
+
Blog2.clear_method_lock :get_latest_entries
|
140
140
|
assert_nothing_raised do
|
141
141
|
Blog2.get_latest_entries
|
142
142
|
end
|
metadata
CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 0.0.2
|
10
|
+
version: 0.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Seamus Abshere
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-02-
|
18
|
+
date: 2011-02-23 00:00:00 -06:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|