padrino-cache 0.18.5 → 0.18.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.rdoc +2 -1
- data/lib/padrino-cache/helpers/fragment.rb +1 -3
- data/lib/padrino-cache/helpers/page.rb +13 -3
- data/lib/padrino-cache.rb +4 -0
- data/test/test_moneta_store.rb +46 -0
- data/test/test_padrino_cache.rb +176 -0
- metadata +6 -6
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: cf35f6badf559377cd57b34dc5b80e688971141df7e6ab08df9daa5ac34b76b0
|
|
4
|
+
data.tar.gz: 0d54a9c770822614501e60d2ed6a3472e7053de2394084ef11b4200919a71576
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 27e85ac63fdad98cfc838a5ee53f43d9f4c37e5a16a10ff561f652ccb9b01a4d7d1d37d45ad4091a152d2eaee989df5860116782e1f7670ca752ab8a457a2941
|
|
7
|
+
data.tar.gz: 294c2e9737c567d6ce8fffec7b79c70d2fb0881924cf8bddf3b0af6ba9e466e48648b79d6083539769b3e5f36431984ddcd0a2db80e605655de919e49b4b1d93
|
data/README.rdoc
CHANGED
|
@@ -339,7 +339,8 @@ but it will do so immediately if any tweets have been deleted.
|
|
|
339
339
|
==== Page-level expiration
|
|
340
340
|
|
|
341
341
|
Page-level expiration works exactly like the example above--by using
|
|
342
|
-
<tt>expire</tt> in your controller. The key is typically <tt>env['PATH_INFO']</tt
|
|
342
|
+
<tt>expire</tt> in your controller. The key is typically <tt>env['PATH_INFO']</tt>; for a route given <tt>host:</tt> it is the
|
|
343
|
+
lowercased host followed by the path (<tt>admin.example.com/</tt>).
|
|
343
344
|
|
|
344
345
|
== Copyright
|
|
345
346
|
|
|
@@ -54,17 +54,15 @@ module Padrino
|
|
|
54
54
|
if settings.cache.key?(key.to_s)
|
|
55
55
|
value = settings.cache[key.to_s]
|
|
56
56
|
logger.bench 'GET Fragment', began_at, key.to_s if defined?(logger) && logger.respond_to?(:bench) && logger.debug?
|
|
57
|
-
concat_content(value.to_s.html_safe)
|
|
58
57
|
else
|
|
59
58
|
value = capture_html(&block)
|
|
60
59
|
settings.cache.store(key.to_s, value, opts)
|
|
61
60
|
logger.bench 'SET Fragment', began_at, key.to_s if defined?(logger) && logger.respond_to?(:bench) && logger.debug?
|
|
62
|
-
concat_content(value)
|
|
63
61
|
end
|
|
64
62
|
else
|
|
65
63
|
value = capture_html(&block)
|
|
66
|
-
concat_content(value)
|
|
67
64
|
end
|
|
65
|
+
concat_content(value)
|
|
68
66
|
end
|
|
69
67
|
end
|
|
70
68
|
end
|
|
@@ -117,7 +117,8 @@ module Padrino
|
|
|
117
117
|
private
|
|
118
118
|
|
|
119
119
|
def serve_cached_response
|
|
120
|
-
|
|
120
|
+
@_cache_lookup_key = @_cache_key
|
|
121
|
+
if settings.caching? && @route.cache_lookup && (cached_response = load_cached_response)
|
|
121
122
|
env['sinatra.route'] = "#{request.request_method} #{@route.sinatra_pattern}"
|
|
122
123
|
content_type cached_response[:content_type]
|
|
123
124
|
halt 200, cached_response[:body]
|
|
@@ -127,7 +128,7 @@ module Padrino
|
|
|
127
128
|
|
|
128
129
|
def load_cached_response
|
|
129
130
|
began_at = Time.now
|
|
130
|
-
route_cache_key =
|
|
131
|
+
route_cache_key = page_cache_key
|
|
131
132
|
|
|
132
133
|
value = settings.cache[route_cache_key]
|
|
133
134
|
logger.bench 'GET Cache', began_at, route_cache_key if defined?(logger) && logger.respond_to?(:bench) && logger.debug? && value
|
|
@@ -139,7 +140,7 @@ module Padrino
|
|
|
139
140
|
return unless @_response_buffer.is_a?(String)
|
|
140
141
|
|
|
141
142
|
began_at = Time.now
|
|
142
|
-
route_cache_key =
|
|
143
|
+
route_cache_key = page_cache_key
|
|
143
144
|
|
|
144
145
|
content = {
|
|
145
146
|
body: @_response_buffer,
|
|
@@ -147,10 +148,19 @@ module Padrino
|
|
|
147
148
|
}
|
|
148
149
|
|
|
149
150
|
settings.cache.store(route_cache_key, content, expires: cache_expires)
|
|
151
|
+
# The lookup runs before the handler, so it can only use a key set by then (or one the route recorded
|
|
152
|
+
# from an earlier request). Look pages up once that key is the one this page was stored under.
|
|
153
|
+
@route.cache_lookup = (@_cache_key || @route.cache_key) == (@_cache_lookup_key || @route.cache_key)
|
|
150
154
|
|
|
151
155
|
logger.bench 'SET Cache', began_at, route_cache_key if defined?(logger) && logger.respond_to?(:bench) && logger.debug?
|
|
152
156
|
end
|
|
153
157
|
|
|
158
|
+
# The key a page is kept under: its cache_key, or else its path, after the host for a route
|
|
159
|
+
# given +host:+, since the same path can serve another route on another host.
|
|
160
|
+
def page_cache_key
|
|
161
|
+
resolve_cache_key || (@route.host ? "#{request.host.to_s.downcase}#{env['PATH_INFO']}" : env['PATH_INFO'])
|
|
162
|
+
end
|
|
163
|
+
|
|
154
164
|
##
|
|
155
165
|
# Resolve the cache_key when it's a block in the correct context.
|
|
156
166
|
#
|
data/lib/padrino-cache.rb
CHANGED
|
@@ -117,6 +117,10 @@ module Padrino
|
|
|
117
117
|
def self.new(name, options = {})
|
|
118
118
|
# Activate expiration by default
|
|
119
119
|
options[:expires] = true unless options.include?(:expires)
|
|
120
|
+
# Lock the stores that keep their data in a Hash in this process, which concurrent requests share.
|
|
121
|
+
# ceiling: other adapters Moneta lists as not thread-safe (PStore, YAML, Daybreak, DBM, ...) still
|
|
122
|
+
# need threadsafe: true from the caller; add them here if Padrino starts recommending one.
|
|
123
|
+
options[:threadsafe] = true if %i[LRUHash Memory].include?(name) && !options.include?(:threadsafe)
|
|
120
124
|
Moneta.new(name, options)
|
|
121
125
|
end
|
|
122
126
|
|
data/test/test_moneta_store.rb
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
require_relative 'helper'
|
|
2
|
+
require 'tmpdir'
|
|
2
3
|
|
|
3
4
|
describe 'Padrino::Cache - Moneta store' do
|
|
4
5
|
def setup
|
|
@@ -51,3 +52,48 @@ describe 'Padrino::Cache - Moneta store' do
|
|
|
51
52
|
assert_nil Padrino.cache[@test_key]
|
|
52
53
|
end
|
|
53
54
|
end
|
|
55
|
+
|
|
56
|
+
describe 'Padrino::Cache - thread safety' do
|
|
57
|
+
# Holds the store's lock (if it has one) inside each_key while another thread writes,
|
|
58
|
+
# and returns whether that write had to wait for the lock.
|
|
59
|
+
def write_waits_for_lock?(store)
|
|
60
|
+
store.clear
|
|
61
|
+
store['held'] = 'x'
|
|
62
|
+
inside = Queue.new
|
|
63
|
+
release = Queue.new
|
|
64
|
+
holder = Thread.new { store.each_key { inside << true; release.pop } }
|
|
65
|
+
inside.pop
|
|
66
|
+
writer = Thread.new { store['other'] = 'y' }
|
|
67
|
+
waited = writer.join(0.2).nil?
|
|
68
|
+
release << true
|
|
69
|
+
[holder, writer].each(&:join)
|
|
70
|
+
assert_equal 'y', store['other']
|
|
71
|
+
waited
|
|
72
|
+
ensure
|
|
73
|
+
release&.push(true)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
it 'locks the default store' do
|
|
77
|
+
assert write_waits_for_lock?(Padrino.cache)
|
|
78
|
+
ensure
|
|
79
|
+
Padrino.cache.clear
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
it 'locks the in-memory stores' do
|
|
83
|
+
assert write_waits_for_lock?(Padrino::Cache.new(:LRUHash))
|
|
84
|
+
assert write_waits_for_lock?(Padrino::Cache.new(:Memory))
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
it 'keeps an explicit threadsafe option' do
|
|
88
|
+
refute write_waits_for_lock?(Padrino::Cache.new(:LRUHash, threadsafe: false))
|
|
89
|
+
Dir.mktmpdir do |dir|
|
|
90
|
+
assert write_waits_for_lock?(Padrino::Cache.new(:File, dir: dir, threadsafe: true))
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
it 'leaves other stores unlocked' do
|
|
95
|
+
Dir.mktmpdir do |dir|
|
|
96
|
+
refute write_waits_for_lock?(Padrino::Cache.new(:File, dir: dir))
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
data/test/test_padrino_cache.rb
CHANGED
|
@@ -96,6 +96,24 @@ describe 'PadrinoCache' do
|
|
|
96
96
|
refute_equal false, called
|
|
97
97
|
end
|
|
98
98
|
|
|
99
|
+
it 'should cache the page of a route given host: for that host only' do
|
|
100
|
+
calls = Hash.new(0)
|
|
101
|
+
mock_app do
|
|
102
|
+
register Padrino::Cache
|
|
103
|
+
enable :caching
|
|
104
|
+
get(:index, host: 'admin.example.com', cache: true) { "admin #{calls[:admin] += 1}" }
|
|
105
|
+
get(:index, cache: true) { "public #{calls[:public] += 1}" }
|
|
106
|
+
end
|
|
107
|
+
get '/', {}, 'HTTP_HOST' => 'admin.example.com'
|
|
108
|
+
assert_equal 'admin 1', body
|
|
109
|
+
get '/', {}, 'HTTP_HOST' => 'www.example.com'
|
|
110
|
+
assert_equal 'public 1', body
|
|
111
|
+
get '/', {}, 'HTTP_HOST' => 'Admin.Example.COM'
|
|
112
|
+
assert_equal 'admin 1', body
|
|
113
|
+
get '/', {}, 'HTTP_HOST' => 'www.example.com'
|
|
114
|
+
assert_equal 'public 1', body
|
|
115
|
+
end
|
|
116
|
+
|
|
99
117
|
it 'should cache HEAD verb' do
|
|
100
118
|
called_times = 0
|
|
101
119
|
mock_app do
|
|
@@ -560,6 +578,84 @@ describe 'PadrinoCache' do
|
|
|
560
578
|
assert_equal 3, call_count
|
|
561
579
|
end
|
|
562
580
|
|
|
581
|
+
it "should look a page up under the route's own cache key from the first request" do
|
|
582
|
+
calls = 0
|
|
583
|
+
mock_app do
|
|
584
|
+
register Padrino::Cache
|
|
585
|
+
enable :caching
|
|
586
|
+
get '/dash', cache: true do
|
|
587
|
+
cache_key { "dash-#{params[:u]}" }
|
|
588
|
+
calls += 1
|
|
589
|
+
"page for #{params[:u]}"
|
|
590
|
+
end
|
|
591
|
+
end
|
|
592
|
+
@app.cache.store('/dash', { body: 'stored under the path', content_type: 'text/html' })
|
|
593
|
+
|
|
594
|
+
get '/dash?u=alice'
|
|
595
|
+
assert_equal 'page for alice', body
|
|
596
|
+
get '/dash?u=bob'
|
|
597
|
+
assert_equal 'page for bob', body
|
|
598
|
+
get '/dash?u=alice'
|
|
599
|
+
assert_equal 'page for alice', body
|
|
600
|
+
assert_equal 2, calls
|
|
601
|
+
end
|
|
602
|
+
|
|
603
|
+
it 'should not serve a page stored under another key to a route with a Symbol cache key' do
|
|
604
|
+
calls = 0
|
|
605
|
+
mock_app do
|
|
606
|
+
register Padrino::Cache
|
|
607
|
+
enable :caching
|
|
608
|
+
get '/about', cache: true do
|
|
609
|
+
cache_key :about
|
|
610
|
+
calls += 1
|
|
611
|
+
'about page'
|
|
612
|
+
end
|
|
613
|
+
end
|
|
614
|
+
@app.cache.store('/about', { body: 'stored under the path', content_type: 'text/html' })
|
|
615
|
+
|
|
616
|
+
head '/about'
|
|
617
|
+
assert_equal 1, calls
|
|
618
|
+
get '/about'
|
|
619
|
+
assert_equal 'about page', body
|
|
620
|
+
get '/about'
|
|
621
|
+
assert_equal 'about page', body
|
|
622
|
+
assert_equal 2, calls
|
|
623
|
+
end
|
|
624
|
+
|
|
625
|
+
it 'should not serve a page stored under the path to a route that sets a String cache key in its body' do
|
|
626
|
+
mock_app do
|
|
627
|
+
register Padrino::Cache
|
|
628
|
+
enable :caching
|
|
629
|
+
get '/post/:id', cache: true do
|
|
630
|
+
cache_key "post-#{params[:id]}"
|
|
631
|
+
"post #{params[:id]}"
|
|
632
|
+
end
|
|
633
|
+
end
|
|
634
|
+
@app.cache.store('/post/1', { body: 'stored under the path', content_type: 'text/html' })
|
|
635
|
+
|
|
636
|
+
2.times { get '/post/1' }
|
|
637
|
+
assert_equal 'post 1', body
|
|
638
|
+
end
|
|
639
|
+
|
|
640
|
+
it 'should not serve the page of another route on the same path to a route with a block cache key' do
|
|
641
|
+
mock_app do
|
|
642
|
+
register Padrino::Cache
|
|
643
|
+
enable :caching
|
|
644
|
+
get('/p', cache: true, agent: /bot/) { 'bot page' }
|
|
645
|
+
get '/p', cache: true do
|
|
646
|
+
cache_key { "p-#{params[:u]}" }
|
|
647
|
+
"page for #{params[:u]}"
|
|
648
|
+
end
|
|
649
|
+
end
|
|
650
|
+
|
|
651
|
+
get '/p', {}, 'HTTP_USER_AGENT' => 'bot'
|
|
652
|
+
assert_equal 'bot page', body
|
|
653
|
+
get '/p?u=alice', {}, 'HTTP_USER_AGENT' => 'browser'
|
|
654
|
+
assert_equal 'page for alice', body
|
|
655
|
+
get '/p', {}, 'HTTP_USER_AGENT' => 'bot'
|
|
656
|
+
assert_equal 'bot page', body
|
|
657
|
+
end
|
|
658
|
+
|
|
563
659
|
it 'should raise an error if providing both a cache_key and block' do
|
|
564
660
|
mock_app do
|
|
565
661
|
register Padrino::Cache
|
|
@@ -721,4 +817,84 @@ describe 'PadrinoCache' do
|
|
|
721
817
|
get '/foo'
|
|
722
818
|
assert_equal 'application/json;charset=utf-8', last_response.content_type
|
|
723
819
|
end
|
|
820
|
+
|
|
821
|
+
it 'should keep cached fragment escaping with LRUHash store' do
|
|
822
|
+
mock_app do
|
|
823
|
+
register Padrino::Cache
|
|
824
|
+
register Padrino::Helpers
|
|
825
|
+
enable :caching
|
|
826
|
+
set :cache, Padrino::Cache.new(:LRUHash)
|
|
827
|
+
get '/plain' do
|
|
828
|
+
erb '<div><%= cache(:plain_frag) { "<b>escaped</b>" } %></div>'
|
|
829
|
+
end
|
|
830
|
+
get '/safe' do
|
|
831
|
+
erb '<div><%= cache(:safe_frag) { "<b>raw</b>".html_safe } %></div>'
|
|
832
|
+
end
|
|
833
|
+
end
|
|
834
|
+
|
|
835
|
+
2.times do
|
|
836
|
+
get '/plain'
|
|
837
|
+
assert_equal '<div><b>escaped</b></div>', body
|
|
838
|
+
end
|
|
839
|
+
|
|
840
|
+
2.times do
|
|
841
|
+
get '/safe'
|
|
842
|
+
assert_equal '<div><b>raw</b></div>', body
|
|
843
|
+
end
|
|
844
|
+
end
|
|
845
|
+
|
|
846
|
+
it 'should keep cached fragment escaping with File store' do
|
|
847
|
+
dir = Dir.mktmpdir('padrino-cache-frag-file')
|
|
848
|
+
begin
|
|
849
|
+
mock_app do
|
|
850
|
+
register Padrino::Cache
|
|
851
|
+
register Padrino::Helpers
|
|
852
|
+
enable :caching
|
|
853
|
+
set :cache, Padrino::Cache.new(:File, dir: dir)
|
|
854
|
+
get '/plain' do
|
|
855
|
+
erb '<div><%= cache(:plain_frag) { "<b>escaped</b>" } %></div>'
|
|
856
|
+
end
|
|
857
|
+
get '/safe' do
|
|
858
|
+
erb '<div><%= cache(:safe_frag) { "<b>raw</b>".html_safe } %></div>'
|
|
859
|
+
end
|
|
860
|
+
end
|
|
861
|
+
|
|
862
|
+
2.times do
|
|
863
|
+
get '/plain'
|
|
864
|
+
assert_equal '<div><b>escaped</b></div>', body
|
|
865
|
+
end
|
|
866
|
+
|
|
867
|
+
2.times do
|
|
868
|
+
get '/safe'
|
|
869
|
+
assert_equal '<div><b>raw</b></div>', body
|
|
870
|
+
end
|
|
871
|
+
|
|
872
|
+
refute_empty Dir.children(dir)
|
|
873
|
+
ensure
|
|
874
|
+
FileUtils.remove_entry(dir) if dir && Dir.exist?(dir)
|
|
875
|
+
end
|
|
876
|
+
end
|
|
877
|
+
|
|
878
|
+
it 'should re-escape cached html_safe fragment in a store that does not preserve string class' do
|
|
879
|
+
plain_store = Class.new(Hash) do
|
|
880
|
+
def store(key, value, _options = {})
|
|
881
|
+
super(key, value.is_a?(String) ? String.new(value) : value)
|
|
882
|
+
end
|
|
883
|
+
end.new
|
|
884
|
+
mock_app do
|
|
885
|
+
register Padrino::Cache
|
|
886
|
+
register Padrino::Helpers
|
|
887
|
+
enable :caching
|
|
888
|
+
set :cache, plain_store
|
|
889
|
+
get '/safe' do
|
|
890
|
+
erb '<div><%= cache(:plain_safe_frag) { "<b>ok</b>".html_safe } %></div>'
|
|
891
|
+
end
|
|
892
|
+
end
|
|
893
|
+
|
|
894
|
+
get '/safe'
|
|
895
|
+
assert_equal '<div><b>ok</b></div>', body
|
|
896
|
+
|
|
897
|
+
get '/safe'
|
|
898
|
+
assert_equal '<div><b>ok</b></div>', body
|
|
899
|
+
end
|
|
724
900
|
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.18.
|
|
4
|
+
version: 0.18.7
|
|
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: 2026-11-
|
|
14
|
+
date: 2026-11-24 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.18.
|
|
22
|
+
version: 0.18.7
|
|
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.18.
|
|
29
|
+
version: 0.18.7
|
|
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.18.
|
|
36
|
+
version: 0.18.7
|
|
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.18.
|
|
43
|
+
version: 0.18.7
|
|
44
44
|
- !ruby/object:Gem::Dependency
|
|
45
45
|
name: moneta
|
|
46
46
|
requirement: !ruby/object:Gem::Requirement
|