lib/redis/objects/locks.rb in redis-objects-0.4.0 vs lib/redis/objects/locks.rb in redis-objects-0.4.1
- old
+ new
@@ -16,25 +16,26 @@
# so it can be used alongside ActiveRecord/DataMapper, etc.
def lock(name, options={})
options[:timeout] ||= 5 # seconds
lock_name = "#{name}_lock"
@redis_objects[lock_name.to_sym] = options.merge(:type => :lock)
+ klass_name = '::' + self.name
if options[:global]
instance_eval <<-EndMethods
def #{lock_name}(&block)
- @#{lock_name} ||= Redis::Lock.new(field_key(:#{lock_name}), redis, @redis_objects[:#{lock_name}])
+ @#{lock_name} ||= Redis::Lock.new(redis_field_key(:#{lock_name}), #{klass_name}.redis, #{klass_name}.redis_objects[:#{lock_name}])
end
EndMethods
class_eval <<-EndMethods
def #{lock_name}(&block)
self.class.#{lock_name}(block)
end
EndMethods
else
class_eval <<-EndMethods
def #{lock_name}(&block)
- @#{lock_name} ||= Redis::Lock.new(field_key(:#{lock_name}), redis, self.class.redis_objects[:#{lock_name}])
+ @#{lock_name} ||= Redis::Lock.new(redis_field_key(:#{lock_name}), #{klass_name}.redis, #{klass_name}.redis_objects[:#{lock_name}])
end
EndMethods
end
end
@@ -43,17 +44,17 @@
# that was specified when the lock was defined.
def obtain_lock(name, id, &block)
verify_lock_defined!(name)
raise ArgumentError, "Missing block to #{self.name}.obtain_lock" unless block_given?
lock_name = "#{name}_lock"
- Redis::Lock.new(field_key(lock_name, id), redis, @redis_objects[lock_name.to_sym]).lock(&block)
+ Redis::Lock.new(redis_field_key(lock_name, id), redis, @redis_objects[lock_name.to_sym]).lock(&block)
end
# Clear the lock. Use with care - usually only in an Admin page to clear
# stale locks (a stale lock should only happen if a server crashes.)
def clear_lock(name, id)
verify_lock_defined!(name)
- redis.del(field_key("#{name}_lock", id))
+ redis.del(redis_field_key("#{name}_lock", id))
end
private
def verify_lock_defined!(name)