@azteam/redis-async 1.0.47 → 1.0.48
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 +1 -1
- package/src/RedisAsync.js +28 -21
package/package.json
CHANGED
package/src/RedisAsync.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import {createClient} from 'redis';
|
|
2
|
-
import {
|
|
3
|
-
import { timeout } from '@azteam/util';
|
|
2
|
+
import {timeout} from '@azteam/util';
|
|
4
3
|
|
|
5
4
|
|
|
6
5
|
class RedisAsync {
|
|
@@ -44,6 +43,9 @@ class RedisAsync {
|
|
|
44
43
|
this.client.quit();
|
|
45
44
|
}
|
|
46
45
|
});
|
|
46
|
+
|
|
47
|
+
this.client.connect();
|
|
48
|
+
|
|
47
49
|
this.client.on('connect', () => {
|
|
48
50
|
this.connected = true;
|
|
49
51
|
this._alert('connect', 'Redis connected');
|
|
@@ -66,14 +68,13 @@ class RedisAsync {
|
|
|
66
68
|
|
|
67
69
|
console.log(`Redis GET ${prefixKey}`);
|
|
68
70
|
|
|
69
|
-
const
|
|
70
|
-
const data = await getAsync(prefixKey);
|
|
71
|
+
const data = await this.client.get(prefixKey);
|
|
71
72
|
if (data) {
|
|
72
73
|
return JSON.parse(data);
|
|
73
74
|
}
|
|
74
75
|
} else {
|
|
75
76
|
this.connect();
|
|
76
|
-
return
|
|
77
|
+
return null;
|
|
77
78
|
}
|
|
78
79
|
|
|
79
80
|
return defaultValue;
|
|
@@ -83,38 +84,42 @@ class RedisAsync {
|
|
|
83
84
|
async ttl(key) {
|
|
84
85
|
const prefixKey = this.parsePrefix(key);
|
|
85
86
|
|
|
86
|
-
|
|
87
|
-
return await ttlAsync(prefixKey);
|
|
87
|
+
return await this.client.ttl(prefixKey);
|
|
88
88
|
}
|
|
89
89
|
|
|
90
90
|
async expire(key, timeSecond = 86400) {
|
|
91
91
|
const prefixKey = this.parsePrefix(key);
|
|
92
|
-
|
|
93
92
|
this.client.expire(prefixKey, timeSecond);
|
|
94
93
|
}
|
|
95
94
|
|
|
96
|
-
async set(key, data, timeSecond = 86400) {
|
|
95
|
+
async set(key, data, timeSecond = 86400, count = 0) {
|
|
97
96
|
const prefixKey = this.parsePrefix(key);
|
|
98
97
|
|
|
99
98
|
if (this.connected) {
|
|
100
99
|
console.log(`Redis SET ${prefixKey}`);
|
|
101
|
-
|
|
100
|
+
await this.client.set(prefixKey, JSON.stringify(data));
|
|
101
|
+
await this.expire(prefixKey, timeSecond);
|
|
102
|
+
return true;
|
|
102
103
|
} else {
|
|
103
104
|
this.connect();
|
|
104
|
-
|
|
105
|
+
|
|
106
|
+
if (count < 5) {
|
|
107
|
+
return this.set(key, data, timeSecond, count + 1);
|
|
108
|
+
}
|
|
109
|
+
return false;
|
|
110
|
+
|
|
105
111
|
}
|
|
106
112
|
}
|
|
107
113
|
|
|
108
|
-
async scan(pattern, cursor = 0, count = 1000) {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
}
|
|
114
|
+
// async scan(pattern, cursor = 0, count = 1000) {
|
|
115
|
+
// const scanAsync = promisify(this.client.scan).bind(this.client);
|
|
116
|
+
// return await scanAsync(cursor, 'MATCH', pattern, 'COUNT', count);
|
|
117
|
+
// }
|
|
112
118
|
|
|
113
|
-
async remove(key, exact = true) {
|
|
119
|
+
async remove(key, exact = true, count = 0) {
|
|
114
120
|
const prefixKey = this.parsePrefix(key);
|
|
115
121
|
|
|
116
122
|
if (this.connected) {
|
|
117
|
-
|
|
118
123
|
if (exact) {
|
|
119
124
|
console.log(`Redis REMOVE ${prefixKey}`);
|
|
120
125
|
|
|
@@ -122,8 +127,7 @@ class RedisAsync {
|
|
|
122
127
|
} else {
|
|
123
128
|
const regexKey = '*' + prefixKey + '*';
|
|
124
129
|
|
|
125
|
-
const
|
|
126
|
-
const keys = await getAsync(regexKey);
|
|
130
|
+
const keys = await this.client.keys(regexKey)
|
|
127
131
|
|
|
128
132
|
if (keys.length > 0) {
|
|
129
133
|
console.log(`Redis REMOVE keys`, keys);
|
|
@@ -133,7 +137,10 @@ class RedisAsync {
|
|
|
133
137
|
|
|
134
138
|
} else {
|
|
135
139
|
this.connect();
|
|
136
|
-
|
|
140
|
+
if (count < 5) {
|
|
141
|
+
return this.remove(key, exact, count + 1);
|
|
142
|
+
}
|
|
143
|
+
return false;
|
|
137
144
|
}
|
|
138
145
|
}
|
|
139
146
|
|
|
@@ -151,4 +158,4 @@ class RedisAsync {
|
|
|
151
158
|
}
|
|
152
159
|
}
|
|
153
160
|
|
|
154
|
-
export default RedisAsync;
|
|
161
|
+
export default RedisAsync;
|