@@ -124,9 +124,9 @@ with the original keys `[ 2, 9, 6, 1 ]`:
124
124
125
125
## Caching
126
126
127
- DataLoader provides a cache for all loads which occur in a single request to
128
- your application. After ` .load() ` is called once with a given key, the resulting
129
- value is cached to eliminate redundant loads.
127
+ DataLoader provides a memoization cache for all loads which occur in a single
128
+ request to your application. After ` .load() ` is called once with a given key,
129
+ the resulting value is cached to eliminate redundant loads.
130
130
131
131
In addition to reliving pressure on your data storage, caching results per-request
132
132
also creates fewer objects which may relieve memory pressure on your application:
@@ -144,7 +144,7 @@ DataLoader caching *does not* replace Redis, Memcache, or any other shared
144
144
application-level cache. DataLoader is first and foremost a data loading mechanism,
145
145
and its cache only serves the purpose of not repeatedly loading the same data in
146
146
the context of a single request to your Application. To do this, it maintains a
147
- simple in-memory cache (more accurately: ` .load() ` is a memoized function).
147
+ simple in-memory memoization cache (more accurately: ` .load() ` is a memoized function).
148
148
149
149
Avoid multiple requests from different users using the DataLoader instance, which
150
150
could result in cached data incorrectly appearing in each request. Typically,
@@ -217,6 +217,46 @@ userLoader.load(1).catch(error => {
217
217
});
218
218
```
219
219
220
+ #### Disabling Cache
221
+
222
+ In certain uncommon cases, a DataLoader which * does not* cache may be desirable.
223
+ Calling ` new DataLoader(myBatchFn, { cache: false }) ` will ensure that every
224
+ call to ` .load() ` will produce a * new* Promise, and requested keys will not be
225
+ saved in memory.
226
+
227
+ However, when the memoization cache is disabled, your batch function will
228
+ receive an array of keys which may contain duplicates! Each key will be
229
+ associated with each call to ` .load() ` . Your batch loader should provide a value
230
+ for each instance of the requested key.
231
+
232
+ For example:
233
+
234
+ ``` js
235
+ var myLoader = new DataLoader (keys => {
236
+ console .log (keys)
237
+ return someBatchLoadFn (keys)
238
+ }, { cache: false })
239
+
240
+ myLoader .load (' A' )
241
+ myLoader .load (' B' )
242
+ myLoader .load (' A' )
243
+
244
+ // > [ 'A', 'B', 'A' ]
245
+ ```
246
+
247
+ More complex cache behavior can be achieved by calling ` .clear() ` or ` .clearAll() `
248
+ rather than disabling the cache completely. For example, this DataLoader will
249
+ provide unique keys to a batch function due to the memoization cache being
250
+ enabled, but will immediately clear its cache when the batch function is called
251
+ so later requests will load new values.
252
+
253
+ ``` js
254
+ var myLoader = new DataLoader (keys => {
255
+ identityLoader .clearAll ()
256
+ return someBatchLoadFn (keys)
257
+ })
258
+ ```
259
+
220
260
221
261
## API
222
262
@@ -245,8 +285,9 @@ Create a new `DataLoader` given a batch loading function and options.
245
285
- * maxBatchSize* : Default ` Infinity ` . Limits the number of items that get
246
286
passed in to the ` batchLoadFn ` .
247
287
248
- - * cache* : Default ` true ` . Set to ` false ` to disable caching, instead
249
- creating a new Promise and new key in the ` batchLoadFn ` for every load.
288
+ - * cache* : Default ` true ` . Set to ` false ` to disable memoization caching,
289
+ instead creating a new Promise and new key in the ` batchLoadFn ` for every
290
+ load of the same key.
250
291
251
292
- * cacheKeyFn* : A function to produce a cache key for a given load key.
252
293
Defaults to ` key => key ` . Useful to provide when JavaScript objects are keys
@@ -413,7 +454,7 @@ let usernameLoader = new DataLoader(names => genUsernames(names).then(users => {
413
454
## Custom Caches
414
455
415
456
DataLoader can optionaly be provided a custom Map instance to use as its
416
- cache. More specifically, any object that implements the methods ` get() ` ,
457
+ memoization cache. More specifically, any object that implements the methods ` get() ` ,
417
458
` set() ` , ` delete() ` and ` clear() ` can be provided. This allows for custom Maps
418
459
which implement various [ cache algorithms] [ ] to be provided. By default,
419
460
DataLoader uses the standard [ Map] [ ] which simply grows until the DataLoader
0 commit comments