Class: Merb::Cache::MemcacheStore
- Inherits:
-
Object
- Object
- Merb::Cache::MemcacheStore
- Defined in:
- lib/merb-cache/cache-store/memcache.rb
Defined Under Namespace
Classes: NotDefined, NotReady
Instance Method Summary collapse
-
#cache(_controller, key, from_now = nil, &block) ⇒ Object
Capture or restore the data in cache.
-
#cache_get(key) ⇒ Object
Fetch data from memcache using the specified key The entry is deleted if it has expired.
-
#cache_set(key, data, from_now = nil) ⇒ Object
Store data to memcache using the specified key.
-
#cache_store_type ⇒ Object
Gives info on the current cache store.
-
#cached?(key) ⇒ Boolean
Checks whether a cache entry exists.
-
#expire(key) ⇒ Object
Expire the cache entry identified by the given key.
-
#expire_all ⇒ Object
Expire all the cache entries.
-
#expire_match(key) ⇒ Object
Expire the cache entries matching the given key.
-
#initialize ⇒ MemcacheStore
constructor
Provides the memcache cache store for merb-cache.
-
#prepare ⇒ Object
This method is there to ensure minimal requirements are met (directories are accessible, table exists, connected to server, …).
Constructor Details
#initialize ⇒ MemcacheStore
Provides the memcache cache store for merb-cache
4 5 6 7 |
# File 'lib/merb-cache/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 |
# File 'lib/merb-cache/cache-store/memcache.rb', line 64 def cache(_controller, key, from_now = nil, &block) _data = cache_get(key) if _data.nil? _data = _controller.send(:capture, &block) cache_set(key, _data, from_now) 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
98 99 100 101 102 103 104 105 106 |
# File 'lib/merb-cache/cache-store/memcache.rb', line 98 def cache_get(key) data = @memcache.get(key) if data.nil? Merb.logger.info!("cache: miss (#{key})") else Merb.logger.debug!("cache: hit (#{key})") end 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
81 82 83 84 85 86 87 |
# File 'lib/merb-cache/cache-store/memcache.rb', line 81 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_type ⇒ Object
Gives info on the current cache store
Returns
The type of the current cache store
151 152 153 |
# File 'lib/merb-cache/cache-store/memcache.rb', line 151 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
41 42 43 |
# File 'lib/merb-cache/cache-store/memcache.rb', line 41 def cached?(key) not cache_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
112 113 114 115 116 117 |
# File 'lib/merb-cache/cache-store/memcache.rb', line 112 def expire(key) @memcache.delete(key) cache_stop_tracking(key) Merb.logger.info!("cache: expired (#{key})") true end |
#expire_all ⇒ Object
Expire all the cache entries
140 141 142 143 144 145 |
# File 'lib/merb-cache/cache-store/memcache.rb', line 140 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.
127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/merb-cache/cache-store/memcache.rb', line 127 def expire_match(key) Merb.logger.debug!("cache: attempting to expire #{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 |
#prepare ⇒ Object
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/cache-store/memcache.rb', line 23 def prepare namespace = @config[:namespace] || 'merb-cache' host = @config[:host] || '127.0.0.1:11211' @memcache = MemCache.new(host, {:namespace => namespace}) @tracking_key = "_#{namespace}_keys" unless @config[:no_tracking] == true raise NotReady unless @memcache.active? true rescue NameError raise NotDefined end |