Class: Ramaze::MemcachedCache

Inherits:
Object
  • Object
show all
Defined in:
lib/ramaze/cache/memcached.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(host = 'localhost', port = '11211', namespace = Global.runner) ⇒ MemcachedCache

Create a new MemcachedCache with host, port and a namespace that defaults to ‘ramaze’

For your own usage you should use another namespace.



17
18
19
20
# File 'lib/ramaze/cache/memcached.rb', line 17

def initialize(host = 'localhost', port = '11211', namespace = Global.runner)
  namespace = Digest::SHA1.hexdigest(namespace)[0..16]
  @cache = MemCache.new("#{host}:#{port}", :namespace => namespace, :multithread => true)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object

please read the documentation of memcache-client for further methods. also, it is highly recommended to install memcache-client_extensions for a bit of speedup and more functionality Some examples:

key

#=> get a key

key

value #=> set a key

delete(key) #=> delete key set_many :x => :y, :n => :m #=> set many key/value pairs get_hash :x, :y #=> return a hash with key/value pairs stats #=> get some statistics about usage namespace #=> get the current namespace namespace = ‘ramaze’ #=> set a different namespace (‘ramaze’ is default) flush_all #=> flush the whole cache (deleting all) compression #=> see if compression is true/false compression = false #=> turn off compression (it’s by default true)



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

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

Instance Method Details

#clearObject

Flush everything, dangerous!



67
68
69
# File 'lib/ramaze/cache/memcached.rb', line 67

def clear
  @cache.flush_all
end

#get_multi(*keys) ⇒ Object

out of some reason MemCache sometimes doesn’t respond to get_multi, have to investigate this further.

for now, i’ll just use the dumbed down version and ask it whether it implements this functionality or not.



52
53
54
55
56
57
58
# File 'lib/ramaze/cache/memcached.rb', line 52

def get_multi(*keys)
  if @cache.respond_to?(:get_multi)
    @cache.get_multi(*keys)
  else
    @cache.get_hash(*keys)
  end
end

#values_at(*keys) ⇒ Object

same as get_multi, but returns only the values (in order)



62
63
64
# File 'lib/ramaze/cache/memcached.rb', line 62

def values_at(*keys)
  get_multi(*keys).values_at(*keys)
end