async-http-cache 0.4.0 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 17173f66af03c869ff56b8dd197565e230d10899eee2d3da2f18685ca670e892
4
- data.tar.gz: 1d6b1a438c596bdca021820f69dd86f02bcca593ffd771ab7912feb8f2f530a4
3
+ metadata.gz: 2439c0fb975f956215ef9eec1e8db38394e10608a4299253516fee12540b5e6f
4
+ data.tar.gz: 616e0a00b3e6fa00c85c29cede66c464b887ca8d56188e93b84cf6ec5d19f7a4
5
5
  SHA512:
6
- metadata.gz: 43ab1d479efa3d86814c73241ba78281ea3ce7fa125fde7393556ba1529192608da09979dc6e895e81621531beaf132195fb993d72638a35c38489e4f6436f37
7
- data.tar.gz: 79e4aaf2b574eab58253a92e5dc55b0f526733ad3335f035f3b07b91a3f1e54bc4e039e047d1c31da4484e41c132607888edc4c44ef8734bdb31c4ee87a86d8a
6
+ metadata.gz: aadf8138fab28ffedeaa0a74f4dedf6f888d84394e4668e6d42757bf8c87e6bad50c9afbd7477d2a26e84294c4ce13db964f4a5567badd7a994434440c0e7a73
7
+ data.tar.gz: 976634254e072fe0ea9f950da879d75954b2b5b56f3de52b9e4d42c10a19b71ed840ddd3c61660f5fa9efaa39ed06fbc5ac24585ebe21615f8ff4d542fff7d93
checksums.yaml.gz.sig ADDED
@@ -0,0 +1,2 @@
1
+ .,-�t�Q����V <#>A�i,��i��J@���ك�l� f�>b�"U�*�), v��%Ou���k_�\��:�����
2
+ �=���ղ`+��7�)��l�Q��:�Zx���
@@ -53,7 +53,7 @@ module Async
53
53
  # Wrap the response with the callback:
54
54
  ::Protocol::HTTP::Body::Completable.wrap(response) do |error|
55
55
  if error
56
- Async.logger.error(self) {error}
56
+ Console.logger.error(self) {error}
57
57
  else
58
58
  yield response, rewindable.buffered
59
59
  end
@@ -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
- if response.status != 200
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
- Async.logger.warn(self) {"HEAD request resulted in non-empty body!"}
98
-
118
+ Console.logger.warn(self) {"HEAD request resulted in non-empty body!"}
119
+
99
120
  return response
100
121
  end
101
122
  end
102
-
123
+
103
124
  return Body.wrap(response) do |response, body|
104
- Async.logger.debug(self) {"Updating cache for #{key}..."}
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
- Async.logger.debug(self) {"Cache hit for #{key}..."}
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
@@ -56,7 +56,7 @@ module Async
56
56
  @etag ||= @headers[ETAG]
57
57
  end
58
58
 
59
- def cachable?
59
+ def cacheable?
60
60
  if cache_control = @headers[CACHE_CONTROL]
61
61
  if cache_control.private? || !cache_control.public?
62
62
  return false
@@ -67,7 +67,7 @@ module Async
67
67
  end
68
68
 
69
69
  if set_cookie = @headers[SET_COOKIE]
70
- Async.logger.warn(self) {"Cannot cache response with set-cookie header!"}
70
+ Console.logger.warn(self) {"Cannot cache response with set-cookie header!"}
71
71
  return false
72
72
  end
73
73
 
@@ -25,22 +25,23 @@ 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
34
35
  @pruned = 0
35
36
 
36
- @gardener = Async do |task|
37
+ @gardener = Async(transient: true, annotation: self.class) do |task|
37
38
  while true
38
- task.sleep(60)
39
+ task.sleep(prune_interval)
39
40
 
40
41
  pruned = self.prune
41
42
  @pruned += pruned
42
43
 
43
- Async.logger.debug(self) do |buffer|
44
+ Console.logger.debug(self) do |buffer|
44
45
  if pruned > 0
45
46
  buffer.puts "Pruned #{pruned} entries."
46
47
  end
@@ -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 < 1024*64
100
+ if length.nil? or length < @maximum_size
100
101
  @index[key] = response
101
102
  end
102
103
  end
@@ -23,7 +23,7 @@
23
23
  module Async
24
24
  module HTTP
25
25
  module Cache
26
- VERSION = "0.4.0"
26
+ VERSION = "0.4.3"
27
27
  end
28
28
  end
29
29
  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.0
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
- date: 2021-04-23 00:00:00.000000000 Z
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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R6lvkGYQ2MYskBveyaxt
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
@@ -52,20 +82,6 @@ dependencies:
52
82
  - - ">="
53
83
  - !ruby/object:Gem::Version
54
84
  version: '0'
55
- - !ruby/object:Gem::Dependency
56
- name: bundler
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
85
  - !ruby/object:Gem::Dependency
70
86
  name: rspec
71
87
  requirement: !ruby/object:Gem::Requirement
@@ -80,33 +96,12 @@ dependencies:
80
96
  - - ">="
81
97
  - !ruby/object:Gem::Version
82
98
  version: '0'
83
- - !ruby/object:Gem::Dependency
84
- name: bake-bundler
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
99
  description:
98
100
  email:
99
- - samuel.williams@oriontransfer.co.nz
100
101
  executables: []
101
102
  extensions: []
102
103
  extra_rdoc_files: []
103
104
  files:
104
- - ".github/workflows/development.yml"
105
- - ".gitignore"
106
- - ".rspec"
107
- - Gemfile
108
- - README.md
109
- - async-http-cache.gemspec
110
105
  - lib/async/http/cache.rb
111
106
  - lib/async/http/cache/body.rb
112
107
  - lib/async/http/cache/general.rb
@@ -134,7 +129,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
134
129
  - !ruby/object:Gem::Version
135
130
  version: '0'
136
131
  requirements: []
137
- rubygems_version: 3.1.2
132
+ rubygems_version: 3.3.7
138
133
  signing_key:
139
134
  specification_version: 4
140
135
  summary: Standard-compliant cache for async-http.
metadata.gz.sig ADDED
@@ -0,0 +1,2 @@
1
+ ?���|$���Ŗ�~�;�r�k�p�񳄉���m��C@�Cѧ/�Bθ�_�ý�X��;�z��|����q]`��.�C���v2�yJ:���.7n�c�*���j����^/>���m����O� F��۴�&��O�����@�aj�N0ޯG8��]EUJz�嗒1��R�j���XSt.��i�ve�Z),�ő(�3���H�V����m��/�e��=��ek�,�
2
+ P�<�P;�|��1�*7[�n,�?�/����beq�o�x�q�ð�y4��,
@@ -1,33 +0,0 @@
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.5
15
- - 2.6
16
- - 2.7
17
-
18
- include:
19
- - os: 'ubuntu'
20
- ruby: '2.6'
21
- env: COVERAGE=PartialSummary,Coveralls
22
-
23
- runs-on: ${{matrix.os}}-latest
24
-
25
- steps:
26
- - uses: actions/checkout@v1
27
- - uses: ruby/setup-ruby@v1
28
- with:
29
- ruby-version: ${{matrix.ruby}}
30
- - name: Install dependencies
31
- run: bundle install
32
- - name: Run tests
33
- run: ${{matrix.env}} bundle exec rspec
data/.gitignore DELETED
@@ -1,13 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /_yardoc/
4
- /coverage/
5
- /doc/
6
- /pkg/
7
- /spec/reports/
8
- /tmp/
9
-
10
- # rspec failure tracking
11
- .rspec_status
12
- Gemfile.lock
13
- .covered.db
data/.rspec DELETED
@@ -1,3 +0,0 @@
1
- --format documentation
2
- --warnings
3
- --require spec_helper
data/Gemfile DELETED
@@ -1,8 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- # Specify your gem's dependencies in async-http-cache.gemspec
4
- gemspec
5
-
6
- # gem "async-http", path: "../async-http"
7
- # gem "protocol-http", path: "../protocol-http"
8
- # gem "protocol-http1", path: "../protocol-http1"
data/README.md DELETED
@@ -1,68 +0,0 @@
1
- # Async::HTTP::Cache
2
-
3
- Provides a cache middleware for `Async::HTTP` clients and servers.
4
-
5
- [![Development](https://github.com/socketry/async-http-cache/workflows/Development/badge.svg?branch=master)](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.
@@ -1,32 +0,0 @@
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", "~> 0.56"
25
-
26
- spec.add_development_dependency "async-rspec", "~> 1.10"
27
-
28
- spec.add_development_dependency "covered"
29
- spec.add_development_dependency "bundler"
30
- spec.add_development_dependency "rspec"
31
- spec.add_development_dependency "bake-bundler"
32
- end