@azteam/redis-async 1.0.66 → 1.0.68

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