@azteam/redis-async 1.0.37

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/README.md ADDED
@@ -0,0 +1 @@
1
+ # redis-async
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@azteam/redis-async",
3
+ "version": "1.0.37",
4
+ "description": "",
5
+ "main": "src/index.js",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/toda93/redis-async.git"
9
+ },
10
+ "engines": {
11
+ "node": ">= 10.0.0",
12
+ "npm": "^6.0.0"
13
+ },
14
+ "keywords": [
15
+ "toda",
16
+ "nodejs",
17
+ "redis",
18
+ "redis async"
19
+ ],
20
+ "author": "toda <sp.azsolution.net@gmail.com>",
21
+ "license": "MIT",
22
+ "bugs": {
23
+ "url": "https://github.com/toda93/redis-async/issues"
24
+ },
25
+ "homepage": "https://github.com/toda93/redis-async#readme",
26
+ "dependencies": {
27
+ "@azteam/util": "1.0.3",
28
+ "redis": "~2.8.0"
29
+ }
30
+ }
@@ -0,0 +1,17 @@
1
+ import RedisAsync from './RedisAsync';
2
+
3
+ class Provider {
4
+ constructor(configs) {
5
+ this.configs = configs;
6
+ this.connections = {};
7
+ }
8
+ getConnection(name) {
9
+ if (!this.connections[name]) {
10
+ this.connections[name] = new RedisAsync(this.configs[name]);
11
+ }
12
+ return this.connections[name];
13
+ }
14
+
15
+ }
16
+
17
+ export default Provider;
@@ -0,0 +1,154 @@
1
+ import redis from 'redis';
2
+ import { promisify } from 'util';
3
+ import { timeout } from '@azteam/util';
4
+
5
+
6
+ class RedisAsync {
7
+ constructor(config) {
8
+ this.connected = false;
9
+ this.host = config.host;
10
+ this.port = config.port;
11
+ this.prefix = config.prefix;
12
+ this.connect();
13
+ }
14
+
15
+ async waitConnection(n = 10) {
16
+ let i = 0;
17
+ while (!this.connected) {
18
+ ++i;
19
+ if (i >= n) {
20
+ return false;
21
+ }
22
+ await timeout(1000);
23
+ }
24
+ return true;
25
+ }
26
+
27
+ parsePrefix(name) {
28
+ if (this.prefix) {
29
+ return `${this.prefix}:${name}`;
30
+ }
31
+ return name;
32
+ }
33
+
34
+ connect() {
35
+ this._alert('connecting', 'Redis connecting...');
36
+
37
+ this.client = redis.createClient({
38
+ host: this.host,
39
+ port: this.port,
40
+
41
+ retry_strategy: (options) => {
42
+ this.connected = false;
43
+ this._alert('Redis disconnected');
44
+ this.client.quit();
45
+ }
46
+ });
47
+ this.client.on('connect', () => {
48
+ this.connected = true;
49
+ this._alert('connect', 'Redis connected');
50
+ });
51
+
52
+ this.client.on('end', () => {
53
+ this._alert('end', 'Redis end');
54
+ });
55
+
56
+ this.client.on('error', (err) => {
57
+ this._alert('error', 'Redis Error' + err);
58
+ });
59
+ }
60
+
61
+ async get(key, defaultValue = null) {
62
+
63
+ const prefixKey = this.parsePrefix(key);
64
+
65
+ if (this.connected) {
66
+
67
+ console.log(`Redis GET ${prefixKey}`);
68
+
69
+ const getAsync = promisify(this.client.get).bind(this.client);
70
+ const data = await getAsync(prefixKey);
71
+ if (data) {
72
+ return JSON.parse(data);
73
+ }
74
+ } else {
75
+ this.connect();
76
+ return this.get(key);
77
+ }
78
+
79
+ return defaultValue;
80
+ }
81
+
82
+
83
+ async ttl(key) {
84
+ const prefixKey = this.parsePrefix(key);
85
+
86
+ const ttlAsync = promisify(this.client.ttl).bind(this.client);
87
+ return await ttlAsync(prefixKey);
88
+ }
89
+
90
+ async expire(key, time = 100000) {
91
+ const prefixKey = this.parsePrefix(key);
92
+
93
+ this.client.expire(prefixKey, time);
94
+ }
95
+
96
+ async set(key, data, time = 100000) {
97
+ const prefixKey = this.parsePrefix(key);
98
+
99
+ if (this.connected) {
100
+ console.log(`Redis SET ${prefixKey}`);
101
+ return await this.client.set(prefixKey, JSON.stringify(data), 'EX', time);
102
+ } else {
103
+ this.connect();
104
+ return this.set(key, data, time);
105
+ }
106
+ }
107
+
108
+ async scan(pattern, cursor = 0, count = 1000) {
109
+ const scanAsync = promisify(this.client.scan).bind(this.client);
110
+ return await scanAsync(cursor, 'MATCH', pattern, 'COUNT', count);
111
+ }
112
+
113
+ async remove(key, exact = true) {
114
+ const prefixKey = this.parsePrefix(key);
115
+
116
+ if (this.connected) {
117
+
118
+ if (exact) {
119
+ console.log(`Redis REMOVE ${prefixKey}`);
120
+
121
+ await this.client.del(prefixKey);
122
+ } else {
123
+ const regexKey = '*' + prefixKey + '*';
124
+
125
+ const getAsync = promisify(this.client.keys).bind(this.client);
126
+ const keys = await getAsync(regexKey);
127
+
128
+ if (keys.length > 0) {
129
+ console.log(`Redis REMOVE keys`, keys);
130
+ return await this.client.del(keys);
131
+ }
132
+ }
133
+
134
+ } else {
135
+ this.connect();
136
+ return this.remove(key, exact);
137
+ }
138
+ }
139
+
140
+
141
+ setAlertCallback(callback) {
142
+ this.alertCallback = callback;
143
+ }
144
+
145
+ _alert(status, msg) {
146
+ if (typeof this.alertCallback === 'function') {
147
+ this.alertCallback(status, msg);
148
+ } else {
149
+ console.error(status, msg);
150
+ }
151
+ }
152
+ }
153
+
154
+ export default RedisAsync;
package/src/index.js ADDED
@@ -0,0 +1,7 @@
1
+ import Provider from './Provider';
2
+ import RedisAsync from './RedisAsync';
3
+
4
+ export {
5
+ RedisAsync,
6
+ Provider as RedisProvider,
7
+ }