lib/asynchronic/data_store/redis.rb in asynchronic-1.2.0 vs lib/asynchronic/data_store/redis.rb in asynchronic-1.2.1
- old
+ new
@@ -1,9 +1,12 @@
module Asynchronic
module DataStore
class Redis
+ TIMEOUT = 0.001
+ LOCKED = 'locked'
+
include Helper
def initialize(scope, *args)
@scope = Key.new scope
@connection = ::Redis.new *args
@@ -25,9 +28,18 @@
@connection.del @scope[key]
end
def keys
@connection.keys(@scope['*']).map { |k| Key.new(k).remove_first }
+ end
+
+ def synchronize(key)
+ while @connection.getset(@scope[key][LOCKED], LOCKED) == LOCKED
+ sleep TIMEOUT
+ end
+ yield
+ ensure
+ @connection.del @scope[key][LOCKED]
end
def connection_args
[@scope, @connection.client.options]
end
\ No newline at end of file