module Egalite::ControllerCache
Attributes
table[RW]
Public Class Methods
create_table(db, opts = {})
click to toggle source
# 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 = {})
click to toggle source
# 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)
click to toggle source
# File lib/egalite/cache.rb, line 47 def self.included(base) base.extend(ClassMethods) end
Public Instance Methods
__controller_cache__dataset(options)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
Calls superclass method
# 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_filter()
click to toggle source
Calls superclass method
# 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