Class: Merb::Cache::MemoryStore
- Inherits:
-
Object
- Object
- Merb::Cache::MemoryStore
- Defined in:
- lib/merb-cache/cache-store/memory.rb
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 the file using the specified key The entry is deleted if it has expired.
-
#cache_set(key, data, from_now = nil) ⇒ Object
Store data to the file 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 ⇒ MemoryStore
constructor
Provides the memory 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 ⇒ MemoryStore
Provides the memory cache store for merb-cache
4 5 6 7 8 9 |
# File 'lib/merb-cache/cache-store/memory.rb', line 4 def initialize @config = Merb::Controller._cache.config @cache = {} @mutex = Mutex.new 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.
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/merb-cache/cache-store/memory.rb', line 52 def cache(_controller, key, from_now = nil, &block) if @cache.key?(key) _data, _expire = *cache_read(key) _cache_hit = _expire.nil? || Time.now < _expire end unless _cache_hit _expire = from_now ? from_now.minutes.from_now : nil _data = _controller.send(:capture, &block) cache_write(key, [_data, _expire]) end _controller.send(:concat, _data, block.binding) true end |
#cache_get(key) ⇒ Object
Fetch data from the file 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
89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/merb-cache/cache-store/memory.rb', line 89 def cache_get(key) if @cache.key?(key) _data, _expire = *cache_read(key) if _expire.nil? || Time.now < _expire Merb.logger.info("cache: hit (#{key})") return _data end @mutex.synchronize do @cache.delete(key) end end Merb.logger.info("cache: miss (#{key})") nil end |
#cache_set(key, data, from_now = nil) ⇒ Object
Store data to the file 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
73 74 75 76 77 78 |
# File 'lib/merb-cache/cache-store/memory.rb', line 73 def cache_set(key, data, from_now = nil) _expire = from_now ? from_now.minutes.from_now : nil cache_write(key, [data, _expire]) 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
139 140 141 |
# File 'lib/merb-cache/cache-store/memory.rb', line 139 def cache_store_type "memory" 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
24 25 26 27 28 29 30 31 |
# File 'lib/merb-cache/cache-store/memory.rb', line 24 def cached?(key) if @cache.key?(key) _data, _expire = *cache_read(key) return true if _expire.nil? || Time.now < _expire expire(key) end false end |
#expire(key) ⇒ Object
Expire the cache entry identified by the given key
Parameter
- key<Sting>
-
The key identifying the cache entry
106 107 108 109 110 111 112 |
# File 'lib/merb-cache/cache-store/memory.rb', line 106 def expire(key) @mutex.synchronize do @cache.delete(key) end Merb.logger.info("cache: expired (#{key})") true end |
#expire_all ⇒ Object
Expire all the cache entries
127 128 129 130 131 132 133 |
# File 'lib/merb-cache/cache-store/memory.rb', line 127 def expire_all @mutex.synchronize do @cache.clear end 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
118 119 120 121 122 123 124 |
# File 'lib/merb-cache/cache-store/memory.rb', line 118 def expire_match(key) @mutex.synchronize do @cache.delete_if do |k,v| k.match(/#{key}/) end end Merb.logger.info("cache: expired matching (#{key})") true end |
#prepare ⇒ Object
This method is there to ensure minimal requirements are met (directories are accessible, table exists, connected to server, …)
13 14 15 |
# File 'lib/merb-cache/cache-store/memory.rb', line 13 def prepare true end |