lock_method 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -6,7 +6,7 @@ Lets you lock a method so that only one process can call it at a time. Defaults
6
6
 
7
7
  == Real-world usage
8
8
 
9
- In production use at {carbon.brighterplanet.com}[http://carbon.brighterplanet.com] and {data.brighterplanet.com}[http://data.brighterplanet.com].
9
+ In production use at {impact.brighterplanet.com}[http://impact.brighterplanet.com] and {data.brighterplanet.com}[http://data.brighterplanet.com].
10
10
 
11
11
  == Example
12
12
 
@@ -20,8 +20,8 @@ In production use at {carbon.brighterplanet.com}[http://carbon.brighterplanet.co
20
20
  lock_method :get_latest_entries
21
21
 
22
22
  # used by lock_method to differentiate between instances
23
- def hash
24
- url.hash
23
+ def as_lock
24
+ url
25
25
  end
26
26
  end
27
27
 
@@ -32,25 +32,21 @@ Then you can do
32
32
 
33
33
  Just in case, you can clear them
34
34
 
35
- my_blog.clear_method_lock :get_latest_entries
35
+ my_blog.lock_method_clear :get_latest_entries
36
36
 
37
37
  == Pays attention to method arguments
38
38
 
39
39
  If you lock Foo.bar(*args), calling Foo.bar(:baz) will not lock out Foo.bar(:zoo).
40
40
 
41
- == Defining #hash
41
+ == Defining #as_lock
42
42
 
43
- If you want to lock instance methods, you should define <tt>#hash</tt> on those instances.
43
+ If you want to lock instance methods, you should define <tt>#as_lock</tt> on those instances.
44
44
 
45
- You should follow Ruby convention and have <tt>#hash</tt> return a <tt>Fixnum</tt>.
45
+ == Locking across hosts
46
46
 
47
- Ideally, you should try to make a <tt>String</tt> or a <tt>Hash</tt> and call the standard <tt>#hash</tt> on that.
47
+ If you want to lock across hosts, just use shared storage, like a remote Redis or memcached instance.
48
48
 
49
- Note: this is NOT the same thing as <tt>#to_hash</tt>! That returns a <tt>Hash</tt>... but what we want is an integer "hash code."
50
-
51
- == Defining #method_lock_hash instead of #hash
52
-
53
- If you don't want to modify #hash, you can use #method_lock_hash instead.
49
+ If you want to lock locally, but you're using shared storage, just get the hostname of the locking instance into the <tt>as_lock</tt>.
54
50
 
55
51
  == Configuration (and supported cache clients)
56
52
 
data/lib/lock_method.rb CHANGED
@@ -20,13 +20,13 @@ module LockMethod
20
20
  Config.instance
21
21
  end
22
22
 
23
- # All Objects, including instances and Classes, get the <tt>#clear_method_lock</tt> method.
23
+ # All Objects, including instances and Classes, get the <tt>#lock_method_clear</tt> method.
24
24
  module InstanceMethods
25
25
  # Clear the lock for a particular method.
26
26
  #
27
27
  # Example:
28
- # my_blog.clear_method_lock :get_latest_entries
29
- def clear_method_lock(method_id)
28
+ # my_blog.lock_method_clear :get_latest_entries
29
+ def lock_method_clear(method_id)
30
30
  lock = ::LockMethod::Lock.new self, method_id
31
31
  lock.delete
32
32
  end
@@ -1,4 +1,4 @@
1
- require 'digest/md5'
1
+ require 'digest/sha1'
2
2
  module LockMethod
3
3
  class Lock #:nodoc: all
4
4
  class << self
@@ -41,12 +41,12 @@ module LockMethod
41
41
  @ttl ||= Config.instance.default_ttl
42
42
  end
43
43
 
44
- def obj_hash
45
- @obj_hash ||= obj.respond_to?(:method_lock_hash) ? obj.method_lock_hash : obj.hash
44
+ def obj_digest
45
+ @obj_digest ||= ::Digest::SHA1.hexdigest(::Marshal.dump(obj.respond_to?(:as_lock) ? obj.as_lock : obj))
46
46
  end
47
47
 
48
48
  def args_digest
49
- @args_digest ||= args.to_a.empty? ? 'empty' : ::Digest::MD5.hexdigest(args.join)
49
+ @args_digest ||= args.to_a.empty? ? 'empty' : ::Digest::SHA1.hexdigest(::Marshal.dump(args))
50
50
  end
51
51
 
52
52
  def delete
@@ -65,7 +65,7 @@ module LockMethod
65
65
  if obj.is_a?(::Class) or obj.is_a?(::Module)
66
66
  [ 'LockMethod', 'Lock', method_signature, args_digest ].join ','
67
67
  else
68
- [ 'LockMethod', 'Lock', method_signature, obj_hash, args_digest ].join ','
68
+ [ 'LockMethod', 'Lock', method_signature, obj_digest, args_digest ].join ','
69
69
  end
70
70
  end
71
71
 
@@ -1,3 +1,3 @@
1
1
  module LockMethod
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
data/test/helper.rb CHANGED
@@ -25,17 +25,17 @@ class Blog1
25
25
  ["voo vaa #{name}"]
26
26
  end
27
27
  lock_method :get_latest_entries2, 5 # second
28
- def hash
29
- raise "Used hash"
28
+ def marshal_dump
29
+ raise "Used marshal_dump"
30
30
  end
31
- def method_lock_hash
32
- name.hash
31
+ def as_lock
32
+ name
33
33
  end
34
34
  end
35
35
 
36
36
  class Blog1a < Blog1
37
- def method_lock_hash
38
- raise "Used method_lock_hash"
37
+ def as_lock
38
+ raise "Used as_lock"
39
39
  end
40
40
  end
41
41
 
data/test/shared_tests.rb CHANGED
@@ -114,7 +114,7 @@ module SharedTests
114
114
  end
115
115
 
116
116
  # but now we clear the lock
117
- new_instance_of_my_blog.clear_method_lock :get_latest_entries
117
+ new_instance_of_my_blog.lock_method_clear :get_latest_entries
118
118
  assert_nothing_raised do
119
119
  new_instance_of_my_blog.get_latest_entries
120
120
  end
@@ -134,7 +134,7 @@ module SharedTests
134
134
  end
135
135
 
136
136
  # but now we clear the lock
137
- Blog2.clear_method_lock :get_latest_entries
137
+ Blog2.lock_method_clear :get_latest_entries
138
138
  assert_nothing_raised do
139
139
  Blog2.get_latest_entries
140
140
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lock_method
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-12 00:00:00.000000000Z
12
+ date: 2012-03-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: cache
16
- requirement: &2153961100 !ruby/object:Gem::Requirement
16
+ requirement: &2152246540 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 0.2.1
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2153961100
24
+ version_requirements: *2152246540
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activesupport
27
- requirement: &2153960680 !ruby/object:Gem::Requirement
27
+ requirement: &2152240680 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2153960680
35
+ version_requirements: *2152240680
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: test-unit
38
- requirement: &2153960220 !ruby/object:Gem::Requirement
38
+ requirement: &2160094560 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2153960220
46
+ version_requirements: *2160094560
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: memcached
49
- requirement: &2153959800 !ruby/object:Gem::Requirement
49
+ requirement: &2160090340 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2153959800
57
+ version_requirements: *2160090340
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: redis
60
- requirement: &2153959380 !ruby/object:Gem::Requirement
60
+ requirement: &2160103240 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *2153959380
68
+ version_requirements: *2160103240
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: ruby-debug19
71
- requirement: &2153958940 !ruby/object:Gem::Requirement
71
+ requirement: &2160101140 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,7 +76,7 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *2153958940
79
+ version_requirements: *2160101140
80
80
  description: Like alias_method, but it's lock_method!
81
81
  email:
82
82
  - seamus@abshere.net
@@ -119,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
119
  version: '0'
120
120
  requirements: []
121
121
  rubyforge_project: lock_method
122
- rubygems_version: 1.8.10
122
+ rubygems_version: 1.8.15
123
123
  signing_key:
124
124
  specification_version: 3
125
125
  summary: Lets you lock methods (to memcached, redis, etc.) as though you had a lockfile
@@ -130,3 +130,4 @@ test_files:
130
130
  - test/test_default_storage.rb
131
131
  - test/test_memcached_storage.rb
132
132
  - test/test_redis_storage.rb
133
+ has_rdoc: