Class: Ramaze::Cache::MemCache

Inherits:
Object
  • Object
show all
Includes:
Cache::API
Defined in:
lib/ramaze/cache/memcache.rb

Overview

Cache based on the memcache library which utilizes the memcache-daemon to store key/value pairs in namespaces.

Please read the documentation of memcache-client for further methods.

It is highly recommended to install memcache-client_extensions for a bit of speedup and more functionality

Constant Summary collapse

OPTIONS =

:multithread: May be turned off at your own risk.

+:readonly+: You most likely want that to be false.
 +:servers+: Array containing at least one of:
             MemCache::Server instance
             Strings like "localhost", "localhost:11211", "localhost:11211:1"
             That accord to "host:port:weight", only host is required.
{
  :multithread => true,
  :readonly    => false,
  :servers     => ['localhost:11211:1'],
}

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object

For everything else that we don’t care to document right now.



91
92
93
94
95
96
# File 'lib/ramaze/cache/memcache.rb', line 91

def method_missing(*args, &block)
  @store.__send__(*args, &block)
rescue ::MemCache::MemCacheError => e
  Log.error(e)
  nil
end

Instance Method Details

#cache_clearObject

Wipe out all data in memcached, use with care.



40
41
42
43
44
45
# File 'lib/ramaze/cache/memcache.rb', line 40

def cache_clear
  @store.flush_all
rescue ::MemCache::MemCacheError => e
  Log.error(e)
  nil
end

#cache_delete(*keys) ⇒ Object



48
49
50
51
52
53
# File 'lib/ramaze/cache/memcache.rb', line 48

def cache_delete(*keys)
  super{|key| @store.delete(key) }
rescue ::MemCache::MemCacheError => e
  Log.error(e)
  nil
end

#cache_fetch(key, default = nil) ⇒ Object

NOTE:

* We have no way of knowing whether the value really is nil, we
  assume you wouldn't cache nil and return the default instead.


58
59
60
61
62
63
64
# File 'lib/ramaze/cache/memcache.rb', line 58

def cache_fetch(key, default = nil)
  value = @store[key]
  value.nil? ? default : value
rescue ::MemCache::MemCacheError => e
  Log.error(e)
  nil
end

#cache_setup(host, user, app, name) ⇒ Object

Connect to memcached



32
33
34
35
36
37
# File 'lib/ramaze/cache/memcache.rb', line 32

def cache_setup(host, user, app, name)
  @namespace = [host, user, app, name].compact.join('-')
  options = {:namespace => @namespace}.merge(OPTIONS)
  servers = options.delete(:servers)
  @store = ::MemCache.new(servers, options)
end

#cache_store(key, value, options = {}) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/ramaze/cache/memcache.rb', line 66

def cache_store(key, value, options = {})
  ttl = options[:ttl] || 0
  @store.set(key, value, ttl)
  value
rescue ::MemCache::MemCacheError => e
  Log.error(e)
  nil
end

#compressionObject

state of compression (true/false)



85
# File 'lib/ramaze/cache/memcache.rb', line 85

def compression; @store.compression; end

#compression=(bool) ⇒ Object

turn compression on or off



88
# File 'lib/ramaze/cache/memcache.rb', line 88

def compression=(bool); @store.compression = bool; end

#namespaceObject

current namespace



79
# File 'lib/ramaze/cache/memcache.rb', line 79

def namespace; @store.namespace; end

#namespace=(ns) ⇒ Object

switch to different namespace



82
# File 'lib/ramaze/cache/memcache.rb', line 82

def namespace=(ns) @namespace = @store.namespace = ns; end

#statsObject

statistics about usage



76
# File 'lib/ramaze/cache/memcache.rb', line 76

def stats; @store.stats; end