padrino-cache 0.18.6 → 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/lib/padrino-cache/helpers/page.rb +5 -1
- data/lib/padrino-cache.rb +4 -0
- data/test/test_moneta_store.rb +46 -0
- data/test/test_padrino_cache.rb +78 -0
- metadata +5 -5
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
|
|
@@ -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]
|
|
@@ -147,6 +148,9 @@ 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
|
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
|
@@ -578,6 +578,84 @@ describe 'PadrinoCache' do
|
|
|
578
578
|
assert_equal 3, call_count
|
|
579
579
|
end
|
|
580
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
|
+
|
|
581
659
|
it 'should raise an error if providing both a cache_key and block' do
|
|
582
660
|
mock_app do
|
|
583
661
|
register Padrino::Cache
|
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
|
|
@@ -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
|