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

NOTE: There is a big issue with persisting sessions in memcache, not only

can they be dropped at any time, essentially logging the user out
without them noticing, but there is also a low limit to the maximum
time-to-live. After 30 days, your session will be dropped, no
matter what.
Please remember that memcache is, first of all, a cache, not a
persistence mechanism.

NOTE: If you try to set a higher ttl than allowed, your stored key/value

will be expired immediately.

Constant Summary collapse

MAX_TTL =
2592000
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.



116
117
118
119
120
121
# File 'lib/ramaze/cache/memcache.rb', line 116

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.



54
55
56
57
58
59
# File 'lib/ramaze/cache/memcache.rb', line 54

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

#cache_delete(*keys) ⇒ Object



62
63
64
65
66
67
# File 'lib/ramaze/cache/memcache.rb', line 62

def cache_delete(*keys)
  super{|key| @store.delete(key); nil }
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.


72
73
74
75
76
77
78
# File 'lib/ramaze/cache/memcache.rb', line 72

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



45
46
47
48
49
50
51
# File 'lib/ramaze/cache/memcache.rb', line 45

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)
  @warned = false
end

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ramaze/cache/memcache.rb', line 80

def cache_store(key, value, options = {})
  ttl = options[:ttl] || 0

  if ttl > MAX_TTL
    unless @warned
      Log.warn('MemCache cannot set a ttl greater than 2592000 seconds.')
      Log.warn('Modify Ramaze.options.session.ttl to a value <= of that.')
      @warned = true
    end

    ttl = MAX_TTL
  end

  @store.set(key, value, ttl)
  value
rescue ::MemCache::MemCacheError => e
  Log.error(e)
  nil
end

#compressionObject

state of compression (true/false)



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

def compression; @store.compression; end

#compression=(bool) ⇒ Object

turn compression on or off



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

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

#namespaceObject

current namespace



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

def namespace; @store.namespace; end

#namespace=(ns) ⇒ Object

switch to different namespace



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

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

#statsObject

statistics about usage



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

def stats; @store.stats; end