module Cachai
Constants
- CACHE_MINUTES
Public Class Methods
boot(domain, redis_host = 'localhost')
click to toggle source
# File lib/models.rb, line 12 def self.boot(domain, redis_host = 'localhost') self.domain = domain self.cache = Redis.new(:host => redis_host) self.load_db! end
cache()
click to toggle source
# File lib/models.rb, line 50 def self.cache @cache end
cache=(obj)
click to toggle source
# File lib/models.rb, line 46 def self.cache=(obj) @cache = obj end
clear_cache(path)
click to toggle source
# File lib/models.rb, line 54 def self.clear_cache(path) cache.del(key_for(path)) end
domain()
click to toggle source
# File lib/models.rb, line 42 def self.domain @domain end
domain=(value)
click to toggle source
# File lib/models.rb, line 38 def self.domain=(value) @domain = value end
get_and_sort_comments_for(post)
click to toggle source
# File lib/models.rb, line 82 def self.get_and_sort_comments_for(post) result = [] top_level = post.responses.comment.approved.top_level nested = post.responses.comment.approved.nested top_level.each_with_index do |comment, i| obj = comment.as_json children = nested.select do |nested| nested.parent_id == comment.id end obj.merge!(:children => children) if children.any? result.push(obj) end result end
get_comments_for(path, nocache = false)
click to toggle source
# File lib/models.rb, line 63 def self.get_comments_for(path, nocache = false) key = key_for(path) unless !nocache && json_list = cache.get(key) # puts "Not cached. Getting from DB: #{path}" if post = Post.find_by_path(path) json_list = get_and_sort_comments_for(post).to_json else json_list = '[]' end cache.set(key, json_list) cache.expire(key, CACHE_MINUTES) end json_list end
key_for(path)
click to toggle source
# File lib/models.rb, line 58 def self.key_for(path) raise "Domain not set!" unless domain "comments:#{domain}:#{path}" end
load_db!()
click to toggle source
# File lib/models.rb, line 18 def self.load_db! load_schema unless schema_loaded? end
load_schema()
click to toggle source
# File lib/models.rb, line 22 def self.load_schema require_relative '../db/schema.rb' end
schema_loaded?()
click to toggle source
# File lib/models.rb, line 26 def self.schema_loaded? Post.first true rescue ActiveRecord::StatementInvalid => e # SQLite3::SQLException => e # return !e.message['no such table'] false rescue ActiveRecord::ConnectionNotEstablished puts "Connection not established." false end