async-http-cache 0.4.2 → 0.4.3
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
- checksums.yaml.gz.sig +2 -0
- data/lib/async/http/cache/general.rb +50 -29
- data/lib/async/http/cache/response.rb +1 -1
- data/lib/async/http/cache/store/memory.rb +4 -3
- data/lib/async/http/cache/version.rb +1 -1
- data.tar.gz.sig +0 -0
- metadata +34 -4
- metadata.gz.sig +2 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2439c0fb975f956215ef9eec1e8db38394e10608a4299253516fee12540b5e6f
|
4
|
+
data.tar.gz: 616e0a00b3e6fa00c85c29cede66c464b887ca8d56188e93b84cf6ec5d19f7a4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aadf8138fab28ffedeaa0a74f4dedf6f888d84394e4668e6d42757bf8c87e6bad50c9afbd7477d2a26e84294c4ce13db964f4a5567badd7a994434440c0e7a73
|
7
|
+
data.tar.gz: 976634254e072fe0ea9f950da879d75954b2b5b56f3de52b9e4d42c10a19b71ed840ddd3c61660f5fa9efaa39ed06fbc5ac24585ebe21615f8ff4d542fff7d93
|
checksums.yaml.gz.sig
ADDED
@@ -1,17 +1,17 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
# Copyright, 2020, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
4
|
-
#
|
4
|
+
#
|
5
5
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
6
|
# of this software and associated documentation files (the "Software"), to deal
|
7
7
|
# in the Software without restriction, including without limitation the rights
|
8
8
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
9
|
# copies of the Software, and to permit persons to whom the Software is
|
10
10
|
# furnished to do so, subject to the following conditions:
|
11
|
-
#
|
11
|
+
#
|
12
12
|
# The above copyright notice and this permission notice shall be included in
|
13
13
|
# all copies or substantial portions of the Software.
|
14
|
-
#
|
14
|
+
#
|
15
15
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
16
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
17
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
@@ -20,6 +20,7 @@
|
|
20
20
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
21
|
# THE SOFTWARE.
|
22
22
|
|
23
|
+
require 'set'
|
23
24
|
require 'protocol/http/middleware'
|
24
25
|
|
25
26
|
require_relative 'body'
|
@@ -31,102 +32,122 @@ module Async
|
|
31
32
|
module Cache
|
32
33
|
class General < ::Protocol::HTTP::Middleware
|
33
34
|
CACHE_CONTROL = 'cache-control'
|
34
|
-
|
35
|
+
|
35
36
|
CONTENT_TYPE = 'content-type'
|
36
37
|
AUTHORIZATION = 'authorization'
|
37
38
|
COOKIE = 'cookie'
|
38
|
-
|
39
|
+
|
40
|
+
# Status codes of responses that MAY be stored by a cache or used in reply
|
41
|
+
# to a subsequent request.
|
42
|
+
#
|
43
|
+
# http://tools.ietf.org/html/rfc2616#section-13.4
|
44
|
+
CACHEABLE_RESPONSE_CODES = {
|
45
|
+
200 => true, # OK
|
46
|
+
203 => true, # Non-Authoritative Information
|
47
|
+
300 => true, # Multiple Choices
|
48
|
+
301 => true, # Moved Permanently
|
49
|
+
302 => true, # Found
|
50
|
+
404 => true, # Not Found
|
51
|
+
410 => true # Gone
|
52
|
+
}.freeze
|
53
|
+
|
39
54
|
def initialize(app, store: Store.default)
|
40
55
|
super(app)
|
41
|
-
|
56
|
+
|
42
57
|
@count = 0
|
43
|
-
|
58
|
+
|
44
59
|
@store = store
|
45
60
|
end
|
46
|
-
|
61
|
+
|
47
62
|
attr :count
|
48
63
|
attr :store
|
49
|
-
|
64
|
+
|
50
65
|
def close
|
51
66
|
@store.close
|
52
67
|
ensure
|
53
68
|
super
|
54
69
|
end
|
55
|
-
|
70
|
+
|
56
71
|
def key(request)
|
57
72
|
@store.normalize(request)
|
58
|
-
|
73
|
+
|
59
74
|
[request.authority, request.method, request.path]
|
60
75
|
end
|
61
|
-
|
76
|
+
|
62
77
|
def cacheable?(request)
|
63
|
-
# We don't support caching requests which have a body:
|
78
|
+
# We don't support caching requests which have a request body:
|
64
79
|
if request.body
|
65
80
|
return false
|
66
81
|
end
|
67
|
-
|
82
|
+
|
68
83
|
# We can't cache upgraded requests:
|
69
84
|
if request.protocol
|
70
85
|
return false
|
71
86
|
end
|
72
|
-
|
87
|
+
|
73
88
|
# We only support caching GET and HEAD requests:
|
74
89
|
unless request.method == 'GET' || request.method == 'HEAD'
|
75
90
|
return false
|
76
91
|
end
|
77
|
-
|
92
|
+
|
78
93
|
if request.headers[AUTHORIZATION]
|
79
94
|
return false
|
80
95
|
end
|
81
|
-
|
96
|
+
|
82
97
|
if request.headers[COOKIE]
|
83
98
|
return false
|
84
99
|
end
|
85
|
-
|
100
|
+
|
86
101
|
# Otherwise, we can cache it:
|
87
102
|
return true
|
88
103
|
end
|
89
|
-
|
104
|
+
|
90
105
|
def wrap(key, request, response)
|
91
|
-
|
106
|
+
unless CACHEABLE_RESPONSE_CODES.include?(response.status)
|
92
107
|
return response
|
93
108
|
end
|
94
|
-
|
109
|
+
|
110
|
+
response_cache_control = response.headers[CACHE_CONTROL]
|
111
|
+
|
112
|
+
if response_cache_control&.no_store? || response_cache_control&.private?
|
113
|
+
return response
|
114
|
+
end
|
115
|
+
|
95
116
|
if request.head? and body = response.body
|
96
117
|
unless body.empty?
|
97
118
|
Console.logger.warn(self) {"HEAD request resulted in non-empty body!"}
|
98
|
-
|
119
|
+
|
99
120
|
return response
|
100
121
|
end
|
101
122
|
end
|
102
|
-
|
123
|
+
|
103
124
|
return Body.wrap(response) do |response, body|
|
104
125
|
Console.logger.debug(self) {"Updating cache for #{key}..."}
|
105
126
|
@store.insert(key, request, Response.new(response, body))
|
106
127
|
end
|
107
128
|
end
|
108
|
-
|
129
|
+
|
109
130
|
def call(request)
|
110
131
|
key = self.key(request)
|
111
|
-
|
132
|
+
|
112
133
|
cache_control = request.headers[CACHE_CONTROL]
|
113
|
-
|
134
|
+
|
114
135
|
unless cache_control&.no_cache?
|
115
136
|
if response = @store.lookup(key, request)
|
116
137
|
Console.logger.debug(self) {"Cache hit for #{key}..."}
|
117
138
|
@count += 1
|
118
|
-
|
139
|
+
|
119
140
|
# Return the cached response:
|
120
141
|
return response
|
121
142
|
end
|
122
143
|
end
|
123
|
-
|
144
|
+
|
124
145
|
unless cache_control&.no_store?
|
125
146
|
if cacheable?(request)
|
126
147
|
return wrap(key, request, super)
|
127
148
|
end
|
128
149
|
end
|
129
|
-
|
150
|
+
|
130
151
|
return super
|
131
152
|
end
|
132
153
|
end
|
@@ -25,9 +25,10 @@ module Async
|
|
25
25
|
module Cache
|
26
26
|
module Store
|
27
27
|
class Memory
|
28
|
-
def initialize(limit: 1024)
|
28
|
+
def initialize(limit: 1024, maximum_size: 1024*64, prune_interval: 60)
|
29
29
|
@index = {}
|
30
30
|
@limit = limit
|
31
|
+
@maximum_size = maximum_size
|
31
32
|
|
32
33
|
@hit = 0
|
33
34
|
@miss = 0
|
@@ -35,7 +36,7 @@ module Async
|
|
35
36
|
|
36
37
|
@gardener = Async(transient: true, annotation: self.class) do |task|
|
37
38
|
while true
|
38
|
-
task.sleep(
|
39
|
+
task.sleep(prune_interval)
|
39
40
|
|
40
41
|
pruned = self.prune
|
41
42
|
@pruned += pruned
|
@@ -96,7 +97,7 @@ module Async
|
|
96
97
|
def insert(key, request, response)
|
97
98
|
if @index.size < @limit
|
98
99
|
length = response.body&.length
|
99
|
-
if length.nil? or length <
|
100
|
+
if length.nil? or length < @maximum_size
|
100
101
|
@index[key] = response
|
101
102
|
end
|
102
103
|
end
|
data.tar.gz.sig
ADDED
Binary file
|
metadata
CHANGED
@@ -1,14 +1,44 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: async-http-cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Williams
|
8
|
+
- Olle Jonsson
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
|
-
cert_chain:
|
11
|
-
|
11
|
+
cert_chain:
|
12
|
+
- |
|
13
|
+
-----BEGIN CERTIFICATE-----
|
14
|
+
MIIE2DCCA0CgAwIBAgIBATANBgkqhkiG9w0BAQsFADBhMRgwFgYDVQQDDA9zYW11
|
15
|
+
ZWwud2lsbGlhbXMxHTAbBgoJkiaJk/IsZAEZFg1vcmlvbnRyYW5zZmVyMRIwEAYK
|
16
|
+
CZImiZPyLGQBGRYCY28xEjAQBgoJkiaJk/IsZAEZFgJuejAeFw0yMjA4MDYwNDUz
|
17
|
+
MjRaFw0zMjA4MDMwNDUzMjRaMGExGDAWBgNVBAMMD3NhbXVlbC53aWxsaWFtczEd
|
18
|
+
MBsGCgmSJomT8ixkARkWDW9yaW9udHJhbnNmZXIxEjAQBgoJkiaJk/IsZAEZFgJj
|
19
|
+
bzESMBAGCgmSJomT8ixkARkWAm56MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIB
|
20
|
+
igKCAYEAomvSopQXQ24+9DBB6I6jxRI2auu3VVb4nOjmmHq7XWM4u3HL+pni63X2
|
21
|
+
9qZdoq9xt7H+RPbwL28LDpDNflYQXoOhoVhQ37Pjn9YDjl8/4/9xa9+NUpl9XDIW
|
22
|
+
sGkaOY0eqsQm1pEWkHJr3zn/fxoKPZPfaJOglovdxf7dgsHz67Xgd/ka+Wo1YqoE
|
23
|
+
e5AUKRwUuvaUaumAKgPH+4E4oiLX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xt
|
38
|
+
Q2K9NVun/S785AP05vKkXZEFYxqG6EW012U4oLcFl5MySFajYXRYbuUpH6AY+HP8
|
39
|
+
voD0MPg1DssDLKwXyt1eKD/+Fq0bFWhwVM/1XiAXL7lyYUyOq24KHgQ2Csg=
|
40
|
+
-----END CERTIFICATE-----
|
41
|
+
date: 2022-08-23 00:00:00.000000000 Z
|
12
42
|
dependencies:
|
13
43
|
- !ruby/object:Gem::Dependency
|
14
44
|
name: async-http
|
@@ -99,7 +129,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
99
129
|
- !ruby/object:Gem::Version
|
100
130
|
version: '0'
|
101
131
|
requirements: []
|
102
|
-
rubygems_version: 3.3.
|
132
|
+
rubygems_version: 3.3.7
|
103
133
|
signing_key:
|
104
134
|
specification_version: 4
|
105
135
|
summary: Standard-compliant cache for async-http.
|
metadata.gz.sig
ADDED