Module: Cachai
- Defined in:
- lib/cachai.rb,
lib/models.rb
Defined Under Namespace
Classes: Admin, Middleware, Post, Response
Constant Summary
collapse
- CACHE_MINUTES =
10 * 60
Class Method Summary
collapse
Class Method Details
.boot(domain, redis_host = 'localhost') ⇒ Object
12
13
14
15
16
|
# 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 ⇒ Object
50
51
52
|
# File 'lib/models.rb', line 50
def self.cache
@cache
end
|
.cache=(obj) ⇒ Object
46
47
48
|
# File 'lib/models.rb', line 46
def self.cache=(obj)
@cache = obj
end
|
.clear_cache(path) ⇒ Object
54
55
56
|
# File 'lib/models.rb', line 54
def self.clear_cache(path)
cache.del(key_for(path))
end
|
.domain ⇒ Object
42
43
44
|
# File 'lib/models.rb', line 42
def self.domain
@domain
end
|
.domain=(value) ⇒ Object
38
39
40
|
# File 'lib/models.rb', line 38
def self.domain=(value)
@domain = value
end
|
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/models.rb', line 82
def self.get_and_sort_comments_for(post)
result = []
top_level = post.responses..approved.top_level
nested = post.responses..approved.nested
top_level.each_with_index do |, i|
obj = .as_json
children = nested.select do |nested|
nested.parent_id == .id
end
obj.merge!(:children => children) if children.any?
result.push(obj)
end
result
end
|
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/models.rb', line 63
def self.(path, nocache = false)
key = key_for(path)
unless !nocache && json_list = cache.get(key)
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) ⇒ Object
58
59
60
61
|
# File 'lib/models.rb', line 58
def self.key_for(path)
raise "Domain not set!" unless domain
"comments:#{domain}:#{path}"
end
|
.load_db! ⇒ Object
18
19
20
|
# File 'lib/models.rb', line 18
def self.load_db!
load_schema unless schema_loaded?
end
|
.load_schema ⇒ Object
22
23
24
|
# File 'lib/models.rb', line 22
def self.load_schema
require_relative '../db/schema.rb'
end
|
.schema_loaded? ⇒ Boolean
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/models.rb', line 26
def self.schema_loaded?
Post.first
true
rescue ActiveRecord::StatementInvalid => e
false
rescue ActiveRecord::ConnectionNotEstablished
puts "Connection not established."
false
end
|