Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom redis client #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
Redis store for node cache manager
==================================

Redis cache store for [node-cache-manager](https://github.com/BryanDonovan/node-cache-manager).
Redis cache store for [node-cache-manager](https://github.com/BryanDonovan/node-cache-manager).

How is this package different from `node-cache-manager-redis`?
----------------------------------------------------------------------------------
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.

This package aims to provide **the most simple wrapper possible** by just passing the configuration to the underlying `node_redis` package.

Installation
Expand Down Expand Up @@ -141,6 +141,21 @@ multiCache.wrap(key2, (cb) => {
});
```

### Existing Redis client

```js
var cacheManager = require('cache-manager');
var redisStore = require('cache-manager-redis-store');
var redis = require('redis');
var redisClient = redis.createClient();

var redisCache = cacheManager.caching({
store: redisStore,
client: redisClient,
ttl: 600
});
```

Contribution
------------

Expand All @@ -150,4 +165,4 @@ Want to help improve this package? We take [pull requests](https://github.com/da
License
-------

The `node-cache-manager-redis-store` is licensed under the MIT license.
The `node-cache-manager-redis-store` is licensed under the MIT license.
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Redis from 'redis';

const redisStore = (...args) => {
const redisCache = Redis.createClient(...args);
const storeArgs = redisCache.options;
const redisCache = args[0].client ? args[0].client : Redis.createClient(...args);
const storeArgs = { ...args[0], ...redisCache.options };

return {
name: 'redis',
Expand Down Expand Up @@ -148,7 +148,7 @@ const redisStore = (...args) => {
if (!cb) {
cb = (err, result) => (err ? reject(err) : resolve(result));
}

redisCache.flushdb(handleResponse(cb));
})
),
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 28 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import cacheManager from 'cache-manager';
import redisStore from '../index';
import redis from 'redis';

let redisCache;
let customRedisCache;
Expand Down Expand Up @@ -308,7 +309,7 @@ describe('get', () => {
it('should reject promise on error', (done) => {
const client = redisCache.store.getClient();
client.get = (key, cb) => cb(new Error('Something went wrong'));

redisCache.get('foo')
.catch((err) => {
expect(err.message).toEqual('Something went wrong');
Expand Down Expand Up @@ -693,6 +694,32 @@ describe('wrap function', () => {
});
}

it('should work with manual client', () => {
var customClientCache = cacheManager.caching({
store: redisStore,
client: redis.createClient(config),
});

const userId = 123;

// First call to wrap should run the code
return customClientCache
.wrap(
'wrap-promise',
() => getUserPromise(userId),
)
.then((user) => {
expect(user.id).toEqual(userId);

// Second call to wrap should retrieve from cache
return customClientCache.wrap(
'wrap-promise',
() => getUserPromise(userId + 1),
)
.then((user) => expect(user.id).toEqual(userId));
});
})

it('should be able to cache objects', (done) => {
const userId = 123;

Expand Down