response_bank 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +37 -2
- data/lib/response_bank.rb +8 -0
- data/lib/response_bank/middleware.rb +7 -2
- data/lib/response_bank/response_cache_handler.rb +1 -1
- data/lib/response_bank/version.rb +1 -1
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2a5d65c4fd4310708e807cde576efeebdc590c16eb8e16190aef77a715ed48c2
|
4
|
+
data.tar.gz: 498da7cd7cfcc2da09b517e6694093551e79288ec4e52e815950448353ba4a0f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5985c959c55ebf8917d9687dd0e3c3d82cae36684f7466d7c1d1805097da2fd57cadaa6efa9f7d618a757e24949a7e493aefe0e298f5c0254d9b595f7871bba8
|
7
|
+
data.tar.gz: 5bc8ac60f2d2ce149120a8f0bc98aca57899511359f5a67a125b6cc3894edf0043dffad8be635ae9fa95a9d1b295220a091b0d2faa118982f9740eac5e3d6786
|
data/README.md
CHANGED
@@ -22,7 +22,32 @@ This gem supports the following versions of Ruby and Rails:
|
|
22
22
|
gem 'response_bank'
|
23
23
|
```
|
24
24
|
|
25
|
-
2.
|
25
|
+
2. add an initializer file. We need to configure the `acquire_lock` method, set the cache store and the logger
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
require 'response_bank'
|
29
|
+
|
30
|
+
module ResponseBank
|
31
|
+
LOCK_TTL = 90
|
32
|
+
|
33
|
+
class << self
|
34
|
+
def acquire_lock(cache_key)
|
35
|
+
cache_store.write("#{cache_key}:lock", '1', unless_exist: true, expires_in: LOCK_TTL, raw: true)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
ResponseBank.cache_store = ActiveSupport::Cache.lookup_store(Rails.configuration.cache_store)
|
41
|
+
ResponseBank.logger = Rails.logger
|
42
|
+
|
43
|
+
```
|
44
|
+
|
45
|
+
3. enables caching on your application
|
46
|
+
```ruby
|
47
|
+
config.action_controller.perform_caching = true
|
48
|
+
```
|
49
|
+
|
50
|
+
4. use `#response_cache` method to any desired controller's action
|
26
51
|
|
27
52
|
```ruby
|
28
53
|
class PostsController < ApplicationController
|
@@ -35,7 +60,17 @@ class PostsController < ApplicationController
|
|
35
60
|
end
|
36
61
|
```
|
37
62
|
|
38
|
-
|
63
|
+
5. **(optional)** set a custom TTL for the cache by overriding the `write_to_backing_cache_store` method in your initializer file
|
64
|
+
```ruby
|
65
|
+
module ResponseBank
|
66
|
+
CACHE_TTL = 30.minutes
|
67
|
+
def write_to_backing_cache_store(_env, key, payload, expires_in: CACHE_TTL)
|
68
|
+
cache_store.write(key, payload, raw: true, expires_in: expires_in)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
```
|
72
|
+
|
73
|
+
6. **(optional)** override custom cache key data. For default, cache key is defined by URL and query string
|
39
74
|
|
40
75
|
```ruby
|
41
76
|
class PostsController < ApplicationController
|
data/lib/response_bank.rb
CHANGED
@@ -21,6 +21,14 @@ module ResponseBank
|
|
21
21
|
yield
|
22
22
|
end
|
23
23
|
|
24
|
+
def write_to_backing_cache_store(_env, key, payload, expires_in: nil)
|
25
|
+
cache_store.write(key, payload, raw: true, expires_in: expires_in)
|
26
|
+
end
|
27
|
+
|
28
|
+
def read_from_backing_cache_store(_env, cache_key, backing_cache_store: cache_store)
|
29
|
+
backing_cache_store.read(cache_key, raw: true)
|
30
|
+
end
|
31
|
+
|
24
32
|
def compress(content)
|
25
33
|
io = StringIO.new
|
26
34
|
gz = Zlib::GzipWriter.new(io)
|
@@ -44,10 +44,15 @@ module ResponseBank
|
|
44
44
|
|
45
45
|
ResponseBank.write_to_cache(env['cacheable.key']) do
|
46
46
|
payload = MessagePack.dump(cache_data)
|
47
|
-
ResponseBank.
|
47
|
+
ResponseBank.write_to_backing_cache_store(
|
48
|
+
env,
|
49
|
+
env['cacheable.key'],
|
50
|
+
payload,
|
51
|
+
expires_in: env['cacheable.versioned-cache-expiry'],
|
52
|
+
)
|
48
53
|
|
49
54
|
if env['cacheable.unversioned-key']
|
50
|
-
ResponseBank.
|
55
|
+
ResponseBank.write_to_backing_cache_store(env, env['cacheable.unversioned-key'], payload)
|
51
56
|
end
|
52
57
|
end
|
53
58
|
|
@@ -127,7 +127,7 @@ module ResponseBank
|
|
127
127
|
end
|
128
128
|
|
129
129
|
def serve_from_cache(cache_key_hash, message, cache_age_tolerance = nil)
|
130
|
-
raw =
|
130
|
+
raw = ResponseBank.read_from_backing_cache_store(@env, cache_key_hash, backing_cache_store: @cache_store)
|
131
131
|
|
132
132
|
if raw
|
133
133
|
hit = MessagePack.load(raw)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: response_bank
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tobias Lütke
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2021-08-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: useragent
|
@@ -129,7 +129,8 @@ files:
|
|
129
129
|
homepage: ''
|
130
130
|
licenses:
|
131
131
|
- MIT
|
132
|
-
metadata:
|
132
|
+
metadata:
|
133
|
+
allowed_push_host: https://rubygems.org
|
133
134
|
post_install_message:
|
134
135
|
rdoc_options: []
|
135
136
|
require_paths:
|
@@ -145,7 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
145
146
|
- !ruby/object:Gem::Version
|
146
147
|
version: '0'
|
147
148
|
requirements: []
|
148
|
-
rubygems_version: 3.
|
149
|
+
rubygems_version: 3.2.20
|
149
150
|
signing_key:
|
150
151
|
specification_version: 4
|
151
152
|
summary: Simple response caching for Ruby applications
|