Class: Merb::Cache::DatabaseStore::Sequel::CacheModel
- Inherits:
-
Object
- Object
- Merb::Cache::DatabaseStore::Sequel::CacheModel
- Defined in:
- lib/merb-cache/cache-store/database-sequel.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
22 23 24 25 26 27 28 |
# File 'lib/merb-cache/cache-store/database-sequel.rb', line 22 def self.cache_get(key) if entry = self.filter(:ckey => key).single_record(:limit => 1) return entry.data if entry.expire.nil? || Time.now < entry.expire self.expire(key) 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
-
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/merb-cache/cache-store/database-sequel.rb', line 41 def self.cache_set(key, data, expire = nil, get = true) attributes = {:ckey => key, :data => data, :expire => expire } if get entry = self.filter(:ckey => key).single_record(:limit => 1) entry.nil? ? self.create(attributes) : entry.set(attributes) else self.create(attributes) end true end |
.check_table ⇒ Object
Perform auto-migration in case the table is unknown in the database
74 75 76 |
# File 'lib/merb-cache/cache-store/database-sequel.rb', line 74 def self.check_table self.create_table 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
56 57 58 |
# File 'lib/merb-cache/cache-store/database-sequel.rb', line 56 def self.expire(key) self.filter(:ckey => key).delete end |
.expire_all ⇒ Object
Expire all the cache entries
69 70 71 |
# File 'lib/merb-cache/cache-store/database-sequel.rb', line 69 def self.expire_all self.delete_all 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 |
# File 'lib/merb-cache/cache-store/database-sequel.rb', line 64 def self.expire_match(key) self.filter{:ckey.like key + "%"}.delete end |