class Circuitry::Locks::Memory

Public Class Methods

semaphore() click to toggle source
# File lib/circuitry/locks/memory.rb, line 11
def semaphore
  @semaphore ||= Mutex.new
end
store() click to toggle source
# File lib/circuitry/locks/memory.rb, line 7
def store
  @store ||= {}
end

Protected Instance Methods

lock(key, ttl) click to toggle source
# File lib/circuitry/locks/memory.rb, line 18
def lock(key, ttl)
  reap

  store do |store|
    if store.key?(key)
      false
    else
      store[key] = Time.now + ttl
      true
    end
  end
end
lock!(key, ttl) click to toggle source
# File lib/circuitry/locks/memory.rb, line 31
def lock!(key, ttl)
  reap

  store do |store|
    store[key] = Time.now + ttl
  end
end
unlock!(key) click to toggle source
# File lib/circuitry/locks/memory.rb, line 39
def unlock!(key)
  store do |store|
    store.delete(key)
  end
end

Private Instance Methods

reap() click to toggle source
# File lib/circuitry/locks/memory.rb, line 53
def reap
  store do |store|
    now = Time.now
    store.delete_if { |_, expires_at| expires_at <= now }
  end
end
semaphore() click to toggle source
# File lib/circuitry/locks/memory.rb, line 60
def semaphore
  self.class.semaphore
end
store() { |class.store| ... } click to toggle source
# File lib/circuitry/locks/memory.rb, line 47
def store
  semaphore.synchronize do
    yield self.class.store
  end
end