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

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

Instance Method Summary (collapse)

Instance Method Details

- (Object) cache_clear

Wipe out all data in localmemcached, use with care.



30
31
32
# File 'lib/ramaze/cache/localmemcache.rb', line 30

def cache_clear
  @store.clear
end

- (Object) cache_delete(*args)



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

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

- (Object) cache_fetch(*args)

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.



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

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

- (Object) cache_setup(host, user, app, name)

Connect to localmemcache



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

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

- (Object) cache_store(*args)



48
49
50
# File 'lib/ramaze/cache/localmemcache.rb', line 48

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