@@ -17,7 +17,7 @@ Include this repo in your `Package.swift` file.
1717To get started, create a DataLoader. Each DataLoader instance represents a unique cache. Typically instances are created per request when used
1818within a web-server if different users can see different things.
1919
20- ## Batching
20+ ## Batching 🍪
2121Batching is not an advanced feature, it's DataLoader's primary feature.
2222
2323Create a DataLoader by providing a batch loading function:
@@ -49,7 +49,7 @@ try userLoader.loadMany(keys: [1, 2, 3], on: eventLoopGroup)
4949```
5050
5151### 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
5353to running the ` batchLoadFunction ` and completing the ` load ` futures. This is to let keys accumulate and
5454batch into a smaller number of total requests. This amount of time is configurable using the ` executionPeriod `
5555option:
@@ -70,15 +70,14 @@ If desired, you can manually execute the `batchLoadFunction` and complete the fu
7070` .execute() ` method.
7171
7272Scheduled 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!
7474
7575### 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.
7878
7979
80- ## Caching
81-
80+ ## Caching 💰
8281DataLoader provides a memoization cache. After ` .load() ` is called with a key, the resulting value is cached
8382for the lifetime of the DataLoader object. This eliminates redundant loads.
8483
@@ -89,7 +88,7 @@ relieve memory pressure on your application:
8988let userLoader = DataLoader< Int , Int > (... )
9089let future1 = userLoader.load (key : 1 , on : eventLoopGroup)
9190let future2 = userLoader.load (key : 1 , on : eventLoopGroup)
92- assert (future1 === future2)
91+ print (future1 == future2) // true
9392```
9493
9594### Caching per-Request
@@ -123,7 +122,7 @@ let userLoader = DataLoader<Int, Int>(...)
123122userLoader.load (key : 4 , on : eventLoopGroup)
124123
125124// 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 ) }
127126
128127// Later the value load is loaded again so the mutated data appears.
129128userLoader.load (key : 4 , on : eventLoopGroup)
@@ -141,7 +140,7 @@ be cached to avoid frequently loading the same `Error`.
141140In some circumstances you may wish to clear the cache for these individual Errors:
142141
143142``` swift
144- userLoader.load (key : 1 , on : eventLoopGroup).catch { error in {
143+ userLoader.load (key : 1 , on : eventLoopGroup).whenFailure { error in
145144 if (/* determine if should clear error */ ) {
146145 userLoader.clear (key : 1 );
147146 }
@@ -191,7 +190,7 @@ let myLoader = DataLoader<String, String>(batchLoadFunction: { keys in
191190})
192191```
193192
194- ## Using with GraphQL
193+ ## Using with GraphQL 🎀
195194
196195DataLoader pairs nicely well with [ GraphQL] ( https://github.com/GraphQLSwift/GraphQL ) and
197196[ Graphiti] ( https://github.com/GraphQLSwift/Graphiti ) . GraphQL fields are designed to be
@@ -219,7 +218,7 @@ Consider the following GraphQL request:
219218```
220219
221220Naively, 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!
223222
224223By using DataLoader, we could batch our requests to a ` User ` type, and
225224only require at most 4 database requests, and possibly fewer if there are cache hits.
@@ -252,8 +251,8 @@ struct UserResolver {
252251
253252class UserContext {
254253 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
257256 keys.map { key in
258257 users.first { $0 .id == key }!
259258 }
@@ -284,8 +283,8 @@ struct UserAPI : API {
284283All your feedback and help to improve this project is very welcome. Please create issues for your bugs, ideas and
285284enhancement requests, or better yet, contribute directly by creating a PR. 😎
286285
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. 💥
289288
290289When creating a pull request, please adhere to the current coding style where possible, and create tests with your
291290code so it keeps providing an awesome test coverage level 💪
0 commit comments