Class: Morlock

Inherits:
Object
  • Object
show all
Defined in:
lib/morlock/base.rb,
lib/morlock/rails.rb,
lib/morlock/version.rb,
lib/morlock/gem_client.rb

Defined Under Namespace

Classes: DalliGemClient, GemClient, MemcacheGemClient, MorlockRailtie, UnknownGemClient

Constant Summary collapse

DEFAULT_EXPIRATION =
60
VERSION =
"0.0.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Morlock

Returns a new instance of Morlock.



8
9
10
# File 'lib/morlock/base.rb', line 8

def initialize(client)
  @client = Morlock::GemClient.wrap(client)
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



6
7
8
# File 'lib/morlock/base.rb', line 6

def client
  @client
end

Instance Method Details

#lock(key, options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/morlock/base.rb', line 12

def lock(key, options = {})
  lock_obtained = @client.add(key, options[:expiration] || DEFAULT_EXPIRATION)
  yield if lock_obtained && block_given?
  options[:success].call if lock_obtained && options[:success]
  options[:failure].call if !lock_obtained && options[:failure]
  lock_obtained
ensure
  @client.delete(key) if lock_obtained
end