Skip to content

Commit c0d2cb4

Browse files
committed
Add README
1 parent 278822e commit c0d2cb4

File tree

3 files changed

+149
-1
lines changed

3 files changed

+149
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules/
22
.idea/
3+
*.log

README.md

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
Redis store for node cache manager
2+
==================================
3+
4+
Redis cache store for [node-cache-manager](https://github.com/BryanDonovan/node-cache-manager).
5+
6+
How is this package different from `node-cache-manager-redis`?
7+
----------------------------------------------------------------------------------
8+
This is a **completely different version** than the earlier [node-cache-manager-redis](https://github.com/dial-once/node-cache-manager-redis). This package does not use `redis-pool` which is unnecessary and not actively maintained.
9+
10+
This package aims to provide **the most simple wrapper possible** by just passing the configuration to the underlying `node_redis` package.
11+
12+
Installation
13+
------------
14+
15+
```sh
16+
npm install cache-manager-redis-store --save
17+
```
18+
or
19+
```sh
20+
yarn add cache-manager-redis-store
21+
```
22+
23+
Usage Examples
24+
--------------
25+
26+
See examples below on how to implement the Redis cache store.
27+
28+
### Single store
29+
30+
```js
31+
var cacheManager = require('cache-manager');
32+
var redisStore = require('cache-manager-redis-store');
33+
34+
var redisCache = cacheManager.caching({
35+
store: redisStore,
36+
host: 'localhost', // default value
37+
port: 6379, // default value
38+
auth_pass: 'XXXXX',
39+
db: 0,
40+
ttl: 600
41+
});
42+
43+
// listen for redis connection error event
44+
var redisClient = redisCache.getClient();
45+
46+
redisClient.on('error', (error) => {
47+
// handle error here
48+
console.log(error);
49+
});
50+
51+
var ttl = 5;
52+
53+
redisCache.set('foo', 'bar', { ttl: ttl }, (err) => {
54+
if (err) {
55+
throw err;
56+
}
57+
58+
redisCache.get('foo', (err, result) => {
59+
console.log(result);
60+
// >> 'bar'
61+
redisCache.del('foo', (err) => {
62+
});
63+
});
64+
});
65+
66+
function getUser(id, cb) {
67+
setTimeout(() => {
68+
console.log("Returning user from slow database.");
69+
cb(null, { id: id, name: 'Bob' });
70+
}, 100);
71+
}
72+
73+
var userId = 123;
74+
var key = `user_${userId}`;
75+
76+
// Note: ttl is optional in wrap()
77+
redisCache.wrap(key, (cb) => {
78+
getUser(userId, cb);
79+
}, { ttl: ttl }, (err, user) => {
80+
console.log(user);
81+
82+
// Second time fetches user from redisCache
83+
redisCache
84+
.wrap(key, () => getUser(userId))
85+
.then(console.log)
86+
.catch(err => {
87+
// handle error
88+
});
89+
});
90+
```
91+
92+
### Multi-store
93+
94+
```js
95+
var cacheManager = require('cache-manager');
96+
var redisStore = require('cache-manager-redis-store');
97+
98+
var redisCache = cacheManager.caching({ store: redisStore, db: 0, ttl: 600 });
99+
var memoryCache = cacheManager.caching({ store: 'memory', max: 100, ttl: 60 });
100+
101+
var multiCache = cacheManager.multiCaching([memoryCache, redisCache]);
102+
103+
var userId2 = 456;
104+
var key2 = `user_${userId2}`;
105+
106+
// Set value in all caches
107+
multiCache.set('foo2', 'bar2', { ttl: ttl }, (err) => {
108+
if (err) {
109+
throw err;
110+
}
111+
112+
// Fetches from highest priority cache that has the key
113+
multiCache.get('foo2', (err, result) => {
114+
console.log(result);
115+
116+
// Delete from all caches
117+
multiCache.del('foo2');
118+
});
119+
});
120+
121+
// Note: ttl is optional in wrap
122+
multiCache.wrap(key2, (cb) => {
123+
getUser(userId2, cb);
124+
}, (err, user) => {
125+
console.log(user);
126+
127+
// Second time fetches user from memoryCache, since it's highest priority.
128+
// If the data expires in the memory cache, the next fetch would pull it from
129+
// the 'someOtherCache', and set the data in memory again.
130+
multiCache.wrap(key2, (cb) => {
131+
getUser(userId2, cb);
132+
}, (err, user) => {
133+
console.log(user);
134+
});
135+
});
136+
```
137+
138+
Contribution
139+
------------
140+
141+
Want to help improve this package? We take [pull requests](https://github.com/dabroek/node-cache-manager-redis-store/pulls).
142+
143+
144+
License
145+
-------
146+
147+
The `node-cache-manager-redis-store` is licensed under the MIT license.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "cache-manager-redis-store",
33
"author": "Matthijs Dabroek <[email protected]>",
44
"description": "Redis store for node-cache-manager module",
5-
"version": "1.0.0",
5+
"version": "1.0.1",
66
"license": "MIT",
77
"main": "dist/index.js",
88
"repository": {

0 commit comments

Comments
 (0)