@azteam/redis-async 1.0.45 → 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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/src/RedisAsync.js +30 -23
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@azteam/redis-async",
3
- "version": "1.0.45",
3
+ "version": "1.0.48",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "repository": {
@@ -24,7 +24,7 @@
24
24
  },
25
25
  "homepage": "https://github.com/toda93/redis-async#readme",
26
26
  "dependencies": {
27
- "@azteam/util": "1.0.6",
27
+ "@azteam/util": "1.0.7",
28
28
  "redis": "4.0.6"
29
29
  }
30
30
  }
package/src/RedisAsync.js CHANGED
@@ -1,6 +1,5 @@
1
- import redis from 'redis';
2
- import { promisify } from 'util';
3
- import { timeout } from '@azteam/util';
1
+ import {createClient} from 'redis';
2
+ import {timeout} from '@azteam/util';
4
3
 
5
4
 
6
5
  class RedisAsync {
@@ -34,7 +33,7 @@ class RedisAsync {
34
33
  connect() {
35
34
  this._alert('connecting', 'Redis connecting...');
36
35
 
37
- this.client = redis.createClient({
36
+ this.client = createClient({
38
37
  host: this.host,
39
38
  port: this.port,
40
39
 
@@ -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 getAsync = promisify(this.client.get).bind(this.client);
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 this.get(key);
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
- const ttlAsync = promisify(this.client.ttl).bind(this.client);
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
- return await this.client.set(prefixKey, JSON.stringify(data), 'EX', timeSecond);
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
- return this.set(key, data, timeSecond);
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
- const scanAsync = promisify(this.client.scan).bind(this.client);
110
- return await scanAsync(cursor, 'MATCH', pattern, 'COUNT', count);
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 getAsync = promisify(this.client.keys).bind(this.client);
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
- return this.remove(key, exact);
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;