Class: Merb::Cache::DatabaseStore::DataMapper::CacheModel
- Inherits:
-
DataMapper::Base
- Object
- DataMapper::Base
- Merb::Cache::DatabaseStore::DataMapper::CacheModel
- Defined in:
- lib/merb-cache/cache-store/database-datamapper.rb
Overview
The cache model
Class Method Summary collapse
-
.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, expire = nil, get = true) ⇒ Object
Store data to the database using the specified key.
-
.check_table ⇒ Object
Perform auto-migration in case the table is unknown in the database.
-
.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.
Class Method Details
.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
20 21 22 23 24 25 26 |
# File 'lib/merb-cache/cache-store/database-datamapper.rb', line 20 def self.cache_get(key) if entry = self.first(key) return entry.data if entry.expire.nil? || DateTime.now < entry.expire entry.destroy! end nil end |
.cache_set(key, data, expire = nil, get = true) ⇒ 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
- expire<~minutes>
-
The number of minutes (from now) the cache should persist
- get<Boolean>
-
used internally to behave like this:
-
when set to true, perform update_or_create on the cache entry
-
when set to false, force creation of the cache entry
-
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/merb-cache/cache-store/database-datamapper.rb', line 39 def self.cache_set(key, data, expire = nil, get = true) attributes = {:ckey => key, :data => data, :expire => expire.nil? ? nil : expire.to_s_db } if get entry = self.first(key) entry.nil? ? self.create(attributes) : entry.update_attributes(attributes) else self.create(attributes) end true end |
.check_table ⇒ Object
Perform auto-migration in case the table is unknown in the database
75 76 77 |
# File 'lib/merb-cache/cache-store/database-datamapper.rb', line 75 def self.check_table self.auto_migrate! unless self.table.exists? end |
.expire(key) ⇒ Object
Expire the cache entry identified by the given key
Parameter
- key<Sting>
-
The key identifying the cache entry
55 56 57 58 |
# File 'lib/merb-cache/cache-store/database-datamapper.rb', line 55 def self.expire(key) entry = self.first(key) entry.destroy! unless entry.nil? end |
.expire_all ⇒ Object
Expire all the cache entries
70 71 72 |
# File 'lib/merb-cache/cache-store/database-datamapper.rb', line 70 def self.expire_all self.truncate! end |
.expire_match(key) ⇒ Object
Expire the cache entries matching the given key
Parameter
- key<Sting>
-
The key matching the cache entries
64 65 66 67 |
# File 'lib/merb-cache/cache-store/database-datamapper.rb', line 64 def self.expire_match(key) #FIXME database.execute("DELETE FROM #{self.table.name} WHERE ckey LIKE '#{key}%'") end |