Module: Egalite::ControllerCache

Defined in:
lib/egalite/cache.rb

Defined Under Namespace

Modules: ClassMethods

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.tableObject

Returns the value of attribute table.



18
19
20
# File 'lib/egalite/cache.rb', line 18

def table
  @table
end

Class Method Details

.create_table(db, opts = {}) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/egalite/cache.rb', line 20

def create_table(db, opts = {})
  table = opts[:table_name] || :controller_cache
  
  create_table_without_query(db, opts)
  db.alter_table(table) {
    add_column :query_string, :varchar
  }
end

.create_table_without_query(db, opts = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/egalite/cache.rb', line 28

def create_table_without_query(db, opts = {})
  table = opts[:table_name] || :controller_cache
  
  db.create_table(table) {
    primary_key :id, :integer, :auto_increment => true
    column :inner_path, :varchar
    column :language, :varchar
    column :updated_at, :timestamp
    column :content, :varchar
  }
end

.included(base) ⇒ Object



47
48
49
# File 'lib/egalite/cache.rb', line 47

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#__controller_cache__dataset(options) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/egalite/cache.rb', line 50

def __controller_cache__dataset(options)
  table = Egalite::ControllerCache.table
  dataset = table.filter(:inner_path => req.inner_path)
  if options[:with_query]
    dataset = dataset.filter(:query_string => __query_string(options))
  end
  if req.language
    dataset = dataset.filter(:language => req.language)
  end
  dataset
end

#__query_string(options) ⇒ Object



61
62
63
64
# File 'lib/egalite/cache.rb', line 61

def __query_string(options)
  return nil unless options[:with_query] and not req.rack_request.query_string.empty?
  req.rack_request.query_string
end

#after_filter_html(html) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/egalite/cache.rb', line 81

def after_filter_html(html)
  html = super(html)
  cache = self.class.controller_cache_actions[req.action_method]
  if cache and Egalite::ControllerCache.table
    dataset = __controller_cache__dataset(cache)
    data = {
      :inner_path => req.inner_path,
      :language => req.language,
      :updated_at => Time.now,
      :content => html,
    }
    if cache[:with_query]
      data[:query_string] = __query_string(cache)
    end
    if dataset.count > 0
      dataset.update(data)
    else
      Egalite::ControllerCache.table.insert(data)
    end
  end
  return html
end

#before_filterObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/egalite/cache.rb', line 65

def before_filter
  cache = self.class.controller_cache_actions[req.action_method]
  if cache and Egalite::ControllerCache.table
    result = super
    if result != true
      return result
    end
    dataset = __controller_cache__dataset(cache)
    record = dataset.first
    return true unless record
    return true if record[:updated_at] < (Time.now - cache[:expire])
    record[:content]
  else
    super
  end
end