Browse Source

build: Allow passing in an existing Redis instance (#30)

emp
Kiko Beats 5 years ago
committed by Jytesh
parent
commit
55415f7de8
  1. 16
      packages/keyv-redis/README.md
  2. 13
      packages/keyv-redis/src/index.js
  3. 13
      packages/keyv-redis/test/test.js

16
packages/keyv-redis/README.md

@ -36,11 +36,23 @@ const keyv = new Keyv('redis://user:pass@localhost:6379', { disable_resubscribin
Or you can manually create a storage adapter instance and pass it to Keyv:
```js
const KeyvRedis = require('@keyv/redis');
const Keyv = require('keyv');
const keyvRedis = new KeyvRedis('redis://user:pass@localhost:6379');
const keyv = new Keyv({ store: keyvRedis });
```
Or reuse a previous Redis instance:
```js
const KeyvRedis = require('@keyv/redis');
const Redis = require('ioredis');
const Keyv = require('keyv');
const redis = new KeyvRedis('redis://user:pass@localhost:6379');
const keyv = new Keyv({ store: redis });
const redis = new Redis('redis://user:pass@localhost:6379');
const keyvRedis = new KeyvRedis(redis);
const keyv = new Keyv({ store: keyvRedis });
```
## License

13
packages/keyv-redis/src/index.js

@ -6,13 +6,14 @@ const Redis = require('ioredis');
class KeyvRedis extends EventEmitter {
constructor(uri, opts) {
super();
opts = Object.assign(
{},
(typeof uri === 'string') ? { uri } : uri,
opts
);
this.redis = new Redis(opts.uri, opts);
if (uri instanceof Redis) {
this.redis = uri;
} else {
opts = Object.assign({}, typeof uri === 'string' ? { uri } : uri, opts);
this.redis = new Redis(opts.uri, opts);
}
this.redis.on('error', err => this.emit('error', err));
}

13
packages/keyv-redis/test/test.js

@ -2,6 +2,7 @@ import test from 'ava';
import keyvTestSuite, { keyvOfficialTests } from '@keyv/test-suite';
import Keyv from 'keyv';
import KeyvRedis from 'this';
import Redis from 'ioredis';
const { REDIS_HOST = 'localhost' } = process.env;
const redisURI = `redis://${REDIS_HOST}`;
@ -10,3 +11,15 @@ keyvOfficialTests(test, Keyv, redisURI, 'redis://foo');
const store = () => new KeyvRedis(redisURI);
keyvTestSuite(test, Keyv, store);
test('reuse a redis instance', async t => {
const redis = new Redis(redisURI);
redis.foo = 'bar';
const keyv = new KeyvRedis(redis);
t.is(keyv.redis.foo, 'bar');
await keyv.set('foo', 'bar');
const value = await redis.get('foo');
t.true(value === 'bar');
t.true(await keyv.get('foo') === value);
});

Loading…
Cancel
Save