Skip to content

Commit d30b50b

Browse files
Brian DeteringBrian Detering
authored andcommitted
additional docs
1 parent d3e7357 commit d30b50b

File tree

1 file changed

+72
-3
lines changed

1 file changed

+72
-3
lines changed

README.md

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
# Redis Dataloader
22

3-
Batching and Caching layer based on the [Facebook Dataloader](https://github.com/facebook/dataloader) API.
3+
Batching and Caching layer using Redis as the Caching layer.
4+
Redis Dataloader is based on the [Facebook Dataloader](https://github.com/facebook/dataloader),
5+
and uses it internally.
6+
7+
## Example
48

59
```javascript
610
const redis = require('redis').createClient();
711
const DataLoader = require('dataloader');
812
const RedisDataLoader = require('redis-dataloader')({ redis: redis });
913

10-
const redisDataLoader = new RedisDataLoader(
14+
const loader = new RedisDataLoader(
1115
// set a prefix for the keys stored in redis. This way you can avoid key
1216
// collisions for different data-sets in your redis instance.
1317
'redis-key-prefix',
@@ -23,12 +27,77 @@ const redisDataLoader = new RedisDataLoader(
2327
expire: 60
2428
}
2529
);
30+
31+
// load an individual item by its key
32+
loader.load(5).then(resp => console.log(resp));
33+
34+
//clear an individiaul item from the local and redis cache.
35+
loader.clear(5).then(() => {})
2636
```
2737

38+
## API Documentation
39+
2840
In general, RedisDataLoader has the same API as the Facebook Dataloader Api,
29-
with a few differences.
41+
with a few differences. Read through the [Facebook Dataloader documentation](https://github.com/facebook/dataloader) and then note the differences mentioned here.
3042

3143
- `clear` returns a promise (waits until redis succeeds at deleting the key)
3244
- `clearAll` is not available (redis does not have an efficient way to do this?)
3345
- `prime` will always overwrite the redis cache. It in turn calls prime on the local cache (which does not adjust the cache if the key already exists)
3446
- dataloader results must be either `null` or a JSON object.
47+
48+
### Instantiation
49+
50+
#### Dependency inject a Redis Connection
51+
52+
```
53+
const redis = require('redis').createClient();
54+
const RedisDataLoader = require('redis-dataloader')({ redis: redis });
55+
```
56+
57+
#### Create a new Dataloader.
58+
59+
Each Dataloader holds its own local in memory cache (Same as Facebook Dataloader),
60+
and additionally caches to your Redis instance.
61+
62+
```
63+
const loader = new RedisDataLoader('<redis key prefix>', '<Facebook Dataloader>', '<Options>');
64+
```
65+
66+
##### Redis Key Prefix
67+
68+
Specify a Prefix that will be appended to each key when storing in Redis.
69+
70+
So for example if your prefix is "bar" and you call `loader.load('foo')`, this key
71+
will be stored in Redis as **bar:foo**
72+
73+
##### Facebook Dataloader
74+
75+
A regular Facebook Dataloader is passed in as the second parameter. It will be
76+
used to fetch data from your underlying datastore (mongo, sql, whatever).
77+
It is very important to **disable the cache** on this dataloader. Redis dataloader
78+
will already do local in memory caching (unless you disable it).
79+
80+
##### Options
81+
82+
All the options available to Facebook Dataloader can be passed in here. An
83+
additional option called **expire** is also available, and will set a ttl in seconds
84+
on all keys set in redis if this option is passed.
85+
86+
87+
### Caching
88+
89+
The purpose of Redis Dataloader is to provide a caching layer in redis on top
90+
of the Facebook Dataloader. Facebook's Dataloader provides a local in memory cache.
91+
This may be ok for short lived per-request caches, but may not be sufficient if
92+
you need a long lived cache and/or you have multiple webservers that need to share
93+
data.
94+
95+
Redis Dataloader will additionally use the same local cache that Facebook Dataloader
96+
provides. It will first check the local cache, then check the redis cache, before
97+
finally checking your underlying datastore. This pattern may be desirable if for
98+
example you create a new DataLoader for each request. If your dataloader is long-lived
99+
you may want to disable to the local cache, and just rely on the redis cache instead
100+
101+
```
102+
const loader = new RedisDataLoader('prefix', new DataLoader(), { cache: false });
103+
```

0 commit comments

Comments
 (0)