Class: Merb::Cache::MemcacheStore

Inherits:
Object
  • Object
show all
Defined in:
lib/merb_cache_more/cache-store/memcache.rb

Defined Under Namespace

Classes: NotDefined, NotReady

Instance Method Summary collapse

Constructor Details

#initializeMemcacheStore

Provides the memcache cache store for merb_cache_more



4
5
6
7
# File 'lib/merb_cache_more/cache-store/memcache.rb', line 4

def initialize
  @config = Merb::Controller._cache.config
  prepare
end

Instance Method Details

#cache(_controller, key, from_now = nil, &block) ⇒ Object

Capture or restore the data in cache. If the cache entry expired or does not exist, the data are taken from the execution of the block, marshalled and stored in cache. Otherwise, the data are loaded from the cache and returned unmarshalled

Parameters

_controller<Merb::Controller>

The instance of the current controller

key<String>

The key identifying the cache entry

from_now<~minutes>

The number of minutes (from now) the cache should persist

&block

The template to be used or not

Additional information

When fetching data (the cache entry exists and has not expired) The data are loaded from the cache and returned unmarshalled. Otherwise: The controller is used to capture the rendered template (from the block). It uses the capture_#engine and concat_#engine methods to do so. The captured data are then marshalled and stored.



64
65
66
67
68
69
70
71
72
73
# File 'lib/merb_cache_more/cache-store/memcache.rb', line 64

def cache(_controller, key, from_now = nil, &block)
  _data = @memcache.get(key)
  if _data.nil?
    _expire = from_now ? from_now.minutes.from_now.to_i : 0
    _data = _controller.send(:capture, &block)
    @memcache.set(key, _data, _expire)
  end
  _controller.send(:concat, _data, block.binding)
  true
end

#cache_get(key) ⇒ Object

Fetch data from memcache using the specified key The entry is deleted if it has expired

Parameter

key<Sting>

The key identifying the cache entry

Returns

data<String, NilClass>

nil is returned whether the entry expired or was not found



99
100
101
102
103
# File 'lib/merb_cache_more/cache-store/memcache.rb', line 99

def cache_get(key)
  data = @memcache.get(key)
  Merb.logger.info("cache: #{data.nil? ? "miss" : "hit"} (#{key})")
  data
end

#cache_set(key, data, from_now = nil) ⇒ Object

Store data to memcache using the specified key

Parameters

key<Sting>

The key identifying the cache entry

data<String>

The data to be put in cache

from_now<~minutes>

The number of minutes (from now) the cache should persist



82
83
84
85
86
87
88
# File 'lib/merb_cache_more/cache-store/memcache.rb', line 82

def cache_set(key, data, from_now = nil)
  _expire = from_now ? from_now.minutes.from_now.to_i : 0
  @memcache.set(key, data, _expire)
  cache_start_tracking(key)
  Merb.logger.info("cache: set (#{key})")
  true
end

#cache_store_typeObject

Gives info on the current cache store

Returns

The type of the current cache store


147
148
149
# File 'lib/merb_cache_more/cache-store/memcache.rb', line 147

def cache_store_type
  "memcache"
end

#cached?(key) ⇒ Boolean

Checks whether a cache entry exists

Parameter

key<String>

The key identifying the cache entry

Returns

true if the cache entry exists, false otherwise

Returns:

  • (Boolean)


41
42
43
# File 'lib/merb_cache_more/cache-store/memcache.rb', line 41

def cached?(key)
  not @memcache.get(key).nil?
end

#expire(key) ⇒ Object

Expire the cache entry identified by the given key

Parameter

key<Sting>

The key identifying the cache entry



109
110
111
112
113
114
# File 'lib/merb_cache_more/cache-store/memcache.rb', line 109

def expire(key)
  @memcache.delete(key)
  cache_stop_tracking(key)
  Merb.logger.info("cache: expired (#{key})")
  true
end

#expire_allObject

Expire all the cache entries



136
137
138
139
140
141
# File 'lib/merb_cache_more/cache-store/memcache.rb', line 136

def expire_all
  @memcache.flush_all
  stop_tracking_keys
  Merb.logger.info("cache: expired all")
  true
end

#expire_match(key) ⇒ Object

Expire the cache entries matching the given key

Parameter

key<Sting>

The key matching the cache entries

Additional info

In memcache this requires to keep track of all keys (on by default).
If you don't need this, set :no_tracking => true in the config.


124
125
126
127
128
129
130
131
132
133
# File 'lib/merb_cache_more/cache-store/memcache.rb', line 124

def expire_match(key)
  if @tracking_key
    for _key in get_tracked_keys
      expire(_key) if /#{key}/ =~ _key
    end
  else
    Merb.logger.info("cache: expire_match is not supported with memcache (set :no_tracking => false in your config")
  end
  true
end

#prepareObject

This method is there to ensure minimal requirements are met (directories are accessible, table exists, connected to server, …)



23
24
25
26
27
28
29
30
31
32
# File 'lib/merb_cache_more/cache-store/memcache.rb', line 23

def prepare
  namespace = @config[:namespace] || 'merb_cache_more'
  host = @config[:host] || '127.0.0.1:11211'
  @memcache = MemCache.new(host, {:namespace => namespace})
  @tracking_key = "_#{namespace}_keys" unless @config[:no_tracking]
  raise NotReady unless @memcache.active?
  true
rescue NameError
  raise NotDefined
end