@@ -17,7 +17,7 @@ Include this repo in your `Package.swift` file.
17
17
To get started, create a DataLoader. Each DataLoader instance represents a unique cache. Typically instances are created per request when used
18
18
within a web-server if different users can see different things.
19
19
20
- ## Batching
20
+ ## Batching 🍪
21
21
Batching is not an advanced feature, it's DataLoader's primary feature.
22
22
23
23
Create a DataLoader by providing a batch loading function:
@@ -49,7 +49,7 @@ try userLoader.loadMany(keys: [1, 2, 3], on: eventLoopGroup)
49
49
```
50
50
51
51
### Execution
52
- By default, a DataLoader will wait for a short time (2ms) from the moment ` load ` is called to collect keys prior
52
+ By default, a DataLoader will wait for a short time from the moment ` load ` is called to collect keys prior
53
53
to running the ` batchLoadFunction ` and completing the ` load ` futures. This is to let keys accumulate and
54
54
batch into a smaller number of total requests. This amount of time is configurable using the ` executionPeriod `
55
55
option:
@@ -70,15 +70,14 @@ If desired, you can manually execute the `batchLoadFunction` and complete the fu
70
70
` .execute() ` method.
71
71
72
72
Scheduled execution can be disabled by setting ` executionPeriod ` to ` nil ` , but be careful - you * must* call ` .execute() `
73
- manually in this case. Otherwise, the futures will never complete.
73
+ manually in this case. Otherwise, the futures will never complete!
74
74
75
75
### Disable batching
76
- It is possible to disable batching by setting the ` batchingEnabled ` option to ` false `
77
- It will invoke the ` batchLoadFunction ` immediately when a key is loaded.
76
+ It is possible to disable batching by setting the ` batchingEnabled ` option to ` false ` .
77
+ In this case, the ` batchLoadFunction ` will be invoked immediately when a key is loaded.
78
78
79
79
80
- ## Caching
81
-
80
+ ## Caching 💰
82
81
DataLoader provides a memoization cache. After ` .load() ` is called with a key, the resulting value is cached
83
82
for the lifetime of the DataLoader object. This eliminates redundant loads.
84
83
@@ -89,7 +88,7 @@ relieve memory pressure on your application:
89
88
let userLoader = DataLoader< Int , Int > (... )
90
89
let future1 = userLoader.load (key : 1 , on : eventLoopGroup)
91
90
let future2 = userLoader.load (key : 1 , on : eventLoopGroup)
92
- assert (future1 === future2)
91
+ print (future1 == future2) // true
93
92
```
94
93
95
94
### Caching per-Request
@@ -123,7 +122,7 @@ let userLoader = DataLoader<Int, Int>(...)
123
122
userLoader.load (key : 4 , on : eventLoopGroup)
124
123
125
124
// A mutation occurs, invalidating what might be in cache.
126
- sqlRun ('UPDATE users WHERE id= 4 SET username= " zuck" ').then { userLoader.clear (4 ) }
125
+ sqlRun ('UPDATE users WHERE id= 4 SET username= " zuck" ').whenComplete { userLoader.clear (key : 4 ) }
127
126
128
127
// Later the value load is loaded again so the mutated data appears.
129
128
userLoader.load (key : 4 , on : eventLoopGroup)
@@ -141,7 +140,7 @@ be cached to avoid frequently loading the same `Error`.
141
140
In some circumstances you may wish to clear the cache for these individual Errors:
142
141
143
142
``` swift
144
- userLoader.load (key : 1 , on : eventLoopGroup).catch { error in {
143
+ userLoader.load (key : 1 , on : eventLoopGroup).whenFailure { error in
145
144
if (/* determine if should clear error */ ) {
146
145
userLoader.clear (key : 1 );
147
146
}
@@ -191,7 +190,7 @@ let myLoader = DataLoader<String, String>(batchLoadFunction: { keys in
191
190
})
192
191
```
193
192
194
- ## Using with GraphQL
193
+ ## Using with GraphQL 🎀
195
194
196
195
DataLoader pairs nicely well with [ GraphQL] ( https://github.com/GraphQLSwift/GraphQL ) and
197
196
[ Graphiti] ( https://github.com/GraphQLSwift/Graphiti ) . GraphQL fields are designed to be
@@ -219,7 +218,7 @@ Consider the following GraphQL request:
219
218
```
220
219
221
220
Naively, if ` me ` , ` bestFriend ` and ` friends ` each need to request the backend,
222
- there could be at most 13 database requests!
221
+ there could be at most 12 database requests!
223
222
224
223
By using DataLoader, we could batch our requests to a ` User ` type, and
225
224
only require at most 4 database requests, and possibly fewer if there are cache hits.
@@ -252,8 +251,8 @@ struct UserResolver {
252
251
253
252
class UserContext {
254
253
let database = ...
255
- let userLoader = DataLoader< Int , User> () { keys in
256
- return User.query (on : database).filter (\.$id ~~ keys).all ().map { users in
254
+ let userLoader = DataLoader< Int , User> () { [ unowned self ] keys in
255
+ return User.query (on : self . database ).filter (\.$id ~~ keys).all ().map { users in
257
256
keys.map { key in
258
257
users.first { $0 .id == key }!
259
258
}
@@ -284,8 +283,8 @@ struct UserAPI : API {
284
283
All your feedback and help to improve this project is very welcome. Please create issues for your bugs, ideas and
285
284
enhancement requests, or better yet, contribute directly by creating a PR. 😎
286
285
287
- When reporting an issue, please add a detailed instruction , and if possible a code snippet or test that can be used
288
- as a reproducer of your problem. 💥
286
+ When reporting an issue, please add a detailed example , and if possible a code snippet or test
287
+ to reproduce your problem. 💥
289
288
290
289
When creating a pull request, please adhere to the current coding style where possible, and create tests with your
291
290
code so it keeps providing an awesome test coverage level 💪
0 commit comments