Class: Tripod::CacheStores::MemcachedCacheStore

Inherits:
Object
  • Object
show all
Defined in:
lib/tripod/cache_stores/memcached_cache_store.rb

Overview

A Tripod::CacheStore that uses Memcached. Note: Make sure you set the memcached -I (slab size) to big enough to store each result, and set the -m (total size) to something quite big (or the cache will recycle too often).

Instance Method Summary collapse

Constructor Details

#initialize(location, size = 1) ⇒ MemcachedCacheStore

initialize a memcached cache store at the specified port (default ‘localhost:11211’) a pool size should also be specified (defaults to 1 for development/local use reasons)



14
15
16
# File 'lib/tripod/cache_stores/memcached_cache_store.rb', line 14

def initialize(location, size = 1)
  @dalli_pool = ConnectionPool.new(:size => size, :timeout => 3) { Dalli::Client.new(location, :value_max_bytes => Tripod.response_limit_bytes) }
end

Instance Method Details

#clear!Object



53
54
55
56
57
# File 'lib/tripod/cache_stores/memcached_cache_store.rb', line 53

def clear!
  @dalli_pool.with do |client|
    client.flush
  end
end

#exist?(key) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
# File 'lib/tripod/cache_stores/memcached_cache_store.rb', line 27

def exist?(key)
  @dalli_pool.with do |client|
    !!client.get(key)
  end
end

#fetch(key) ⇒ Object

 takes a block

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
# File 'lib/tripod/cache_stores/memcached_cache_store.rb', line 19

def fetch(key)
  raise ArgumentError.new("expected a block") unless block_given?

  @dalli_pool.with do |client|
    client.fetch(key) { yield }
  end
end

#read(key) ⇒ Object



39
40
41
42
43
# File 'lib/tripod/cache_stores/memcached_cache_store.rb', line 39

def read(key)
  @dalli_pool.with do |client|
    client.get(key)
  end
end

#statsObject



45
46
47
48
49
50
51
# File 'lib/tripod/cache_stores/memcached_cache_store.rb', line 45

def stats
  output = []
  @dalli_pool.with do |client|
    output << client.stats
  end
  output
end

#write(key, data) ⇒ Object



33
34
35
36
37
# File 'lib/tripod/cache_stores/memcached_cache_store.rb', line 33

def write(key, data)
  @dalli_pool.with do |client|
    client.set(key, data)
  end
end