api-blueprint 0.8.2 → 0.9.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 +21 -0
- data/lib/api-blueprint/cache.rb +7 -1
- data/lib/api-blueprint/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 61f08022e66a80930b38d402669208101268a8c1
|
4
|
+
data.tar.gz: 42f56d78286f6b1f0af2ff56d35ae3987790326c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e47571b0c8cb9454b4a43e1a8220743d9878d547c55528c34e764912a387aa2373273f518e42476652a1c638666c7f0dd298450f86a2dd7fd65658b620d170c3
|
7
|
+
data.tar.gz: b97f2796c2d0e3b100d051792b7cb50c4b93bd9fbfc5bc46ed17e61417c531d88a80a75d8b79749802d2638acdb3db8d68286ebb1714e174b38793b2a12040f2
|
data/README.md
CHANGED
@@ -242,6 +242,27 @@ class AstronautsInSpace < ApiBlueprint::Model
|
|
242
242
|
end
|
243
243
|
```
|
244
244
|
|
245
|
+
## Caching
|
246
|
+
|
247
|
+
ApiBlueprint includes the ability to cache responses and avoid numerous api calls to endpoints, but does not implement a caching mechanism itself. Instead it exposes a skeleton cache class which you can override with your own caching mechanism. See the [Rails cache example](https://github.com/iZettle/api-blueprint/blob/master/examples/cache.rb) for an example implementation using `Rails.cache.write`, `Rails.cache.read`, etc.
|
248
|
+
|
249
|
+
Caching is enabled on the runner level. In this case, using the Rails session id to make the cache unique to each user:
|
250
|
+
|
251
|
+
```ruby
|
252
|
+
ApiBlueprint::Runner.new({
|
253
|
+
cache: BlueprintCache.new(key: session.id)
|
254
|
+
})
|
255
|
+
```
|
256
|
+
|
257
|
+
The `ApiBlueprint::Cache` class has a method to generate unique keys for the cache items by creating a checksum of the request headers and url. It doesn't include the body of the request in this checksum by default, and if you want to exclude more headers, you can do so using the `ignored_headers` setting on the Cache class. For example, to not include "X-Real-IP" and "X-Request-Id" headers, which would otherwise render the cache useless:
|
258
|
+
|
259
|
+
```ruby
|
260
|
+
ApiBlueprint::Cache.configure do |config|
|
261
|
+
# Using .concat here because the default is [:body] and you probably want to keep that
|
262
|
+
config.ignored_headers.concat ["X-Real-IP", "X-Request-Id"]
|
263
|
+
end
|
264
|
+
```
|
265
|
+
|
245
266
|
## A note on Dry::Struct immutability
|
246
267
|
|
247
268
|
Models you create use `Dry::Struct` to handle initialization and assignment. `Dry::Struct` is designed with immutability in mind, so if you need to mutate the objects you have, there are two possibilities; explicitly define an `attr_writer` for the attributes which you want to mutate, or do things the "Dry::Struct way" and use the current instance to initialize a new instance:
|
data/lib/api-blueprint/cache.rb
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
module ApiBlueprint
|
2
2
|
class Cache
|
3
3
|
extend Dry::Initializer
|
4
|
+
extend Dry::Configurable
|
5
|
+
|
6
|
+
setting :ignored_headers, [:body]
|
4
7
|
|
5
8
|
option :key
|
6
9
|
|
@@ -17,7 +20,10 @@ module ApiBlueprint
|
|
17
20
|
end
|
18
21
|
|
19
22
|
def generate_cache_key(klass, options)
|
20
|
-
|
23
|
+
if options.is_a? Hash
|
24
|
+
options = options.clone.with_indifferent_access.except *self.class.config.ignored_headers
|
25
|
+
end
|
26
|
+
|
21
27
|
options_digest = Digest::MD5.hexdigest Marshal::dump(options.to_s.chars.sort.join)
|
22
28
|
"#{key}:#{klass&.name}:#{options_digest}"
|
23
29
|
end
|