@@ -132,33 +132,79 @@ with the original keys `[ 2, 9, 6, 1 ]`:
132132
133133### Caching
134134
135- After being loaded once, the resulting value is cached, eliminating
136- redundant requests.
135+ DataLoader provides a cache for all loads which occur in a single request to
136+ your application. After ` .load() ` is called once with a given key, the resulting
137+ value is cached to eliminate redundant loads.
137138
138- In the example above, if User ` 1 ` was last invited by User ` 2 ` , only a single
139- round trip will occur.
140-
141- Caching results in creating fewer objects which may relieve memory pressure on
142- your application:
139+ In addition to reliving pressure on your data storage, caching results per-request
140+ also creates fewer objects which may relieve memory pressure on your application:
143141
144142``` js
143+ var userLoader = new DataLoader (... )
145144var promise1A = userLoader .load (1 )
146145var promise1B = userLoader .load (1 )
147146assert (promise1A === promise1B)
148147```
149148
149+ #### Caching per-Request
150+
151+ DataLoader caching * does not* replace Redis, Memcache, or any other shared
152+ application-level cache. DataLoader is first and foremost a data loading mechanism,
153+ and its cache only serves the purpose of not repeatedly loading the same data in
154+ the context of a single request to your Application. To do this, it maintains a
155+ simple in-memory cache (more accurately: ` .load() ` is a memoized function).
156+
157+ Avoid multiple requests from different users using the DataLoader instance, which
158+ could result in cached data incorrectly appearing in each request. Typically,
159+ DataLoader instances are created when a Request begins, and are not used once the
160+ Request ends.
161+
162+ For example, when using with ` express ` :
163+
164+ ``` js
165+ function createLoaders (authToken ) {
166+ return {
167+ users: new DataLoader (ids => genUsers (authToken, ids)),
168+ }
169+ }
170+
171+ var app = express ()
172+
173+ app .get (' /' , function (req , res ) {
174+ var authToken = authenticateUser (req)
175+ var loaders = createLoaders (authToken)
176+ res .send (renderPage (req, loaders))
177+ })
178+
179+ app .listen ()
180+ ```
181+
150182#### Clearing Cache
151183
184+ In certain uncommon cases, clearing the request cache may be necessary.
185+
152186The most common example when clearing the loader's cache is necessary is after
153- a mutation or update, when a cached value may be out of date and future loads
154- should not use any possibly cached value.
187+ a mutation or update within the same request , when a cached value could be out of
188+ date and future loads should not use any possibly cached value.
155189
156190Here's a simple example using SQL UPDATE to illustrate.
157191
158192``` js
193+ // Request begins...
194+ var userLoader = new DataLoader (... )
195+
196+ // And a value happens to be loaded (and cached).
197+ userLoader .load (4 ).then (... )
198+
199+ // A mutation occurs, invalidating what might be in cache.
159200sqlRun (' UPDATE users WHERE id=4 SET username="zuck"' ).then (
160201 () => userLoader .clear (4 )
161202)
203+
204+ // Later the value load is loaded again so the mutated data appears.
205+ userLoader .load (4 ).then (... )
206+
207+ // Request completes.
162208```
163209
164210#### Caching Errors
0 commit comments