Class: Merb::Cache::DatabaseStore::ActiveRecord::CacheModel

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/merb-cache/cache-store/database-activerecord.rb

Overview

The cache model

Class Method Summary collapse

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



32
33
34
35
36
37
38
# File 'lib/merb-cache/cache-store/database-activerecord.rb', line 32

def self.cache_get(key)
  if entry = self.find(:first, :conditions => ["ckey=?", key])
    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



51
52
53
54
55
56
57
58
59
60
# File 'lib/merb-cache/cache-store/database-activerecord.rb', line 51

def self.cache_set(key, data, expire = nil, get = true)
  attributes = {:ckey => key, :data => data, :expire => expire }
  if get
    entry = self.find(:first, :conditions => ["ckey=?",key])
    entry.nil? ? self.create(attributes) : entry.update_attributes(attributes)
  else
    self.create(attributes)
  end
  true
end

.check_tableObject

Perform auto-migration in case the table is unknown in the database



84
85
86
# File 'lib/merb-cache/cache-store/database-activerecord.rb', line 84

def self.check_table
  CacheMigration.up 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



66
67
68
# File 'lib/merb-cache/cache-store/database-activerecord.rb', line 66

def self.expire(key)
  self.delete_all(["ckey=?", key])
end

.expire_allObject

Expire all the cache entries



79
80
81
# File 'lib/merb-cache/cache-store/database-activerecord.rb', line 79

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



74
75
76
# File 'lib/merb-cache/cache-store/database-activerecord.rb', line 74

def self.expire_match(key)
  self.delete_all(["ckey like ?", key + "%"])
end