redpear 0.9.0 → 0.9.1

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.
@@ -24,7 +24,9 @@ class Redpear::Store::Lock < Redpear::Store::Base
24
24
  conn.get key
25
25
  end
26
26
 
27
- # Creates a lock and yields a transaction. Example:
27
+ # Creates a lock and yields a transaction.
28
+ #
29
+ # @example
28
30
  #
29
31
  # sender = Redpear::Store::Hash.new "accounts:sender", connection
30
32
  # recipient = Redpear::Store::Hash.new "accounts:recipient", connection
@@ -41,6 +43,7 @@ class Redpear::Store::Lock < Redpear::Store::Base
41
43
  # @option [Integer] wait_timeout
42
44
  # Wait for `wait_timeout` seconds to obtain a lock, before timing out. Defaults to 2.
43
45
  # @yield [] processes the block within the lock
46
+ # @raise [Redpear::Store::Lock::LockTimeout] timeout error if lock cannot be ontained
44
47
  def lock(options = {})
45
48
  options = self.class.default_options.merge(options)
46
49
  result = nil
@@ -67,6 +70,52 @@ class Redpear::Store::Lock < Redpear::Store::Base
67
70
  purge! if timestamp && timestamp > Time.now.to_f
68
71
  end
69
72
 
73
+ # Conditional locking. Performs a transaction if lock can be acquired.
74
+ # @param [Hash] options - see #lock
75
+ # @yield [] processes the block within the lock
76
+ # @return [Boolean] true if successful
77
+ def lock?(options = {}, &block)
78
+ lock(options, &block)
79
+ true
80
+ rescue LockTimeout
81
+ false
82
+ end
83
+
84
+ # Reserves a lock and yields a transaction, but only if not reserved by
85
+ # someone else. This is useful when processes can be triggered by concurrent
86
+ # threads, but should only be executed once.
87
+ #
88
+ # @example Calling `execute_once` method only once
89
+ #
90
+ # def execute_once
91
+ # # Do something
92
+ # end
93
+ #
94
+ # t1 = Thread.new do
95
+ # lock = Redpear::Store::Lock.new "locks:reservation", connection
96
+ # lock.reserve(60) { execute_once } # Reserve for max. 60 seconds
97
+ # end
98
+ #
99
+ # t2 = Thread.new do
100
+ # lock = Redpear::Store::Lock.new "locks:reservation", connection
101
+ # lock.reserve(60) { execute_once }
102
+ # end
103
+ #
104
+ # t1.join
105
+ # t2.join
106
+ #
107
+ # @param [Integer] seconds the number of seconds
108
+ # @param [Hash] options
109
+ # @option [Boolean] options :clear
110
+ # Clear the lock after execution, defaults to false
111
+ # @yield [] processes the block within the lock
112
+ def reserve(seconds, options = {})
113
+ timestamp = to_time(seconds).to_f
114
+ yield if lock_obtained?(timestamp) || expired_lock_obtained?(timestamp)
115
+ ensure
116
+ purge! if options[:clear]
117
+ end
118
+
70
119
  protected
71
120
 
72
121
  # @param [Float] timestamp
@@ -2,7 +2,7 @@ module Redpear
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 9
5
- TINY = 0
5
+ TINY = 1
6
6
  PRE = nil
7
7
 
8
8
  STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redpear
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-07 00:00:00.000000000 Z
12
+ date: 2012-07-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: redis
@@ -129,30 +129,30 @@ executables: []
129
129
  extensions: []
130
130
  extra_rdoc_files: []
131
131
  files:
132
+ - lib/redpear.rb
132
133
  - lib/redpear/connection.rb
133
- - lib/redpear/schema.rb
134
- - lib/redpear/store/lock.rb
135
- - lib/redpear/store/set.rb
136
- - lib/redpear/store/base.rb
137
- - lib/redpear/store/value.rb
138
- - lib/redpear/store/sorted_set.rb
139
- - lib/redpear/store/enumerable.rb
140
- - lib/redpear/store/hash.rb
141
- - lib/redpear/store/counter.rb
142
- - lib/redpear/store/list.rb
143
- - lib/redpear/concern.rb
144
- - lib/redpear/store.rb
145
- - lib/redpear/model.rb
146
134
  - lib/redpear/schema/column.rb
147
- - lib/redpear/schema/score.rb
148
135
  - lib/redpear/schema/index.rb
149
136
  - lib/redpear/schema/collection.rb
137
+ - lib/redpear/schema/score.rb
138
+ - lib/redpear/concern.rb
139
+ - lib/redpear/store.rb
150
140
  - lib/redpear/model/factory_girl.rb
151
141
  - lib/redpear/model/finders.rb
152
- - lib/redpear/model/machinist.rb
153
142
  - lib/redpear/model/expiration.rb
143
+ - lib/redpear/model/machinist.rb
154
144
  - lib/redpear/version.rb
155
- - lib/redpear.rb
145
+ - lib/redpear/store/lock.rb
146
+ - lib/redpear/store/enumerable.rb
147
+ - lib/redpear/store/counter.rb
148
+ - lib/redpear/store/hash.rb
149
+ - lib/redpear/store/base.rb
150
+ - lib/redpear/store/value.rb
151
+ - lib/redpear/store/list.rb
152
+ - lib/redpear/store/set.rb
153
+ - lib/redpear/store/sorted_set.rb
154
+ - lib/redpear/model.rb
155
+ - lib/redpear/schema.rb
156
156
  homepage: https://github.com/bsm/redpear
157
157
  licenses: []
158
158
  post_install_message: