Class: Ramaze::Cache::LocalMemCache

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

Overview

Cache based on the localmemcache library which utilizes mmap to share strings in memory between ruby instances.

Constant Summary collapse

OPTIONS =
{
  :size_mb    => 1024,
  :serialize  => true,
  :serializer => ::Marshal, # something that responds to ::load and ::dump
}

Instance Method Summary collapse

Instance Method Details

#cache_clearObject

Wipe out all data in localmemcached, use with care.



33
34
35
# File 'lib/ramaze/cache/localmemcache.rb', line 33

def cache_clear
  @store.clear
end

#cache_delete(*args) ⇒ Object



37
38
39
# File 'lib/ramaze/cache/localmemcache.rb', line 37

def cache_delete(*args)
  super{|key| @store.delete(key.to_s); nil }
end

#cache_fetch(*args) ⇒ 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.


44
45
46
47
48
49
# File 'lib/ramaze/cache/localmemcache.rb', line 44

def cache_fetch(*args)
  super{|key|
    value = @store[key.to_s]
    @serializer.load(value) if value
  }
end

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

Connect to localmemcache



21
22
23
24
25
26
27
28
29
30
# File 'lib/ramaze/cache/localmemcache.rb', line 21

def cache_setup(host, user, app, name)
  @namespace = [host, user, app, name].compact.join('-')

  options = {:namespace => @namespace}.merge(OPTIONS)

  @serialize = options.delete(:serialize)
  @serializer = options.delete(:serializer)

  @store = ::LocalMemCache.new(options)
end

#cache_store(*args) ⇒ Object



51
52
53
# File 'lib/ramaze/cache/localmemcache.rb', line 51

def cache_store(*args)
  super{|key, value| @store[key.to_s] = @serializer.dump(value) }
end