@azteam/redis-async 1.0.47 → 1.0.50

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.47",
3
+ "version": "1.0.50",
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.7",
27
+ "@azteam/util": "1.0.8",
28
28
  "redis": "4.0.6"
29
29
  }
30
30
  }
package/src/Provider.js CHANGED
@@ -6,7 +6,7 @@ class Provider {
6
6
  this.configs = configs;
7
7
  if (isSingle) {
8
8
  this.configs = {
9
- single: configs
9
+ single: configs,
10
10
  };
11
11
  }
12
12
  }
@@ -17,7 +17,6 @@ class Provider {
17
17
  }
18
18
  return this.connections[name];
19
19
  }
20
-
21
20
  }
22
21
 
23
22
  export default Provider;
package/src/RedisAsync.js CHANGED
@@ -1,7 +1,5 @@
1
1
  import {createClient} from 'redis';
2
- import { promisify } from 'util';
3
- import { timeout } from '@azteam/util';
4
-
2
+ import {timeout} from '@azteam/util';
5
3
 
6
4
  class RedisAsync {
7
5
  constructor(config) {
@@ -42,8 +40,11 @@ class RedisAsync {
42
40
  this.connected = false;
43
41
  this._alert('connect', 'Redis disconnected');
44
42
  this.client.quit();
45
- }
43
+ },
46
44
  });
45
+
46
+ this.client.connect();
47
+
47
48
  this.client.on('connect', () => {
48
49
  this.connected = true;
49
50
  this._alert('connect', 'Redis connected');
@@ -59,62 +60,61 @@ class RedisAsync {
59
60
  }
60
61
 
61
62
  async get(key, defaultValue = null) {
62
-
63
63
  const prefixKey = this.parsePrefix(key);
64
64
 
65
65
  if (this.connected) {
66
-
67
66
  console.log(`Redis GET ${prefixKey}`);
68
67
 
69
- const getAsync = promisify(this.client.get).bind(this.client);
70
- const data = await getAsync(prefixKey);
68
+ const data = await this.client.get(prefixKey);
71
69
  if (data) {
72
70
  return JSON.parse(data);
73
71
  }
74
72
  } else {
75
73
  this.connect();
76
- return this.get(key);
74
+ return null;
77
75
  }
78
76
 
79
77
  return defaultValue;
80
78
  }
81
79
 
82
-
83
80
  async ttl(key) {
84
81
  const prefixKey = this.parsePrefix(key);
85
82
 
86
- const ttlAsync = promisify(this.client.ttl).bind(this.client);
87
- return await ttlAsync(prefixKey);
83
+ return await this.client.ttl(prefixKey);
88
84
  }
89
85
 
90
86
  async expire(key, timeSecond = 86400) {
91
87
  const prefixKey = this.parsePrefix(key);
92
-
93
88
  this.client.expire(prefixKey, timeSecond);
94
89
  }
95
90
 
96
- async set(key, data, timeSecond = 86400) {
91
+ async set(key, data, timeSecond = 86400, count = 0) {
97
92
  const prefixKey = this.parsePrefix(key);
98
93
 
99
94
  if (this.connected) {
100
95
  console.log(`Redis SET ${prefixKey}`);
101
- return await this.client.set(prefixKey, JSON.stringify(data), 'EX', timeSecond);
96
+ await this.client.set(prefixKey, JSON.stringify(data));
97
+ await this.expire(prefixKey, timeSecond);
98
+ return true;
102
99
  } else {
103
100
  this.connect();
104
- return this.set(key, data, timeSecond);
101
+
102
+ if (count < 5) {
103
+ return this.set(key, data, timeSecond, count + 1);
104
+ }
105
+ return false;
105
106
  }
106
107
  }
107
108
 
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
- }
109
+ // async scan(pattern, cursor = 0, count = 1000) {
110
+ // const scanAsync = promisify(this.client.scan).bind(this.client);
111
+ // return await scanAsync(cursor, 'MATCH', pattern, 'COUNT', count);
112
+ // }
112
113
 
113
- async remove(key, exact = true) {
114
+ async remove(key, exact = true, count = 0) {
114
115
  const prefixKey = this.parsePrefix(key);
115
116
 
116
117
  if (this.connected) {
117
-
118
118
  if (exact) {
119
119
  console.log(`Redis REMOVE ${prefixKey}`);
120
120
 
@@ -122,22 +122,22 @@ class RedisAsync {
122
122
  } else {
123
123
  const regexKey = '*' + prefixKey + '*';
124
124
 
125
- const getAsync = promisify(this.client.keys).bind(this.client);
126
- const keys = await getAsync(regexKey);
125
+ const keys = await this.client.keys(regexKey);
127
126
 
128
127
  if (keys.length > 0) {
129
128
  console.log(`Redis REMOVE keys`, keys);
130
129
  return await this.client.del(keys);
131
130
  }
132
131
  }
133
-
134
132
  } else {
135
133
  this.connect();
136
- return this.remove(key, exact);
134
+ if (count < 5) {
135
+ return this.remove(key, exact, count + 1);
136
+ }
137
+ return false;
137
138
  }
138
139
  }
139
140
 
140
-
141
141
  setAlertCallback(callback) {
142
142
  this.alertCallback = callback;
143
143
  }
@@ -151,4 +151,4 @@ class RedisAsync {
151
151
  }
152
152
  }
153
153
 
154
- export default RedisAsync;
154
+ export default RedisAsync;
package/src/index.js CHANGED
@@ -1,7 +1,4 @@
1
1
  import Provider from './Provider';
2
2
  import RedisAsync from './RedisAsync';
3
3
 
4
- export {
5
- RedisAsync,
6
- Provider as RedisProvider,
7
- }
4
+ export {RedisAsync, Provider as RedisProvider};