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!



65
66
67
# File 'lib/ramaze/cache/memcached.rb', line 65

def clear
  @cache.flush_all
end

#get_multi(*keys) ⇒ Object

Fetch for multiple keys at once.



48
49
50
51
52
53
54
55
56
# File 'lib/ramaze/cache/memcached.rb', line 48

def get_multi(*keys)
  # MemCache only responds to get_multi if the memcache-client_extensions gem
  # is installed, so we check first and resort to another approach if not.
  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)



60
61
62
# File 'lib/ramaze/cache/memcached.rb', line 60

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