async-http-cache 0.1.1
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 +7 -0
- data/.github/workflows/development.yml +34 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/Gemfile +4 -0
- data/README.md +68 -0
- data/Rakefile +6 -0
- data/async-http-cache.gemspec +33 -0
- data/lib/async/http/cache.rb +24 -0
- data/lib/async/http/cache/body.rb +55 -0
- data/lib/async/http/cache/general.rb +126 -0
- data/lib/async/http/cache/response.rb +83 -0
- data/lib/async/http/cache/store.rb +36 -0
- data/lib/async/http/cache/store/memory.rb +109 -0
- data/lib/async/http/cache/store/vary.rb +78 -0
- data/lib/async/http/cache/version.rb +29 -0
- metadata +156 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 923d97088cda9896dcec9c48c21b6142fc9002acfc15ae70a5cb6fac4eacf086
|
4
|
+
data.tar.gz: 9819dd06fa5f44a42c7b9f866477fa163df6e857ce0c5618306f5962f2bbfcc1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c1ffbd84fe0928014e500f0290a25720581e50ef688e30ad8161fd3b62ea7a6acc21fe2b40a12cf47ef0f3ec2e007e2613f63cb4fdf1518bf324ae88c63b58ad
|
7
|
+
data.tar.gz: e4ece204ed54c4d23c1e831d887ae2b7cb169e702b80e890f4bac5438f393fb31c15a873f494ac1ffcc1d3a88663b4eb12e0abb2c95c9fee4e4f030771158b8c
|
@@ -0,0 +1,34 @@
|
|
1
|
+
name: Development
|
2
|
+
|
3
|
+
on: [push, pull_request]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
test:
|
7
|
+
strategy:
|
8
|
+
matrix:
|
9
|
+
os:
|
10
|
+
- ubuntu
|
11
|
+
- macos
|
12
|
+
|
13
|
+
ruby:
|
14
|
+
- 2.4
|
15
|
+
- 2.5
|
16
|
+
- 2.6
|
17
|
+
- 2.7
|
18
|
+
|
19
|
+
include:
|
20
|
+
- os: 'ubuntu'
|
21
|
+
ruby: '2.6'
|
22
|
+
env: COVERAGE=PartialSummary,Coveralls
|
23
|
+
|
24
|
+
runs-on: ${{matrix.os}}-latest
|
25
|
+
|
26
|
+
steps:
|
27
|
+
- uses: actions/checkout@v1
|
28
|
+
- uses: ruby/setup-ruby@v1
|
29
|
+
with:
|
30
|
+
ruby-version: ${{matrix.ruby}}
|
31
|
+
- name: Install dependencies
|
32
|
+
run: bundle install
|
33
|
+
- name: Run tests
|
34
|
+
run: ${{matrix.env}} bundle exec rspec
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# Async::HTTP::Cache
|
2
|
+
|
3
|
+
Provides a cache middleware for `Async::HTTP` clients and servers.
|
4
|
+
|
5
|
+
[](https://github.com/socketry/async-http-cache/actions?workflow=Development)
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
### Client Side
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
require 'async'
|
13
|
+
require 'async/http'
|
14
|
+
require 'async/http/cache'
|
15
|
+
|
16
|
+
endpoint = Async::HTTP::Endpoint.parse("https://www.oriontransfer.co.nz")
|
17
|
+
client = Async::HTTP::Client.new(endpoint)
|
18
|
+
cache = Async::HTTP::Cache::General.new(client)
|
19
|
+
|
20
|
+
Async do
|
21
|
+
2.times do
|
22
|
+
response = cache.get("/products/index")
|
23
|
+
puts response.inspect
|
24
|
+
# <Async::HTTP::Protocol::HTTP2::Response ...>
|
25
|
+
# <Async::HTTP::Cache::Response ...>
|
26
|
+
response.finish
|
27
|
+
end
|
28
|
+
ensure
|
29
|
+
cache.close
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
## Vary
|
34
|
+
|
35
|
+
The `vary` header creates a headache for proxy implementations, because it creates a combinatorial explosion of cache keys, even if the content is the same. Try to avoid it unless absolutely necessary.
|
36
|
+
|
37
|
+
## Contributing
|
38
|
+
|
39
|
+
1. Fork it
|
40
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
41
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
42
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
43
|
+
5. Create new Pull Request
|
44
|
+
|
45
|
+
|
46
|
+
## License
|
47
|
+
|
48
|
+
Released under the MIT license.
|
49
|
+
|
50
|
+
Copyright, 2018, by [Samuel G. D. Williams](http://www.codeotaku.com/samuel-williams).
|
51
|
+
|
52
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
53
|
+
of this software and associated documentation files (the "Software"), to deal
|
54
|
+
in the Software without restriction, including without limitation the rights
|
55
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
56
|
+
copies of the Software, and to permit persons to whom the Software is
|
57
|
+
furnished to do so, subject to the following conditions:
|
58
|
+
|
59
|
+
The above copyright notice and this permission notice shall be included in
|
60
|
+
all copies or substantial portions of the Software.
|
61
|
+
|
62
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
63
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
64
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
65
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
66
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
67
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
68
|
+
THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
|
2
|
+
require_relative 'lib/async/http/cache/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |spec|
|
5
|
+
spec.name = "async-http-cache"
|
6
|
+
spec.version = Async::HTTP::Cache::VERSION
|
7
|
+
spec.authors = ["Samuel Williams"]
|
8
|
+
spec.email = ["samuel.williams@oriontransfer.co.nz"]
|
9
|
+
|
10
|
+
spec.summary = "Standard-compliant cache for async-http."
|
11
|
+
spec.homepage = "https://github.com/socketry/async-http-cache"
|
12
|
+
spec.license = "MIT"
|
13
|
+
|
14
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
15
|
+
|
16
|
+
# Specify which files should be added to the gem when it is released.
|
17
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
18
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
19
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
|
+
end
|
21
|
+
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_dependency "async-http"
|
25
|
+
spec.add_dependency "protocol-http", "~> 0.14.4"
|
26
|
+
|
27
|
+
spec.add_development_dependency "async-rspec", "~> 1.10"
|
28
|
+
|
29
|
+
spec.add_development_dependency "covered"
|
30
|
+
spec.add_development_dependency "bundler"
|
31
|
+
spec.add_development_dependency "rspec"
|
32
|
+
spec.add_development_dependency "rake"
|
33
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright, 2020, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
10
|
+
# furnished to do so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in
|
13
|
+
# all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
# THE SOFTWARE.
|
22
|
+
|
23
|
+
require_relative "cache/version"
|
24
|
+
require_relative "cache/general"
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
#
|
3
|
+
# Copyright, 2020, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
10
|
+
# furnished to do so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in
|
13
|
+
# all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
# THE SOFTWARE.
|
22
|
+
|
23
|
+
require 'protocol/http/body/rewindable'
|
24
|
+
require 'protocol/http/body/streamable'
|
25
|
+
|
26
|
+
module Async
|
27
|
+
module HTTP
|
28
|
+
module Cache
|
29
|
+
module Body
|
30
|
+
def self.wrap(message, &block)
|
31
|
+
if body = message.body
|
32
|
+
# Create a rewindable body wrapping the message body:
|
33
|
+
rewindable = ::Protocol::HTTP::Body::Rewindable.new(body)
|
34
|
+
|
35
|
+
# Set the message body to the rewindable body:
|
36
|
+
message.body = rewindable
|
37
|
+
|
38
|
+
# Wrap the message with the callback:
|
39
|
+
::Protocol::HTTP::Body::Streamable.wrap(message) do |error|
|
40
|
+
if error
|
41
|
+
Async.logger.error(self) {error}
|
42
|
+
else
|
43
|
+
yield message, rewindable.buffered
|
44
|
+
end
|
45
|
+
end
|
46
|
+
else
|
47
|
+
yield message, nil
|
48
|
+
end
|
49
|
+
|
50
|
+
return message
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,126 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright, 2020, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
10
|
+
# furnished to do so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in
|
13
|
+
# all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
# THE SOFTWARE.
|
22
|
+
|
23
|
+
require 'protocol/http/middleware'
|
24
|
+
|
25
|
+
require_relative 'body'
|
26
|
+
require_relative 'response'
|
27
|
+
require_relative 'store'
|
28
|
+
|
29
|
+
module Async
|
30
|
+
module HTTP
|
31
|
+
module Cache
|
32
|
+
class General < ::Protocol::HTTP::Middleware
|
33
|
+
CACHE_CONTROL = 'cache-control'
|
34
|
+
CONTENT_TYPE = 'content-type'
|
35
|
+
AUTHORIZATION = 'authorization'
|
36
|
+
COOKIE = 'cookie'
|
37
|
+
|
38
|
+
def initialize(app, store: Store.default)
|
39
|
+
super(app)
|
40
|
+
|
41
|
+
@count = 0
|
42
|
+
|
43
|
+
@store = store
|
44
|
+
end
|
45
|
+
|
46
|
+
attr :count
|
47
|
+
attr :store
|
48
|
+
|
49
|
+
def close
|
50
|
+
@store.close
|
51
|
+
ensure
|
52
|
+
super
|
53
|
+
end
|
54
|
+
|
55
|
+
def key(request)
|
56
|
+
@store.normalize(request)
|
57
|
+
|
58
|
+
[request.authority, request.method, request.path]
|
59
|
+
end
|
60
|
+
|
61
|
+
def cacheable?(request)
|
62
|
+
# We don't support caching requests which have a body:
|
63
|
+
if request.body
|
64
|
+
return false
|
65
|
+
end
|
66
|
+
|
67
|
+
# We can't cache upgraded requests:
|
68
|
+
if request.protocol
|
69
|
+
return false
|
70
|
+
end
|
71
|
+
|
72
|
+
# We only support caching GET and HEAD requests:
|
73
|
+
unless request.method == 'GET' || request.method == 'HEAD'
|
74
|
+
return false
|
75
|
+
end
|
76
|
+
|
77
|
+
if request.headers[AUTHORIZATION]
|
78
|
+
return false
|
79
|
+
end
|
80
|
+
|
81
|
+
if request.headers[COOKIE]
|
82
|
+
return false
|
83
|
+
end
|
84
|
+
|
85
|
+
# Otherwise, we can cache it:
|
86
|
+
return true
|
87
|
+
end
|
88
|
+
|
89
|
+
def wrap(key, request, response)
|
90
|
+
if response.status != 200
|
91
|
+
return response
|
92
|
+
end
|
93
|
+
|
94
|
+
return Body.wrap(response) do |message, body|
|
95
|
+
Async.logger.debug(self) {"Updating cache for #{key}..."}
|
96
|
+
@store.insert(key, request, Response.new(message, body))
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def call(request)
|
101
|
+
key = self.key(request)
|
102
|
+
|
103
|
+
cache_control = request.headers[CACHE_CONTROL]
|
104
|
+
|
105
|
+
unless cache_control&.no_cache?
|
106
|
+
if response = @store.lookup(key, request)
|
107
|
+
Async.logger.debug(self) {"Cache hit for #{key}..."}
|
108
|
+
@count += 1
|
109
|
+
|
110
|
+
# Create a dup of the response:
|
111
|
+
return response
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
unless cache_control&.no_store?
|
116
|
+
if cacheable?(request)
|
117
|
+
return wrap(key, request, super)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
return super
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright, 2020, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
10
|
+
# furnished to do so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in
|
13
|
+
# all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
# THE SOFTWARE.
|
22
|
+
|
23
|
+
require 'protocol/http/response'
|
24
|
+
require 'async/clock'
|
25
|
+
|
26
|
+
module Async
|
27
|
+
module HTTP
|
28
|
+
module Cache
|
29
|
+
class Response < ::Protocol::HTTP::Response
|
30
|
+
CACHE_CONTROL = 'cache-control'
|
31
|
+
SET_COOKIE = 'set-cookie'
|
32
|
+
|
33
|
+
def initialize(response, body)
|
34
|
+
@generated_at = Async::Clock.now
|
35
|
+
|
36
|
+
super(
|
37
|
+
response.version,
|
38
|
+
response.status,
|
39
|
+
response.headers.dup,
|
40
|
+
body,
|
41
|
+
response.protocol
|
42
|
+
)
|
43
|
+
|
44
|
+
@max_age = @headers[CACHE_CONTROL]&.max_age
|
45
|
+
end
|
46
|
+
|
47
|
+
attr :generated_at
|
48
|
+
|
49
|
+
def cachable?
|
50
|
+
if cache_control = @headers[CACHE_CONTROL]
|
51
|
+
if cache_control.private? || !cache_control.public?
|
52
|
+
return false
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
if set_cookie = @headers[SET_COOKIE]
|
57
|
+
Async.logger.warn(self) {"Cannot cache response with set-cookie header!"}
|
58
|
+
return false
|
59
|
+
end
|
60
|
+
|
61
|
+
return true
|
62
|
+
end
|
63
|
+
|
64
|
+
def age
|
65
|
+
Async::Clock.now - @generated_at
|
66
|
+
end
|
67
|
+
|
68
|
+
def expired?
|
69
|
+
self.age > @max_age
|
70
|
+
end
|
71
|
+
|
72
|
+
def dup
|
73
|
+
dup = super
|
74
|
+
|
75
|
+
dup.body = @body.dup
|
76
|
+
dup.headers = @headers.dup
|
77
|
+
|
78
|
+
return dup
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright, 2020, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
10
|
+
# furnished to do so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in
|
13
|
+
# all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
# THE SOFTWARE.
|
22
|
+
|
23
|
+
require_relative 'store/memory'
|
24
|
+
require_relative 'store/vary'
|
25
|
+
|
26
|
+
module Async
|
27
|
+
module HTTP
|
28
|
+
module Cache
|
29
|
+
module Store
|
30
|
+
def self.default
|
31
|
+
Vary.new(Memory.new)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright, 2020, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
10
|
+
# furnished to do so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in
|
13
|
+
# all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
# THE SOFTWARE.
|
22
|
+
|
23
|
+
module Async
|
24
|
+
module HTTP
|
25
|
+
module Cache
|
26
|
+
module Store
|
27
|
+
class Memory
|
28
|
+
def initialize(limit: 1024)
|
29
|
+
@index = {}
|
30
|
+
@limit = limit
|
31
|
+
|
32
|
+
@hit = 0
|
33
|
+
@miss = 0
|
34
|
+
@pruned = 0
|
35
|
+
|
36
|
+
@gardener = Async do |task|
|
37
|
+
while true
|
38
|
+
task.sleep(60)
|
39
|
+
|
40
|
+
pruned = self.prune
|
41
|
+
@pruned += pruned
|
42
|
+
|
43
|
+
Async.logger.debug(self) do |buffer|
|
44
|
+
if pruned > 0
|
45
|
+
buffer.puts "Pruned #{pruned} entries."
|
46
|
+
end
|
47
|
+
|
48
|
+
buffer.puts "Hits: #{@hit} Misses: #{@miss} Pruned: #{@pruned} Ratio: #{(100.0*@hit/@miss).round(2)}%"
|
49
|
+
|
50
|
+
body_usage = @index.sum{|key, value| value.body.length}
|
51
|
+
buffer.puts "Index size: #{@index.size} Memory usage: #{(body_usage / 1024.0**2).round(2)}MiB"
|
52
|
+
|
53
|
+
# @index.each do |key, value|
|
54
|
+
# buffer.puts "#{key.join('-')}: #{value.body.length}B"
|
55
|
+
# end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def close
|
62
|
+
@gardener.stop
|
63
|
+
end
|
64
|
+
|
65
|
+
attr :index
|
66
|
+
|
67
|
+
def lookup(key, request)
|
68
|
+
if response = @index[key]
|
69
|
+
if response.expired?
|
70
|
+
@index.delete(key)
|
71
|
+
|
72
|
+
@pruned += 1
|
73
|
+
|
74
|
+
return nil
|
75
|
+
end
|
76
|
+
|
77
|
+
@hit += 1
|
78
|
+
|
79
|
+
return response.dup
|
80
|
+
else
|
81
|
+
@miss += 1
|
82
|
+
|
83
|
+
return nil
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def insert(key, request, response)
|
88
|
+
if @index.size < @limit
|
89
|
+
if response.body.length < 1024*64
|
90
|
+
@index[key] = response
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
# @return [Integer] the number of pruned entries.
|
96
|
+
def prune
|
97
|
+
initial_count = @index.size
|
98
|
+
|
99
|
+
@index.delete_if do |key, value|
|
100
|
+
value.expired?
|
101
|
+
end
|
102
|
+
|
103
|
+
return initial_count - @index.size
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright, 2020, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
10
|
+
# furnished to do so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in
|
13
|
+
# all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
# THE SOFTWARE.
|
22
|
+
|
23
|
+
module Async
|
24
|
+
module HTTP
|
25
|
+
module Cache
|
26
|
+
module Store
|
27
|
+
VARY = 'vary'
|
28
|
+
ACCEPT_ENCODING = 'accept-encoding'
|
29
|
+
|
30
|
+
class Vary
|
31
|
+
def initialize(delegate, vary = {})
|
32
|
+
@delegate = delegate
|
33
|
+
@vary = vary
|
34
|
+
end
|
35
|
+
|
36
|
+
def close
|
37
|
+
@delegate.close
|
38
|
+
end
|
39
|
+
|
40
|
+
attr :delegate
|
41
|
+
|
42
|
+
def normalize(request)
|
43
|
+
if accept_encoding = request.headers[ACCEPT_ENCODING]
|
44
|
+
if accept_encoding.include?("gzip")
|
45
|
+
request.headers.set(ACCEPT_ENCODING, "gzip")
|
46
|
+
else
|
47
|
+
request.headers.delete(ACCEPT_ENCODING)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def key_for(headers, vary)
|
53
|
+
vary.map{|key| headers[key]}
|
54
|
+
end
|
55
|
+
|
56
|
+
def lookup(key, request)
|
57
|
+
if vary = @vary[key]
|
58
|
+
# We should provide user-supported normalization here:
|
59
|
+
key = key + key_for(request.headers, vary)
|
60
|
+
end
|
61
|
+
|
62
|
+
return @delegate.lookup(key, request)
|
63
|
+
end
|
64
|
+
|
65
|
+
def insert(key, request, response)
|
66
|
+
if vary = response.headers[VARY]&.sort
|
67
|
+
@vary[key] = vary
|
68
|
+
|
69
|
+
key = key + key_for(request.headers, vary)
|
70
|
+
end
|
71
|
+
|
72
|
+
@delegate.insert(key, request, response)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright, 2020, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
10
|
+
# furnished to do so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in
|
13
|
+
# all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
# THE SOFTWARE.
|
22
|
+
|
23
|
+
module Async
|
24
|
+
module HTTP
|
25
|
+
module Cache
|
26
|
+
VERSION = "0.1.1"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,156 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: async-http-cache
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Samuel Williams
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-03-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: async-http
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: protocol-http
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.14.4
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.14.4
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: async-rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.10'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.10'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: covered
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: bundler
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rake
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description:
|
112
|
+
email:
|
113
|
+
- samuel.williams@oriontransfer.co.nz
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".github/workflows/development.yml"
|
119
|
+
- ".gitignore"
|
120
|
+
- ".rspec"
|
121
|
+
- Gemfile
|
122
|
+
- README.md
|
123
|
+
- Rakefile
|
124
|
+
- async-http-cache.gemspec
|
125
|
+
- lib/async/http/cache.rb
|
126
|
+
- lib/async/http/cache/body.rb
|
127
|
+
- lib/async/http/cache/general.rb
|
128
|
+
- lib/async/http/cache/response.rb
|
129
|
+
- lib/async/http/cache/store.rb
|
130
|
+
- lib/async/http/cache/store/memory.rb
|
131
|
+
- lib/async/http/cache/store/vary.rb
|
132
|
+
- lib/async/http/cache/version.rb
|
133
|
+
homepage: https://github.com/socketry/async-http-cache
|
134
|
+
licenses:
|
135
|
+
- MIT
|
136
|
+
metadata: {}
|
137
|
+
post_install_message:
|
138
|
+
rdoc_options: []
|
139
|
+
require_paths:
|
140
|
+
- lib
|
141
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: 2.3.0
|
146
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
147
|
+
requirements:
|
148
|
+
- - ">="
|
149
|
+
- !ruby/object:Gem::Version
|
150
|
+
version: '0'
|
151
|
+
requirements: []
|
152
|
+
rubygems_version: 3.1.2
|
153
|
+
signing_key:
|
154
|
+
specification_version: 4
|
155
|
+
summary: Standard-compliant cache for async-http.
|
156
|
+
test_files: []
|