Sha256: 41c154f8bec380a9b8f060720eb07b165f32ff5004d1dd99a3ad70513f1d523c

Contents?: true

Size: 587 Bytes

Versions: 5

Compression:

Stored size: 587 Bytes

Contents

module Massive
  module Locking
    def locked?(key, expire_in=60 * 1000)
      lock_key = lock_key_for(key)

      !redis.setnx(lock_key, Time.now.to_i + (expire_in)/1000).tap do |result|
        expire(lock_key, expire_in) if result
      end
    end

    protected

    def lock_key_for(key)
      "#{self.class.name.underscore}:#{id}:#{key}"
    end

    def expire(lock_key, expire_in)
      redis.pexpire(lock_key, expire_in)
    rescue Redis::CommandError
      redis.expire(lock_key, (expire_in/1000).to_i)
    end

    def redis
      @redis ||= Massive.redis
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
massive-0.4.0 lib/massive/locking.rb
massive-0.3.0 lib/massive/locking.rb
massive-0.2.0 lib/massive/locking.rb
massive-0.1.1 lib/massive/locking.rb
massive-0.1.0 lib/massive/locking.rb