Class: SimpleCache::MemcachedStore

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_cache/memcached_store.rb

Constant Summary collapse

Marshal =
::SimpleCache::Marshal

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ MemcachedStore

Returns a new instance of MemcachedStore.



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/simple_cache/memcached_store.rb', line 4

def initialize(url)
  require "dalli"

  uri = URI.parse(url)

  @dc = Dalli::Client.new "#{uri.host}:#{uri.port}", 
    :namespace => (uri.path == "/" || uri.path == "" ? "simple_cache" : uri.path), 
    :compress => true,
    :username => uri.user,
    :password => uri.password
end

Instance Method Details

#clearObject



28
29
30
# File 'lib/simple_cache/memcached_store.rb', line 28

def clear
  @dc.flush_all
end

#fetch(key, &block) ⇒ Object



16
17
18
19
20
21
# File 'lib/simple_cache/memcached_store.rb', line 16

def fetch(key, &block)
  value = @dc.get(key)
  return Marshal.unmarshal(value) if value
  return yield(self, key) if block
  nil
end

#store(key, value, ttl = nil) ⇒ Object



23
24
25
26
# File 'lib/simple_cache/memcached_store.rb', line 23

def store(key, value, ttl = nil)
  @dc.set(key, Marshal.marshal(value), ttl)
  value
end