cachai 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/cachai.gemspec +2 -1
- data/db/schema.rb +1 -0
- data/lib/cachai.rb +39 -8
- metadata +16 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 30fc83e3066f50a0c604c9491d162620e656abe2
|
4
|
+
data.tar.gz: 230bbda6e5ea18c7423232aef1b77140684b6118
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: da12f0377d0f63859e1a11e7f634c4d4a3586360e077d7eb10b17919fe75b81a03da04d4b4c57d2d3a91d537bd519169f49815e041cb8bdcca794bdb3166faf1
|
7
|
+
data.tar.gz: 5274c7264da974acf125f72dae62f7c30256867dc9718e8117bfa5f354bf8ee122051ed9027472965b3be8f394f7f18e9b2154e33cf8c69992c21190e8701c97
|
data/cachai.gemspec
CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = "cachai"
|
7
|
-
spec.version = '0.0.
|
7
|
+
spec.version = '0.0.2'
|
8
8
|
spec.authors = ["Tomás Pollak"]
|
9
9
|
spec.email = ["tomas@forkhq.com"]
|
10
10
|
spec.description = %q{Middleware for embedabble comments.}
|
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
|
|
20
20
|
spec.add_development_dependency "bundler", "~> 1.3"
|
21
21
|
spec.add_development_dependency "rspec", "~> 2.6"
|
22
22
|
|
23
|
+
spec.add_dependency "redis"
|
23
24
|
spec.add_dependency "sinatra"
|
24
25
|
spec.add_dependency "sinatra-activerecord"
|
25
26
|
spec.add_dependency "sqlite3"
|
data/db/schema.rb
CHANGED
data/lib/cachai.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
require 'sinatra/base'
|
2
|
+
require 'redis'
|
2
3
|
require 'json'
|
3
4
|
require 'rake'
|
5
|
+
|
4
6
|
require_relative 'comment'
|
5
7
|
require_relative 'akismet'
|
6
8
|
|
@@ -11,6 +13,10 @@ module Cachai
|
|
11
13
|
# set :database_file, "config/database.yml"
|
12
14
|
# set :public_folder, File.join(settings.root, 'public')
|
13
15
|
# set :protection, true
|
16
|
+
# set :logging, nil
|
17
|
+
|
18
|
+
# use Rack::CommonLogger, LOGGER
|
19
|
+
# use Rack::Static, :urls => %w(/css /img /js /favicon.ico), :root => 'public'
|
14
20
|
use ActiveRecord::ConnectionAdapters::ConnectionManagement
|
15
21
|
|
16
22
|
def initialize(app, opts = nil)
|
@@ -19,6 +25,9 @@ module Cachai
|
|
19
25
|
|
20
26
|
load_schema unless schema_loaded?
|
21
27
|
|
28
|
+
redis_host = opts.delete(:redis_host) || 'localhost'
|
29
|
+
@redis = Redis.new(:host => redis_host)
|
30
|
+
|
22
31
|
if key = opts.delete(:akismet_key)
|
23
32
|
@akismet = Akismet.new(:api_key => key, :blog => "http://#{@domain}")
|
24
33
|
else
|
@@ -32,18 +41,17 @@ module Cachai
|
|
32
41
|
check_domain!(params[:domain])
|
33
42
|
|
34
43
|
# puts "Comments for: #{params[:domain]}#{params[:path]}"
|
35
|
-
|
44
|
+
json_list = get_comments(params[:path])
|
36
45
|
|
37
46
|
if params[:callback]
|
38
47
|
content_type 'application/javascript'
|
39
|
-
"#{params[:callback]}(#{
|
48
|
+
"#{params[:callback]}(#{json_list});"
|
40
49
|
else
|
41
50
|
json(list)
|
42
51
|
end
|
43
52
|
end
|
44
53
|
|
45
54
|
post '/comments.?:format?' do
|
46
|
-
|
47
55
|
begin
|
48
56
|
data = JSON.parse(request.body.read)
|
49
57
|
check_domain!(data['domain'])
|
@@ -62,6 +70,8 @@ module Cachai
|
|
62
70
|
}
|
63
71
|
|
64
72
|
comment = Comment.create!(attrs)
|
73
|
+
|
74
|
+
@redis.del(redis_key(data['path']))
|
65
75
|
json({ :status => 'ok', :comment => comment })
|
66
76
|
|
67
77
|
rescue JSON::ParserError
|
@@ -73,9 +83,19 @@ module Cachai
|
|
73
83
|
|
74
84
|
private
|
75
85
|
|
86
|
+
def set_cache(timestamp)
|
87
|
+
return if timestamp.nil?
|
88
|
+
last_modified timestamp
|
89
|
+
cache_control :public, :must_revalidate, :max_age => 60
|
90
|
+
end
|
91
|
+
|
92
|
+
def prevent_cache
|
93
|
+
cache_control :public, :no_cache, :no_store, :must_revalidate, :max_age => 0
|
94
|
+
# expires 1.year.ago
|
95
|
+
end
|
96
|
+
|
76
97
|
def load_schema
|
77
98
|
require 'sinatra/activerecord/rake'
|
78
|
-
# Rake::Task['db:schema:load'].invoke
|
79
99
|
require_relative '../db/schema.rb'
|
80
100
|
end
|
81
101
|
|
@@ -83,7 +103,7 @@ module Cachai
|
|
83
103
|
Comment.first
|
84
104
|
true
|
85
105
|
rescue ActiveRecord::StatementInvalid => e
|
86
|
-
# SQLite3::SQLException => e
|
106
|
+
# SQLite3::SQLException => e
|
87
107
|
# return !e.message['no such table']
|
88
108
|
false
|
89
109
|
end
|
@@ -98,11 +118,22 @@ module Cachai
|
|
98
118
|
|
99
119
|
def json(obj)
|
100
120
|
content_type 'application/json'
|
101
|
-
obj.to_json
|
121
|
+
return obj.is_a?(String) ? obj : obj.to_json
|
122
|
+
end
|
123
|
+
|
124
|
+
def get_comments(path)
|
125
|
+
key = redis_key(path)
|
126
|
+
unless json_list = @redis.get(key)
|
127
|
+
puts "Not cached. Getting from DB: #{path}"
|
128
|
+
json_list = Comment.where({ :path => path }).to_json
|
129
|
+
@redis.set(key, json_list)
|
130
|
+
@redis.expire(key, 60 * 60) # one hour
|
131
|
+
end
|
132
|
+
json_list
|
102
133
|
end
|
103
134
|
|
104
|
-
def
|
105
|
-
|
135
|
+
def redis_key(path)
|
136
|
+
"comments:#{@domain}:#{path}"
|
106
137
|
end
|
107
138
|
|
108
139
|
def is_spam?(data, link, request)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cachai
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tomás Pollak
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '2.6'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: redis
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: sinatra
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -134,10 +148,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
134
148
|
version: '0'
|
135
149
|
requirements: []
|
136
150
|
rubyforge_project:
|
137
|
-
rubygems_version: 2.
|
151
|
+
rubygems_version: 2.4.5
|
138
152
|
signing_key:
|
139
153
|
specification_version: 4
|
140
154
|
summary: Middleware for embedabble comments.
|
141
155
|
test_files:
|
142
156
|
- test/app_test.rb
|
143
|
-
has_rdoc:
|