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
# 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 || "simple_cache", :compress => true)
end

Instance Method Details

#clearObject



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

def clear
  @dc.flush_all
end

#fetch(key, &block) ⇒ Object



12
13
14
15
16
17
# File 'lib/simple_cache/memcached_store.rb', line 12

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



19
20
21
22
# File 'lib/simple_cache/memcached_store.rb', line 19

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