@less-is-more/less-js 1.2.35 → 1.2.36

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 +1 -1
  2. package/src/redis.js +23 -12
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@less-is-more/less-js",
3
- "version": "1.2.35",
3
+ "version": "1.2.36",
4
4
  "description": "Fast develop kit for nodejs",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/redis.js CHANGED
@@ -14,23 +14,34 @@ module.exports = class Redis {
14
14
 
15
15
  static async getInstance() {
16
16
  if (!this.#client) {
17
- if (this.#host) {
18
- let params = {
19
- socket: { host: this.#host },
20
- };
21
- if (this.#password) {
22
- params.username = this.#user;
23
- params.password = this.#password;
24
- }
25
- this.#client = await redis.createClient(params);
26
- await this.#client.connect();
27
- } else {
28
- throw new Error("Redis not init");
17
+ await Redis.connect();
18
+ } else {
19
+ // 旧的实例判断一下是否可以链接,不能就重新建一个
20
+ try {
21
+ await this.#client.ping();
22
+ } catch (e) {
23
+ await Redis.connect();
29
24
  }
30
25
  }
31
26
  return this.#client;
32
27
  }
33
28
 
29
+ static async connect() {
30
+ if (this.#host) {
31
+ let params = {
32
+ socket: { host: this.#host },
33
+ };
34
+ if (this.#password) {
35
+ params.username = this.#user;
36
+ params.password = this.#password;
37
+ }
38
+ this.#client = redis.createClient(params);
39
+ await this.#client.connect();
40
+ } else {
41
+ throw new Error("Redis not init");
42
+ }
43
+ }
44
+
34
45
  static async exec(name, ...args) {
35
46
  let client = await Redis.getInstance();
36
47
  args.unshift(name);