Module: Adapter::Memcached

Defined in:
lib/adapter/memcached.rb,
lib/adapter/memcached/version.rb

Constant Summary collapse

VERSION =
"0.5.2"

Instance Method Summary collapse

Instance Method Details

#clearObject



20
21
22
# File 'lib/adapter/memcached.rb', line 20

def clear
  client.flush
end

#delete(key) ⇒ Object



15
16
17
18
# File 'lib/adapter/memcached.rb', line 15

def delete(key)
  read(key).tap { client.delete(key_for(key)) }
rescue ::Memcached::NotFound
end

#lock(name, options = {}, &block) ⇒ Object

Raises:

  • (Adapter::LockTimeout)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/adapter/memcached.rb', line 24

def lock(name, options={}, &block)
  key           = key_for(name)
  start         = Time.now
  lock_acquired = false
  expiration    = options.fetch(:expiration, 1)
  timeout       = options.fetch(:timeout, 5)

  while (Time.now - start) < timeout
    begin
      client.add(key, 'locked', expiration)
      lock_acquired = true
      break
    rescue ::Memcached::NotStored
      sleep 0.1
    end
  end

  raise(Adapter::LockTimeout.new(name, timeout)) unless lock_acquired

  begin
    yield
  ensure
    begin
      client.delete(key)
    rescue ::Memcached::NotFound
    end
  end
end

#read(key) ⇒ Object



6
7
8
9
# File 'lib/adapter/memcached.rb', line 6

def read(key)
  decode(client.get(key_for(key)))
rescue ::Memcached::NotFound
end

#write(key, value) ⇒ Object



11
12
13
# File 'lib/adapter/memcached.rb', line 11

def write(key, value)
  client.set(key_for(key), encode(value))
end