lock_method 0.0.1 → 0.0.2
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/.gitignore +1 -0
- data/README.rdoc +1 -1
- data/lib/lock_method.rb +1 -5
- data/lib/lock_method/config.rb +11 -4
- data/lib/lock_method/default_storage_client.rb +47 -0
- data/lib/lock_method/lock.rb +4 -4
- data/lib/lock_method/version.rb +1 -1
- data/lock_method.gemspec +1 -0
- data/test/test_default_storage.rb +1 -1
- data/test/test_memcached_storage.rb +3 -2
- data/test/test_redis_storage.rb +3 -2
- metadata +24 -11
- data/lib/lock_method/storage.rb +0 -60
- data/lib/lock_method/storage/default_storage_client.rb +0 -47
data/.gitignore
CHANGED
data/README.rdoc
CHANGED
data/lib/lock_method.rb
CHANGED
@@ -2,8 +2,8 @@ require 'lock_method/version'
|
|
2
2
|
# See the README.rdoc for more info!
|
3
3
|
module LockMethod
|
4
4
|
autoload :Config, 'lock_method/config'
|
5
|
-
autoload :Storage, 'lock_method/storage'
|
6
5
|
autoload :Lock, 'lock_method/lock'
|
6
|
+
autoload :DefaultStorageClient, 'lock_method/default_storage_client'
|
7
7
|
|
8
8
|
# This is what gets raised when you try to run a locked method.
|
9
9
|
class Locked < ::StandardError
|
@@ -13,10 +13,6 @@ module LockMethod
|
|
13
13
|
Config.instance
|
14
14
|
end
|
15
15
|
|
16
|
-
def self.storage #:nodoc:
|
17
|
-
Storage.instance
|
18
|
-
end
|
19
|
-
|
20
16
|
# All Objects, including instances and Classes, get the <tt>#clear_lock</tt> method.
|
21
17
|
module InstanceMethods
|
22
18
|
# Clear the lock for a particular method.
|
data/lib/lock_method/config.rb
CHANGED
@@ -19,16 +19,23 @@ module LockMethod
|
|
19
19
|
# * memcache-storage[https://github.com/mperham/memcache-storage] (MemCache, the one commonly used by Rails)
|
20
20
|
#
|
21
21
|
# Supported Redis clients:
|
22
|
-
# * redis[https://github.com/ezmobius/redis-rb]
|
22
|
+
# * redis[https://github.com/ezmobius/redis-rb]
|
23
|
+
#
|
24
|
+
# Supports anything that works with the cache[https://github.com/seamusabshere/cache] gem.
|
23
25
|
#
|
24
26
|
# Example:
|
25
27
|
# LockMethod.config.storage = Memcached.new '127.0.0.1:11211'
|
26
|
-
def storage=(
|
27
|
-
|
28
|
+
def storage=(raw_client_or_nil)
|
29
|
+
if raw_client_or_nil.nil?
|
30
|
+
@storage = nil
|
31
|
+
else
|
32
|
+
require 'cache'
|
33
|
+
@storage = ::Cache.new raw_client_or_nil
|
34
|
+
end
|
28
35
|
end
|
29
36
|
|
30
37
|
def storage #:nodoc:
|
31
|
-
@storage ||=
|
38
|
+
@storage ||= DefaultStorageClient.instance
|
32
39
|
end
|
33
40
|
|
34
41
|
# TTL for method caches. Defaults to 24 hours.
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
require 'tmpdir'
|
3
|
+
require 'fileutils'
|
4
|
+
require 'thread'
|
5
|
+
module LockMethod
|
6
|
+
class DefaultStorageClient #:nodoc: all
|
7
|
+
include ::Singleton
|
8
|
+
def get(k)
|
9
|
+
return unless ::File.exist? path(k)
|
10
|
+
::Marshal.load ::File.read(path(k))
|
11
|
+
rescue ::Errno::ENOENT
|
12
|
+
end
|
13
|
+
|
14
|
+
def set(k, v, ttl)
|
15
|
+
semaphore.synchronize do
|
16
|
+
::File.open(path(k), ::File::RDWR|::File::CREAT) do |f|
|
17
|
+
f.flock ::File::LOCK_EX
|
18
|
+
f.write ::Marshal.dump(v)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def delete(k)
|
24
|
+
::FileUtils.rm_f path(k)
|
25
|
+
end
|
26
|
+
|
27
|
+
def flush
|
28
|
+
::FileUtils.rm_rf dir
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def semaphore
|
34
|
+
@semaphore ||= ::Mutex.new
|
35
|
+
end
|
36
|
+
|
37
|
+
def path(k)
|
38
|
+
::File.join dir, k
|
39
|
+
end
|
40
|
+
|
41
|
+
def dir
|
42
|
+
dir = ::File.expand_path(::File.join(::Dir.tmpdir, 'lock_method'))
|
43
|
+
::FileUtils.mkdir_p dir unless ::File.directory? dir
|
44
|
+
dir
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/lib/lock_method/lock.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
module LockMethod
|
2
|
-
class Lock
|
2
|
+
class Lock #:nodoc: all
|
3
3
|
class << self
|
4
4
|
def find(method_signature)
|
5
|
-
if hsh =
|
5
|
+
if hsh = Config.instance.storage.get(method_signature)
|
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
|
-
|
62
|
+
Config.instance.storage.delete method_signature
|
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
|
-
|
71
|
+
Config.instance.storage.set method_signature, to_hash, ttl
|
72
72
|
end
|
73
73
|
|
74
74
|
def to_hash
|
data/lib/lock_method/version.rb
CHANGED
data/lock_method.gemspec
CHANGED
@@ -18,6 +18,7 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
19
|
s.require_paths = ["lib"]
|
20
20
|
|
21
|
+
s.add_dependency 'cache'
|
21
22
|
s.add_development_dependency 'test-unit'
|
22
23
|
s.add_development_dependency 'memcached'
|
23
24
|
s.add_development_dependency 'redis'
|
@@ -4,8 +4,9 @@ require 'memcached'
|
|
4
4
|
|
5
5
|
class TestMemcachedStorage < Test::Unit::TestCase
|
6
6
|
def setup
|
7
|
-
|
8
|
-
|
7
|
+
my_cache = Memcached.new 'localhost:11211'
|
8
|
+
my_cache.flush
|
9
|
+
LockMethod.config.storage = my_cache
|
9
10
|
end
|
10
11
|
|
11
12
|
include SharedTests
|
data/test/test_redis_storage.rb
CHANGED
@@ -7,8 +7,9 @@ if ENV['REDIS_URL']
|
|
7
7
|
class TestRedisStorage < Test::Unit::TestCase
|
8
8
|
def setup
|
9
9
|
uri = URI.parse(ENV["REDIS_URL"])
|
10
|
-
|
11
|
-
|
10
|
+
my_cache = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
|
11
|
+
my_cache.flushdb
|
12
|
+
LockMethod.config.storage = my_cache
|
12
13
|
end
|
13
14
|
|
14
15
|
include SharedTests
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lock_method
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Seamus Abshere
|
@@ -15,11 +15,11 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-02-
|
18
|
+
date: 2011-02-17 00:00:00 -06:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
|
-
name:
|
22
|
+
name: cache
|
23
23
|
prerelease: false
|
24
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
25
|
none: false
|
@@ -30,10 +30,10 @@ dependencies:
|
|
30
30
|
segments:
|
31
31
|
- 0
|
32
32
|
version: "0"
|
33
|
-
type: :
|
33
|
+
type: :runtime
|
34
34
|
version_requirements: *id001
|
35
35
|
- !ruby/object:Gem::Dependency
|
36
|
-
name:
|
36
|
+
name: test-unit
|
37
37
|
prerelease: false
|
38
38
|
requirement: &id002 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
@@ -47,7 +47,7 @@ dependencies:
|
|
47
47
|
type: :development
|
48
48
|
version_requirements: *id002
|
49
49
|
- !ruby/object:Gem::Dependency
|
50
|
-
name:
|
50
|
+
name: memcached
|
51
51
|
prerelease: false
|
52
52
|
requirement: &id003 !ruby/object:Gem::Requirement
|
53
53
|
none: false
|
@@ -61,7 +61,7 @@ dependencies:
|
|
61
61
|
type: :development
|
62
62
|
version_requirements: *id003
|
63
63
|
- !ruby/object:Gem::Dependency
|
64
|
-
name:
|
64
|
+
name: redis
|
65
65
|
prerelease: false
|
66
66
|
requirement: &id004 !ruby/object:Gem::Requirement
|
67
67
|
none: false
|
@@ -74,6 +74,20 @@ dependencies:
|
|
74
74
|
version: "0"
|
75
75
|
type: :development
|
76
76
|
version_requirements: *id004
|
77
|
+
- !ruby/object:Gem::Dependency
|
78
|
+
name: ruby-debug
|
79
|
+
prerelease: false
|
80
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
hash: 3
|
86
|
+
segments:
|
87
|
+
- 0
|
88
|
+
version: "0"
|
89
|
+
type: :development
|
90
|
+
version_requirements: *id005
|
77
91
|
description: Like alias_method, but it's lock_method!
|
78
92
|
email:
|
79
93
|
- seamus@abshere.net
|
@@ -90,9 +104,8 @@ files:
|
|
90
104
|
- Rakefile
|
91
105
|
- lib/lock_method.rb
|
92
106
|
- lib/lock_method/config.rb
|
107
|
+
- lib/lock_method/default_storage_client.rb
|
93
108
|
- lib/lock_method/lock.rb
|
94
|
-
- lib/lock_method/storage.rb
|
95
|
-
- lib/lock_method/storage/default_storage_client.rb
|
96
109
|
- lib/lock_method/version.rb
|
97
110
|
- lock_method.gemspec
|
98
111
|
- test/helper.rb
|
data/lib/lock_method/storage.rb
DELETED
@@ -1,60 +0,0 @@
|
|
1
|
-
require 'singleton'
|
2
|
-
module LockMethod
|
3
|
-
# All storage requests go through a clearinghouse.
|
4
|
-
class Storage #:nodoc: all
|
5
|
-
autoload :DefaultStorageClient, 'lock_method/storage/default_storage_client'
|
6
|
-
|
7
|
-
include ::Singleton
|
8
|
-
|
9
|
-
def delete(k)
|
10
|
-
if defined?(::Memcached) and bare_storage.is_a?(::Memcached)
|
11
|
-
begin; bare_storage.delete(k); rescue ::Memcached::NotFound; nil; end
|
12
|
-
elsif defined?(::Redis) and bare_storage.is_a?(::Redis)
|
13
|
-
bare_storage.del k
|
14
|
-
else
|
15
|
-
bare_storage.delete k
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
def flush
|
20
|
-
bare_storage.send %w{ flush flush_all clear flushdb }.detect { |c| bare_storage.respond_to? c }
|
21
|
-
end
|
22
|
-
|
23
|
-
def get(k)
|
24
|
-
if defined?(::Memcached) and bare_storage.is_a?(::Memcached)
|
25
|
-
begin; bare_storage.get(k); rescue ::Memcached::NotFound; nil; end
|
26
|
-
elsif defined?(::Redis) and bare_storage.is_a?(::Redis)
|
27
|
-
if cached_v = bare_storage.get(k) and cached_v.is_a?(::String)
|
28
|
-
::Marshal.load cached_v
|
29
|
-
end
|
30
|
-
elsif bare_storage.respond_to?(:get)
|
31
|
-
bare_storage.get k
|
32
|
-
elsif bare_storage.respond_to?(:read)
|
33
|
-
bare_storage.read k
|
34
|
-
else
|
35
|
-
raise "Don't know how to work with #{bare_storage.inspect}"
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
def set(k, v, ttl)
|
40
|
-
ttl ||= ::LockMethod.config.default_ttl
|
41
|
-
if defined?(::Redis) and bare_storage.is_a?(::Redis)
|
42
|
-
bare_storage.setex k, ttl, ::Marshal.dump(v)
|
43
|
-
elsif bare_storage.respond_to?(:set)
|
44
|
-
bare_storage.set k, v, ttl
|
45
|
-
elsif bare_storage.respond_to?(:write)
|
46
|
-
if ttl == 0
|
47
|
-
bare_storage.write k, v # never expire
|
48
|
-
else
|
49
|
-
bare_storage.write k, v, :expires_in => ttl
|
50
|
-
end
|
51
|
-
else
|
52
|
-
raise "Don't know how to work with #{bare_storage.inspect}"
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
def bare_storage
|
57
|
-
Config.instance.storage
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
@@ -1,47 +0,0 @@
|
|
1
|
-
require 'tmpdir'
|
2
|
-
require 'fileutils'
|
3
|
-
require 'thread'
|
4
|
-
module LockMethod
|
5
|
-
class Storage
|
6
|
-
class DefaultStorageClient
|
7
|
-
def get(k)
|
8
|
-
return unless ::File.exist? path(k)
|
9
|
-
::Marshal.load ::File.read(path(k))
|
10
|
-
rescue ::Errno::ENOENT
|
11
|
-
end
|
12
|
-
|
13
|
-
def set(k, v, ttl)
|
14
|
-
semaphore.synchronize do
|
15
|
-
::File.open(path(k), ::File::RDWR|::File::CREAT) do |f|
|
16
|
-
f.flock ::File::LOCK_EX
|
17
|
-
f.write ::Marshal.dump(v)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def delete(k)
|
23
|
-
::FileUtils.rm_f path(k)
|
24
|
-
end
|
25
|
-
|
26
|
-
def flush
|
27
|
-
::FileUtils.rm_rf dir
|
28
|
-
end
|
29
|
-
|
30
|
-
private
|
31
|
-
|
32
|
-
def semaphore
|
33
|
-
@semaphore ||= ::Mutex.new
|
34
|
-
end
|
35
|
-
|
36
|
-
def path(k)
|
37
|
-
::File.join dir, k
|
38
|
-
end
|
39
|
-
|
40
|
-
def dir
|
41
|
-
dir = ::File.expand_path(::File.join(::Dir.tmpdir, 'lock_method'))
|
42
|
-
::FileUtils.mkdir_p dir unless ::File.directory? dir
|
43
|
-
dir
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|