padrino-cache 0.12.2 → 0.12.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/padrino-cache.rb +5 -1
- data/lib/padrino-cache/helpers/page.rb +63 -39
- data/test/test_padrino_cache.rb +112 -1
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1e64349dade4e85cb293e1b2de5381bf21d7f45c
|
4
|
+
data.tar.gz: 6fa8e3ab11b1ba47667497d81937e629eea2dcda
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0f408303e59ce1e0dbd4cf3f99fcfa42d0e8a3e1e3c007834066bcaf10793b961e4161332536ba6baac59683a50bec95f73b8cad9c177fea3ee86567b37e23a3
|
7
|
+
data.tar.gz: dfabcbecfa9dcb42c1f20af48209247bc0f785e2e97a417618f229393948f0e015a0e3f54e5aa11834698a89d1ba94f5ca9ec6fb60a0b457a62904d99d7ff551
|
data/lib/padrino-cache.rb
CHANGED
@@ -101,8 +101,12 @@ module Padrino
|
|
101
101
|
app.set :cache, Padrino::Cache.new(:File,
|
102
102
|
:dir => Padrino.root('tmp', defined?(app.app_name) ? app.app_name.to_s : '', 'cache'))
|
103
103
|
app.disable :caching
|
104
|
+
included(app)
|
105
|
+
end
|
106
|
+
|
107
|
+
def included(base)
|
108
|
+
base.extend Padrino::Cache::Helpers::Page::ClassMethods
|
104
109
|
end
|
105
|
-
alias :included :registered
|
106
110
|
|
107
111
|
def padrino_route_added(route, verb, path, args, options, block)
|
108
112
|
Padrino::Cache::Helpers::Page.padrino_route_added(route, verb, path, args, options, block)
|
@@ -60,8 +60,7 @@ module Padrino
|
|
60
60
|
#
|
61
61
|
# @api public
|
62
62
|
def expires(time)
|
63
|
-
@route.cache_expires = time
|
64
|
-
@_last_expires = time
|
63
|
+
@route.cache_expires = time
|
65
64
|
end
|
66
65
|
|
67
66
|
def expires_in(time)
|
@@ -94,55 +93,80 @@ module Padrino
|
|
94
93
|
# end
|
95
94
|
#
|
96
95
|
def cache_key(name = nil, &block)
|
97
|
-
|
98
|
-
@route.cache_key =
|
96
|
+
fail "Can not provide both cache_key and a block" if name && block
|
97
|
+
@route.cache_key = name || block
|
99
98
|
end
|
100
99
|
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
began_at = Time.now
|
106
|
-
|
107
|
-
value = settings.cache[resolve_cache_key || env['PATH_INFO']]
|
108
|
-
logger.debug "GET Cache", began_at, @route.cache_key || env['PATH_INFO'] if defined?(logger) && value
|
109
|
-
|
110
|
-
if value.kind_of?(Hash)
|
111
|
-
content_type value[:content_type]
|
112
|
-
halt 200, value[:body]
|
113
|
-
elsif value
|
114
|
-
halt 200, value
|
115
|
-
end
|
116
|
-
end
|
117
|
-
end
|
100
|
+
CACHED_VERBS = { 'GET' => true, 'HEAD' => true }.freeze
|
101
|
+
|
102
|
+
def self.padrino_route_added(route, verb, *)
|
103
|
+
return unless route.cache && CACHED_VERBS[verb]
|
118
104
|
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
:content_type => @_content_type
|
125
|
-
}
|
126
|
-
|
127
|
-
if @_last_expires
|
128
|
-
settings.cache.store(resolve_cache_key || env['PATH_INFO'], content, :expires => @_last_expires)
|
129
|
-
@_last_expires = nil
|
130
|
-
else
|
131
|
-
settings.cache.store(resolve_cache_key || env['PATH_INFO'], content)
|
132
|
-
end
|
133
|
-
|
134
|
-
logger.debug "SET Cache", began_at, @route.cache_key || env['PATH_INFO'] if defined?(logger)
|
135
|
-
end
|
105
|
+
route.before_filters do
|
106
|
+
next unless settings.caching?
|
107
|
+
if cached_response = load_cached_response
|
108
|
+
content_type cached_response[:content_type]
|
109
|
+
halt 200, cached_response[:body]
|
136
110
|
end
|
137
111
|
end
|
112
|
+
|
113
|
+
route.after_filters do
|
114
|
+
save_cached_response(route.cache_expires) if settings.caching?
|
115
|
+
end
|
138
116
|
end
|
139
117
|
|
140
118
|
private
|
119
|
+
|
120
|
+
def load_cached_response
|
121
|
+
began_at = Time.now
|
122
|
+
route_cache_key = resolve_cache_key || env['PATH_INFO']
|
123
|
+
|
124
|
+
value = settings.cache[route_cache_key]
|
125
|
+
logger.debug "GET Cache", began_at, route_cache_key if defined?(logger) && value
|
126
|
+
|
127
|
+
value
|
128
|
+
end
|
129
|
+
|
130
|
+
def save_cached_response(cache_expires)
|
131
|
+
return unless @_response_buffer.kind_of?(String)
|
132
|
+
|
133
|
+
began_at = Time.now
|
134
|
+
route_cache_key = resolve_cache_key || env['PATH_INFO']
|
135
|
+
|
136
|
+
content = {
|
137
|
+
:body => @_response_buffer,
|
138
|
+
:content_type => @_content_type
|
139
|
+
}
|
140
|
+
|
141
|
+
settings.cache.store(route_cache_key, content, :expires => cache_expires)
|
142
|
+
|
143
|
+
logger.debug "SET Cache", began_at, route_cache_key if defined?(logger)
|
144
|
+
end
|
145
|
+
|
141
146
|
##
|
142
147
|
# Resolve the cache_key when it's a block in the correct context.
|
143
148
|
#
|
144
149
|
def resolve_cache_key
|
145
|
-
|
150
|
+
key = @route.cache_key
|
151
|
+
key.is_a?(Proc) ? instance_eval(&key) : key
|
152
|
+
end
|
153
|
+
|
154
|
+
module ClassMethods
|
155
|
+
##
|
156
|
+
# A method to set `expires` time inside `controller` blocks.
|
157
|
+
#
|
158
|
+
# @example
|
159
|
+
# controller :users do
|
160
|
+
# expires 15
|
161
|
+
#
|
162
|
+
# get :show do
|
163
|
+
# 'shown'
|
164
|
+
# end
|
165
|
+
# end
|
166
|
+
#
|
167
|
+
def expires(time)
|
168
|
+
@_expires = time
|
169
|
+
end
|
146
170
|
end
|
147
171
|
end
|
148
172
|
end
|
data/test/test_padrino_cache.rb
CHANGED
@@ -38,6 +38,57 @@ describe "PadrinoCache" do
|
|
38
38
|
refute_equal false, called
|
39
39
|
end
|
40
40
|
|
41
|
+
it 'should cache HEAD verb' do
|
42
|
+
called_times = 0
|
43
|
+
mock_app do
|
44
|
+
register Padrino::Cache
|
45
|
+
enable :caching
|
46
|
+
get('/foo', :cache => true){ called_times += 1; called_times.to_s }
|
47
|
+
end
|
48
|
+
head "/foo"
|
49
|
+
head "/foo"
|
50
|
+
assert_equal 1, called_times
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'should not cache POST verb' do
|
54
|
+
called_times = 0
|
55
|
+
mock_app do
|
56
|
+
register Padrino::Cache
|
57
|
+
enable :caching
|
58
|
+
post('/foo', :cache => true){ called_times += 1; called_times.to_s }
|
59
|
+
end
|
60
|
+
post "/foo"
|
61
|
+
assert_equal 1, called_times
|
62
|
+
post "/foo"
|
63
|
+
assert_equal 2, called_times
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'should not cache DELETE verb' do
|
67
|
+
called_times = 0
|
68
|
+
mock_app do
|
69
|
+
register Padrino::Cache
|
70
|
+
enable :caching
|
71
|
+
delete('/foo', :cache => true){ called_times += 1; called_times.to_s }
|
72
|
+
end
|
73
|
+
delete "/foo"
|
74
|
+
assert_equal 1, called_times
|
75
|
+
delete "/foo"
|
76
|
+
assert_equal 2, called_times
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'should not cache PUT verb' do
|
80
|
+
called_times = 0
|
81
|
+
mock_app do
|
82
|
+
register Padrino::Cache
|
83
|
+
enable :caching
|
84
|
+
put('/foo', :cache => true){ called_times += 1; called_times.to_s }
|
85
|
+
end
|
86
|
+
put "/foo"
|
87
|
+
assert_equal 1, called_times
|
88
|
+
put "/foo"
|
89
|
+
assert_equal 2, called_times
|
90
|
+
end
|
91
|
+
|
41
92
|
it 'should delete from the cache' do
|
42
93
|
called = false
|
43
94
|
mock_app do
|
@@ -115,7 +166,7 @@ describe "PadrinoCache" do
|
|
115
166
|
assert_equal 'test page again', body
|
116
167
|
end
|
117
168
|
|
118
|
-
it 'should
|
169
|
+
it 'should allow controller-wide caching' do
|
119
170
|
called = false
|
120
171
|
mock_app do
|
121
172
|
controller :cache => true do
|
@@ -132,6 +183,26 @@ describe "PadrinoCache" do
|
|
132
183
|
assert_equal 'test', body
|
133
184
|
end
|
134
185
|
|
186
|
+
it 'should allow controller-wide expires' do
|
187
|
+
called = false
|
188
|
+
mock_app do
|
189
|
+
register Padrino::Cache
|
190
|
+
controller :cache => true do
|
191
|
+
enable :caching
|
192
|
+
expires 1
|
193
|
+
get("/foo"){ called ? halt(500) : (called = 'test') }
|
194
|
+
end
|
195
|
+
end
|
196
|
+
get "/foo"
|
197
|
+
assert_equal 200, status
|
198
|
+
assert_equal 'test', body
|
199
|
+
get "/foo"
|
200
|
+
assert_equal 200, status
|
201
|
+
assert_equal 'test', body
|
202
|
+
Time.stub(:now, Time.now + 2) { get "/foo" }
|
203
|
+
assert_equal 500, status
|
204
|
+
end
|
205
|
+
|
135
206
|
it 'should allow cache disabling on a per route basis' do
|
136
207
|
called = false
|
137
208
|
mock_app do
|
@@ -346,4 +417,44 @@ describe "PadrinoCache" do
|
|
346
417
|
assert_equal 'bar', body
|
347
418
|
assert_equal 1, counter
|
348
419
|
end
|
420
|
+
|
421
|
+
it 'should allow different expiring times for different pages' do
|
422
|
+
skip
|
423
|
+
called_times_a = 0
|
424
|
+
called_times_b = 0
|
425
|
+
mock_app do
|
426
|
+
register Padrino::Cache
|
427
|
+
enable :caching
|
428
|
+
controller :cache => true do
|
429
|
+
get("/foo") do
|
430
|
+
expires 1
|
431
|
+
called_times_a += 1
|
432
|
+
called_times_b.to_s
|
433
|
+
end
|
434
|
+
get("/bar") do
|
435
|
+
expires 3
|
436
|
+
called_times_b += 1
|
437
|
+
called_times_b.to_s
|
438
|
+
end
|
439
|
+
end
|
440
|
+
end
|
441
|
+
Time.stub(:now, Time.now) { get "/foo"; get "/bar" }
|
442
|
+
assert_equal 1, called_times_a
|
443
|
+
assert_equal 1, called_times_b
|
444
|
+
Time.stub(:now, Time.now + 0.5) { get "/foo"; get "/bar" }
|
445
|
+
assert_equal 1, called_times_a
|
446
|
+
assert_equal 1, called_times_b
|
447
|
+
Time.stub(:now, Time.now + 2) { get "/foo"; get "/bar" }
|
448
|
+
assert_equal 2, called_times_a
|
449
|
+
assert_equal 1, called_times_b
|
450
|
+
Time.stub(:now, Time.now + 2.5) { get "/foo"; get "/bar" }
|
451
|
+
assert_equal 2, called_times_a
|
452
|
+
assert_equal 1, called_times_b
|
453
|
+
Time.stub(:now, Time.now + 4) { get "/foo"; get "/bar" }
|
454
|
+
assert_equal 3, called_times_a
|
455
|
+
assert_equal 2, called_times_b
|
456
|
+
Time.stub(:now, Time.now + 5.5) { get "/foo"; get "/bar" }
|
457
|
+
assert_equal 4, called_times_a
|
458
|
+
assert_equal 2, called_times_b
|
459
|
+
end
|
349
460
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: padrino-cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.12.
|
4
|
+
version: 0.12.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Padrino Team
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2014-
|
14
|
+
date: 2014-08-13 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: padrino-core
|
@@ -19,28 +19,28 @@ dependencies:
|
|
19
19
|
requirements:
|
20
20
|
- - '='
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: 0.12.
|
22
|
+
version: 0.12.3
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
26
26
|
requirements:
|
27
27
|
- - '='
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version: 0.12.
|
29
|
+
version: 0.12.3
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
31
|
name: padrino-helpers
|
32
32
|
requirement: !ruby/object:Gem::Requirement
|
33
33
|
requirements:
|
34
34
|
- - '='
|
35
35
|
- !ruby/object:Gem::Version
|
36
|
-
version: 0.12.
|
36
|
+
version: 0.12.3
|
37
37
|
type: :runtime
|
38
38
|
prerelease: false
|
39
39
|
version_requirements: !ruby/object:Gem::Requirement
|
40
40
|
requirements:
|
41
41
|
- - '='
|
42
42
|
- !ruby/object:Gem::Version
|
43
|
-
version: 0.12.
|
43
|
+
version: 0.12.3
|
44
44
|
- !ruby/object:Gem::Dependency
|
45
45
|
name: moneta
|
46
46
|
requirement: !ruby/object:Gem::Requirement
|