Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 55 lines (46 sloc) 1.157 kb
e13e43a @jnunemaker Initial commit.
authored
1 require 'adapter'
2 require 'memcached'
3
4 module Adapter
5 module Memcached
6 def read(key)
7 decode(client.get(key_for(key)))
8 rescue ::Memcached::NotFound
9 end
10
11 def write(key, value)
12 client.set(key_for(key), encode(value))
13 end
14
15 def delete(key)
16 read(key).tap { client.delete(key_for(key)) }
17 rescue ::Memcached::NotFound
18 end
19
20 def clear
21 client.flush
22 end
23
24 def lock(name, options={}, &block)
ed8945e @jnunemaker Removed need for key packing.
authored
25 key = key_for(name)
e13e43a @jnunemaker Initial commit.
authored
26 start = Time.now
27 lock_acquired = false
28 expiration = options.fetch(:expiration, 1)
29 timeout = options.fetch(:timeout, 5)
30
31 while (Time.now - start) < timeout
32 begin
33 client.add(key, 'locked', expiration)
34 lock_acquired = true
35 break
36 rescue ::Memcached::NotStored
37 sleep 0.1
38 end
39 end
40
41 raise(Adapter::LockTimeout.new(name, timeout)) unless lock_acquired
42
43 begin
44 yield
45 ensure
ed8945e @jnunemaker Removed need for key packing.
authored
46 begin
47 client.delete(key)
48 rescue ::Memcached::NotFound
49 end
e13e43a @jnunemaker Initial commit.
authored
50 end
51 end
52 end
53 end
54
55 Adapter.define(:memcached, Adapter::Memcached)
Something went wrong with that request. Please try again.