@keyv/redis 2.2.3 → 2.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,57 +1,68 @@
1
1
  {
2
- "name": "@keyv/redis",
3
- "version": "2.2.3",
4
- "description": "Redis storage adapter for Keyv",
5
- "main": "src/index.js",
6
- "scripts": {
7
- "test": "xo && nyc ava",
8
- "coverage": "nyc report --reporter=text-lcov > coverage.lcov",
9
- "clean": "rm -rf node_modules && rm -rf .nyc_output && rm -rf coverage.lcov"
10
- },
11
- "xo": {
12
- "extends": "xo-lukechilds",
13
- "rules": {
14
- "unicorn/prefer-module": 0
15
- }
16
- },
17
- "ava": {
18
- "require": [
19
- "requirable"
20
- ]
21
- },
22
- "repository": {
23
- "type": "git",
24
- "url": "git+https://github.com/jaredwray/keyv.git"
25
- },
26
- "keywords": [
27
- "redis",
28
- "keyv",
29
- "storage",
30
- "adapter",
31
- "key",
32
- "value",
33
- "store",
34
- "cache",
35
- "ttl"
36
- ],
37
- "author": "Jared Wray <me@jaredwray.com> (http://jaredwray.com)",
38
- "license": "MIT",
39
- "bugs": {
40
- "url": "https://github.com/jaredwray/keyv/issues"
41
- },
42
- "homepage": "https://github.com/jaredwray/keyv",
43
- "dependencies": {
44
- "ioredis": "^4.28.5"
45
- },
46
- "devDependencies": {
47
- "@keyv/test-suite": "*",
48
- "ava": "^3.15.0",
49
- "delay": "^5.0.0",
50
- "eslint-config-xo-lukechilds": "^1.0.1",
51
- "keyv": "*",
52
- "nyc": "^15.1.0",
53
- "requirable": "^1.0.5",
54
- "this": "^1.1.0",
55
- "xo": "^0.47.0"
56
- }
2
+ "name": "@keyv/redis",
3
+ "version": "2.3.2",
4
+ "description": "Redis storage adapter for Keyv",
5
+ "main": "src/index.js",
6
+ "scripts": {
7
+ "test": "xo && nyc ava",
8
+ "coverage": "nyc report --reporter=text-lcov > coverage.lcov",
9
+ "clean": "rm -rf node_modules && rm -rf .nyc_output && rm -rf coverage.lcov"
10
+ },
11
+ "xo": {
12
+ "rules": {
13
+ "unicorn/prefer-module": 0,
14
+ "unicorn/prefer-node-protocol": 0
15
+ }
16
+ },
17
+ "ava": {
18
+ "require": [
19
+ "requirable"
20
+ ]
21
+ },
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "git+https://github.com/jaredwray/keyv.git"
25
+ },
26
+ "keywords": [
27
+ "redis",
28
+ "keyv",
29
+ "storage",
30
+ "adapter",
31
+ "key",
32
+ "value",
33
+ "store",
34
+ "cache",
35
+ "ttl"
36
+ ],
37
+ "author": "Jared Wray <me@jaredwray.com> (http://jaredwray.com)",
38
+ "license": "MIT",
39
+ "bugs": {
40
+ "url": "https://github.com/jaredwray/keyv/issues"
41
+ },
42
+ "homepage": "https://github.com/jaredwray/keyv",
43
+ "dependencies": {
44
+ "ioredis": "^5.0.3"
45
+ },
46
+ "devDependencies": {
47
+ "@keyv/test-suite": "*",
48
+ "ava": "^4.1.0",
49
+ "delay": "^5.0.0",
50
+ "keyv": "*",
51
+ "nyc": "^15.1.0",
52
+ "requirable": "^1.0.5",
53
+ "this": "^1.1.0",
54
+ "tsd": "^0.20.0",
55
+ "typescript": "^4.6.3",
56
+ "xo": "^0.48.0"
57
+ },
58
+ "tsd" : {
59
+ "directory" : "test"
60
+ },
61
+ "types": "./src/index.d.ts",
62
+ "engines": {
63
+ "node": ">= 12"
64
+ },
65
+ "files": [
66
+ "src"
67
+ ]
57
68
  }
package/src/index.d.ts ADDED
@@ -0,0 +1,27 @@
1
+ import {EventEmitter} from 'events';
2
+ import {Store} from 'keyv';
3
+ import {ClientOpts} from 'redis';
4
+
5
+ declare class KeyvRedis extends EventEmitter implements Store<string | undefined> {
6
+ readonly ttlSupport: false;
7
+ namespace?: string | undefined;
8
+ opts: Record<string, unknown>;
9
+ redis: any;
10
+ constructor(options?: KeyvRedis.Options);
11
+ constructor(uri: string, options?: KeyvRedis.Options);
12
+ get(key: string): Promise<string | undefined>;
13
+ getMany(keys: string[]): Promise<string[] | undefined>;
14
+ set(key: string, value: string | undefined, ttl?: number): Promise<any>;
15
+ delete(key: string): boolean;
16
+ deleteMany(keys: string[]): boolean;
17
+ clear(): Promise<void>;
18
+ iterator(namespace: string | undefined): AsyncGenerator<any, void, any>;
19
+ has(key: string): boolean;
20
+ }
21
+ declare namespace KeyvRedis {
22
+ interface Options extends ClientOpts {
23
+ uri?: string | undefined;
24
+ dialect?: string | undefined;
25
+ }
26
+ }
27
+ export = KeyvRedis;
package/src/index.js CHANGED
@@ -5,11 +5,13 @@ class KeyvRedis extends EventEmitter {
5
5
  constructor(uri, options) {
6
6
  super();
7
7
  this.ttlSupport = true;
8
+ this.opts = {};
9
+ this.opts.dialect = 'redis';
8
10
 
9
11
  if (uri instanceof Redis || uri instanceof Redis.Cluster) {
10
12
  this.redis = uri;
11
13
  } else {
12
- options = Object.assign({}, typeof uri === 'string' ? { uri } : uri, options);
14
+ options = {...(typeof uri === 'string' ? {uri} : uri), ...options};
13
15
  this.redis = new Redis(options.uri, options);
14
16
  }
15
17
 
@@ -31,6 +33,11 @@ class KeyvRedis extends EventEmitter {
31
33
  });
32
34
  }
33
35
 
36
+ getMany(keys) {
37
+ return this.redis.mget(keys)
38
+ .then(rows => rows.every(row => row === null) ? [] : rows);
39
+ }
40
+
34
41
  set(key, value, ttl) {
35
42
  if (typeof value === 'undefined') {
36
43
  return Promise.resolve(undefined);
@@ -53,11 +60,46 @@ class KeyvRedis extends EventEmitter {
53
60
  .then(() => items > 0));
54
61
  }
55
62
 
63
+ deleteMany(key) {
64
+ return this.delete(key);
65
+ }
66
+
56
67
  clear() {
57
68
  return this.redis.smembers(this._getNamespace())
58
- .then(keys => this.redis.del(keys.concat(this._getNamespace())))
69
+ .then(keys => this.redis.del([...keys, ...this._getNamespace()]))
59
70
  .then(() => undefined);
60
71
  }
72
+
73
+ async * iterator(namespace) {
74
+ const scan = this.redis.scan.bind(this.redis);
75
+ const get = this.redis.mget.bind(this.redis);
76
+ async function * iterate(curs, pattern) {
77
+ const [cursor, keys] = await scan(curs, 'MATCH', pattern);
78
+ if (keys.length === 0) {
79
+ return;
80
+ }
81
+
82
+ const values = await get(keys);
83
+ for (const i in keys) {
84
+ if (Object.prototype.hasOwnProperty.call(keys, i)) {
85
+ const key = keys[i];
86
+ const value = values[i];
87
+ yield [key, value];
88
+ }
89
+ }
90
+
91
+ if (cursor !== '0') {
92
+ yield * iterate(cursor, pattern);
93
+ }
94
+ }
95
+
96
+ yield * iterate(0, `${namespace ? namespace + ':' : ''}*`);
97
+ }
98
+
99
+ has(key) {
100
+ return this.redis.exists(key)
101
+ .then(value => value !== 0);
102
+ }
61
103
  }
62
104
 
63
105
  module.exports = KeyvRedis;
@@ -1 +0,0 @@
1
- {"/Users/jaredwray/src/github.com/jaredwray/keyv/packages/redis/src/index.js":{"path":"/Users/jaredwray/src/github.com/jaredwray/keyv/packages/redis/src/index.js","statementMap":{"0":{"start":{"line":1,"column":21},"end":{"line":1,"column":38}},"1":{"start":{"line":2,"column":14},"end":{"line":2,"column":32}},"2":{"start":{"line":6,"column":2},"end":{"line":6,"column":10}},"3":{"start":{"line":7,"column":2},"end":{"line":7,"column":25}},"4":{"start":{"line":9,"column":2},"end":{"line":14,"column":3}},"5":{"start":{"line":10,"column":3},"end":{"line":10,"column":20}},"6":{"start":{"line":12,"column":3},"end":{"line":12,"column":81}},"7":{"start":{"line":13,"column":3},"end":{"line":13,"column":48}},"8":{"start":{"line":16,"column":2},"end":{"line":16,"column":61}},"9":{"start":{"line":16,"column":34},"end":{"line":16,"column":59}},"10":{"start":{"line":20,"column":2},"end":{"line":20,"column":39}},"11":{"start":{"line":24,"column":2},"end":{"line":31,"column":6}},"12":{"start":{"line":26,"column":4},"end":{"line":28,"column":5}},"13":{"start":{"line":27,"column":5},"end":{"line":27,"column":22}},"14":{"start":{"line":30,"column":4},"end":{"line":30,"column":17}},"15":{"start":{"line":35,"column":2},"end":{"line":37,"column":3}},"16":{"start":{"line":36,"column":3},"end":{"line":36,"column":37}},"17":{"start":{"line":39,"column":2},"end":{"line":47,"column":59}},"18":{"start":{"line":41,"column":4},"end":{"line":43,"column":5}},"19":{"start":{"line":42,"column":5},"end":{"line":42,"column":50}},"20":{"start":{"line":45,"column":4},"end":{"line":45,"column":38}},"21":{"start":{"line":47,"column":15},"end":{"line":47,"column":57}},"22":{"start":{"line":51,"column":2},"end":{"line":53,"column":28}},"23":{"start":{"line":52,"column":18},"end":{"line":53,"column":26}},"24":{"start":{"line":53,"column":16},"end":{"line":53,"column":25}},"25":{"start":{"line":57,"column":2},"end":{"line":59,"column":26}},"26":{"start":{"line":58,"column":17},"end":{"line":58,"column":66}},"27":{"start":{"line":59,"column":15},"end":{"line":59,"column":24}},"28":{"start":{"line":63,"column":0},"end":{"line":63,"column":27}}},"fnMap":{"0":{"name":"(anonymous_0)","decl":{"start":{"line":5,"column":1},"end":{"line":5,"column":2}},"loc":{"start":{"line":5,"column":27},"end":{"line":17,"column":2}},"line":5},"1":{"name":"(anonymous_1)","decl":{"start":{"line":16,"column":25},"end":{"line":16,"column":26}},"loc":{"start":{"line":16,"column":34},"end":{"line":16,"column":59}},"line":16},"2":{"name":"(anonymous_2)","decl":{"start":{"line":19,"column":1},"end":{"line":19,"column":2}},"loc":{"start":{"line":19,"column":17},"end":{"line":21,"column":2}},"line":19},"3":{"name":"(anonymous_3)","decl":{"start":{"line":23,"column":1},"end":{"line":23,"column":2}},"loc":{"start":{"line":23,"column":10},"end":{"line":32,"column":2}},"line":23},"4":{"name":"(anonymous_4)","decl":{"start":{"line":25,"column":9},"end":{"line":25,"column":10}},"loc":{"start":{"line":25,"column":18},"end":{"line":31,"column":4}},"line":25},"5":{"name":"(anonymous_5)","decl":{"start":{"line":34,"column":1},"end":{"line":34,"column":2}},"loc":{"start":{"line":34,"column":22},"end":{"line":48,"column":2}},"line":34},"6":{"name":"(anonymous_6)","decl":{"start":{"line":40,"column":9},"end":{"line":40,"column":10}},"loc":{"start":{"line":40,"column":15},"end":{"line":46,"column":4}},"line":40},"7":{"name":"(anonymous_7)","decl":{"start":{"line":47,"column":9},"end":{"line":47,"column":10}},"loc":{"start":{"line":47,"column":15},"end":{"line":47,"column":57}},"line":47},"8":{"name":"(anonymous_8)","decl":{"start":{"line":50,"column":1},"end":{"line":50,"column":2}},"loc":{"start":{"line":50,"column":13},"end":{"line":54,"column":2}},"line":50},"9":{"name":"(anonymous_9)","decl":{"start":{"line":52,"column":9},"end":{"line":52,"column":10}},"loc":{"start":{"line":52,"column":18},"end":{"line":53,"column":26}},"line":52},"10":{"name":"(anonymous_10)","decl":{"start":{"line":53,"column":10},"end":{"line":53,"column":11}},"loc":{"start":{"line":53,"column":16},"end":{"line":53,"column":25}},"line":53},"11":{"name":"(anonymous_11)","decl":{"start":{"line":56,"column":1},"end":{"line":56,"column":2}},"loc":{"start":{"line":56,"column":9},"end":{"line":60,"column":2}},"line":56},"12":{"name":"(anonymous_12)","decl":{"start":{"line":58,"column":9},"end":{"line":58,"column":10}},"loc":{"start":{"line":58,"column":17},"end":{"line":58,"column":66}},"line":58},"13":{"name":"(anonymous_13)","decl":{"start":{"line":59,"column":9},"end":{"line":59,"column":10}},"loc":{"start":{"line":59,"column":15},"end":{"line":59,"column":24}},"line":59}},"branchMap":{"0":{"loc":{"start":{"line":9,"column":2},"end":{"line":14,"column":3}},"type":"if","locations":[{"start":{"line":9,"column":2},"end":{"line":14,"column":3}},{"start":{"line":9,"column":2},"end":{"line":14,"column":3}}],"line":9},"1":{"loc":{"start":{"line":9,"column":6},"end":{"line":9,"column":58}},"type":"binary-expr","locations":[{"start":{"line":9,"column":6},"end":{"line":9,"column":26}},{"start":{"line":9,"column":30},"end":{"line":9,"column":58}}],"line":9},"2":{"loc":{"start":{"line":12,"column":31},"end":{"line":12,"column":70}},"type":"cond-expr","locations":[{"start":{"line":12,"column":57},"end":{"line":12,"column":64}},{"start":{"line":12,"column":67},"end":{"line":12,"column":70}}],"line":12},"3":{"loc":{"start":{"line":26,"column":4},"end":{"line":28,"column":5}},"type":"if","locations":[{"start":{"line":26,"column":4},"end":{"line":28,"column":5}},{"start":{"line":26,"column":4},"end":{"line":28,"column":5}}],"line":26},"4":{"loc":{"start":{"line":35,"column":2},"end":{"line":37,"column":3}},"type":"if","locations":[{"start":{"line":35,"column":2},"end":{"line":37,"column":3}},{"start":{"line":35,"column":2},"end":{"line":37,"column":3}}],"line":35},"5":{"loc":{"start":{"line":41,"column":4},"end":{"line":43,"column":5}},"type":"if","locations":[{"start":{"line":41,"column":4},"end":{"line":43,"column":5}},{"start":{"line":41,"column":4},"end":{"line":43,"column":5}}],"line":41}},"s":{"0":1,"1":1,"2":162,"3":162,"4":162,"5":2,"6":160,"7":160,"8":162,"9":8,"10":304,"11":30,"12":30,"13":11,"14":19,"15":30,"16":1,"17":29,"18":29,"19":1,"20":28,"21":29,"22":5,"23":5,"24":5,"25":135,"26":135,"27":135,"28":1},"f":{"0":162,"1":8,"2":304,"3":30,"4":30,"5":30,"6":29,"7":29,"8":5,"9":5,"10":5,"11":135,"12":135,"13":135},"b":{"0":[2,160],"1":[162,160],"2":[158,2],"3":[11,19],"4":[1,29],"5":[1,28]},"_coverageSchema":"1a1c01bbd47fc00a2c39e90264f33305004495a9","hash":"bd8f3b81707d244584617a53dd7adf94e42c4798","contentHash":"c5e135535b639a2ad3a20d9fad0afd67bb707402a023b20709855704d5b485fa"}}
@@ -1 +0,0 @@
1
- {"parent":"810254a5-cb5c-4b87-a408-502013ff5a42","pid":28483,"argv":["/Users/jaredwray/.nvm/versions/node/v16.13.1/bin/node","/Users/jaredwray/src/github.com/jaredwray/keyv/node_modules/ava/lib/worker/subprocess.js"],"execArgv":[],"cwd":"/Users/jaredwray/src/github.com/jaredwray/keyv/packages/redis","time":1644194105186,"ppid":28482,"coverageFilename":"/Users/jaredwray/src/github.com/jaredwray/keyv/packages/redis/.nyc_output/26233546-084d-42c8-b03f-1baeb0cafeca.json","externalId":"","uuid":"26233546-084d-42c8-b03f-1baeb0cafeca","files":["/Users/jaredwray/src/github.com/jaredwray/keyv/packages/redis/src/index.js"]}
@@ -1 +0,0 @@
1
- {"parent":null,"pid":28482,"argv":["/Users/jaredwray/.nvm/versions/node/v16.13.1/bin/node","/Users/jaredwray/src/github.com/jaredwray/keyv/packages/redis/node_modules/.bin/ava"],"execArgv":[],"cwd":"/Users/jaredwray/src/github.com/jaredwray/keyv/packages/redis","time":1644194104974,"ppid":28481,"coverageFilename":"/Users/jaredwray/src/github.com/jaredwray/keyv/packages/redis/.nyc_output/810254a5-cb5c-4b87-a408-502013ff5a42.json","externalId":"","uuid":"810254a5-cb5c-4b87-a408-502013ff5a42","files":[]}
@@ -1 +0,0 @@
1
- {"processes":{"26233546-084d-42c8-b03f-1baeb0cafeca":{"parent":"810254a5-cb5c-4b87-a408-502013ff5a42","children":[]},"810254a5-cb5c-4b87-a408-502013ff5a42":{"parent":null,"children":["26233546-084d-42c8-b03f-1baeb0cafeca"]}},"files":{"/Users/jaredwray/src/github.com/jaredwray/keyv/packages/redis/src/index.js":["26233546-084d-42c8-b03f-1baeb0cafeca"]},"externalIds":{}}
package/test/test.js DELETED
@@ -1,36 +0,0 @@
1
- const test = require('ava');
2
- const keyvTestSuite = require('@keyv/test-suite').default;
3
- const { keyvOfficialTests } = require('@keyv/test-suite');
4
- const Keyv = require('keyv');
5
- const KeyvRedis = require('this');
6
- const Redis = require('ioredis');
7
-
8
- const REDIS_HOST = 'localhost';
9
- const redisURI = `redis://${REDIS_HOST}`;
10
-
11
- keyvOfficialTests(test, Keyv, redisURI, 'redis://foo');
12
-
13
- const store = () => new KeyvRedis(redisURI);
14
-
15
- keyvTestSuite(test, Keyv, store);
16
-
17
- test('reuse a redis instance', async t => {
18
- const redis = new Redis(redisURI);
19
- redis.foo = 'bar';
20
- const keyv = new KeyvRedis(redis);
21
- t.is(keyv.redis.foo, 'bar');
22
-
23
- await keyv.set('foo', 'bar');
24
- const value = await redis.get('foo');
25
- t.true(value === 'bar');
26
- t.true(await keyv.get('foo') === value);
27
- });
28
-
29
- test('set an undefined key', async t => {
30
- const redis = new Redis(redisURI);
31
- const keyv = new KeyvRedis(redis);
32
-
33
- await keyv.set('foo2', undefined);
34
- const result = await keyv.get('foo2');
35
- t.true(result === undefined);
36
- });