Class: Merb::Cache::DatabaseStore
- Inherits:
-
Object
- Object
- Merb::Cache::DatabaseStore
- Includes:
- ActiveRecord, DataMapper, Sequel
- Defined in:
- lib/merb-cache/cache-store/database.rb
Defined Under Namespace
Classes: OrmNotFound
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 database using the specified key The entry is deleted if it has expired.
-
#cache_set(key, data, from_now = nil) ⇒ Object
Store data to the database 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 ⇒ DatabaseStore
constructor
Provides the database cache store for merb-cache.
-
#prepare ⇒ Object
This method is there to ensure minimal requirements are met (file permissions, table exists, connected to server, …).
Constructor Details
#initialize ⇒ DatabaseStore
Provides the database cache store for merb-cache
4 5 6 7 |
# File 'lib/merb-cache/cache-store/database.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.
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/merb-cache/cache-store/database.rb', line 68 def cache(_controller, key, from_now = nil, &block) _data = CacheModel.cache_get(key) if _data.nil? _expire = from_now ? from_now.minutes.from_now : nil _data = _controller.send(:capture, &block) CacheModel.cache_set(key, Marshal.dump(_data), _expire, false) else _data = Marshal.load(_data) end _controller.send(:concat, _data, block.binding) true end |
#cache_get(key) ⇒ Object
Fetch data from the database 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
104 105 106 107 108 |
# File 'lib/merb-cache/cache-store/database.rb', line 104 def cache_get(key) data = CacheModel.cache_get(key) Merb.logger.info("cache: #{data.nil? ? "miss" : "hit"} (#{key})") data.nil? ? nil : Marshal.load(data) end |
#cache_set(key, data, from_now = nil) ⇒ Object
Store data to the database 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
88 89 90 91 92 93 |
# File 'lib/merb-cache/cache-store/database.rb', line 88 def cache_set(key, data, from_now = nil) _expire = from_now ? from_now.minutes.from_now : nil CacheModel.cache_set(key, Marshal.dump(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
141 142 143 |
# File 'lib/merb-cache/cache-store/database.rb', line 141 def cache_store_type "database" 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
45 46 47 |
# File 'lib/merb-cache/cache-store/database.rb', line 45 def cached?(key) not CacheModel.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
114 115 116 117 118 |
# File 'lib/merb-cache/cache-store/database.rb', line 114 def expire(key) CacheModel.expire(key) Merb.logger.info("cache: expired (#{key})") true end |
#expire_all ⇒ Object
Expire all the cache entries
131 132 133 134 135 |
# File 'lib/merb-cache/cache-store/database.rb', line 131 def expire_all CacheModel.expire_all 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
124 125 126 127 128 |
# File 'lib/merb-cache/cache-store/database.rb', line 124 def expire_match(key) CacheModel.expire_match(key) Merb.logger.info("cache: expired matching (#{key})") true end |
#prepare ⇒ Object
This method is there to ensure minimal requirements are met (file permissions, table exists, connected to server, …)
33 34 35 36 |
# File 'lib/merb-cache/cache-store/database.rb', line 33 def prepare CacheModel.check_table true end |