GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Description: Merb More: The Full Stack. Take what you need; leave what you don't.
Homepage: http://www.merbivore.com
Clone URL: git://github.com/wycats/merb-more.git
ezmobius (author)
Thu Mar 06 14:29:56 -0800 2008
commit  9efa802c756f57eee432df5205854e415f19d125
tree    f32f1260449a80f2617d7ee00d3e9422ceb9878e
parent  bec1d610ee776927bbbc8d038835eb0b303da6db
merb-more / merb-cache / lib / merb-cache / cache-store / database-activerecord.rb
100644 53 lines (46 sloc) 1.425 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
module Merb::Cache::Store::ActiveRecord
  class CacheMigration < ActiveRecord::Migration
    def self.up
      create_table (Merb::Controller._cache.config[:table_name]), :primary_key => :ckey do |t|
        t.column :ckey, :string
        t.column :data, :text
        t.datetime :expire, :default => nil
      end
    end
    def self.down
      drop_table Merb::Controller._cache.config[:table_name]
    end
  end
 
  class CacheModel < ActiveRecord::Base
    set_table_name Merb::Controller._cache.config[:table_name]
    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
 
    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
 
    def self.expire(key)
      self.delete_all(["ckey=?", key])
    end
 
    def self.expire_match(key)
      self.delete_all(["ckey like ?", key + "%"])
    end
 
    def self.expire_all
      self.delete_all
    end
 
    def self.check_table
      CacheMigration.up unless self.table_exists?
    end
  end
end