@less-is-more/less-js 1.2.21 → 1.2.22

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": "@less-is-more/less-js",
3
- "version": "1.2.21",
3
+ "version": "1.2.22",
4
4
  "description": "Fast develop kit for nodejs",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/cache.js CHANGED
@@ -3,7 +3,7 @@ const Param = require("./param");
3
3
 
4
4
  module.exports = class Cache {
5
5
  /**
6
- * 缓存并获取结果
6
+ * 缓存并获取结果(调用方)
7
7
  * @param {*} key 缓存关键词,拼接参数
8
8
  * @param {number} timeSecond 缓存秒数
9
9
  * @param {object} thisObject this指向的对象
@@ -30,4 +30,50 @@ module.exports = class Cache {
30
30
  return savedData;
31
31
  }
32
32
  }
33
+
34
+ /**
35
+ *
36
+ * @param {string} keyPrefix 缓存前缀
37
+ * @param {number} timeSecond 缓存时间
38
+ * @param {*} args 直接传arguments,用于拼接缓存key
39
+ * @param {function} fn 没有缓存的实现
40
+ * @returns 优先返回对象
41
+ */
42
+ static async auto(keyPrefix, timeSecond, args, fn) {
43
+ let fullKey = keyPrefix;
44
+ for (let i = 0; i < args.length; i++) {
45
+ fullKey += ":" + args[i].toString();
46
+ }
47
+ let savedData = await Redis.exec("get", fullKey);
48
+ if (savedData == null) {
49
+ let result = await fn();
50
+ if (!Param.isBlank(result)) {
51
+ // 处理JSON
52
+ if (result instanceof Object) {
53
+ await Redis.exec(
54
+ "setex",
55
+ fullKey,
56
+ timeSecond + "",
57
+ JSON.stringify(result)
58
+ );
59
+ } else {
60
+ await Redis.exec(
61
+ "setex",
62
+ fullKey,
63
+ timeSecond + "",
64
+ result.toString()
65
+ );
66
+ }
67
+ return result;
68
+ }
69
+ } else {
70
+ console.log("Found cache", fullKey);
71
+ // 优先转成json
72
+ if (savedData.startsWith("{")) {
73
+ return JSON.parse(savedData);
74
+ } else {
75
+ return savedData;
76
+ }
77
+ }
78
+ }
33
79
  };
package/src/ret.js CHANGED
@@ -20,14 +20,23 @@ module.exports = class Ret {
20
20
  this.#instance.success = true;
21
21
  return this;
22
22
  }
23
+ isOk(){
24
+ return this.#instance.success;
25
+ }
23
26
  fail() {
24
27
  this.#instance.success = false;
25
28
  return this;
26
29
  }
30
+ isFail(){
31
+ return !this.#instance.success;
32
+ }
27
33
  setMessage(message) {
28
34
  this.#instance.message = message;
29
35
  return this;
30
36
  }
37
+ getMessage() {
38
+ return this.#instance.message;
39
+ }
31
40
  setCode(code) {
32
41
  this.#instance.code = code;
33
42
  return this;
@@ -36,6 +45,9 @@ module.exports = class Ret {
36
45
  this.#instance.data = data;
37
46
  return this;
38
47
  }
48
+ getData() {
49
+ return this.#instance.data;
50
+ }
39
51
  toString() {
40
52
  return JSON.stringify(this.#instance);
41
53
  }
@@ -3,45 +3,83 @@ const Cache = require("../src/cache.js");
3
3
  const Redis = require("../src/redis.js");
4
4
 
5
5
  describe("cache.js", () => {
6
- before(() => {
7
- console.log("init");
8
- Redis.init("127.0.0.1", "default", "KS5UggH4LNyyLBdr");
9
- });
6
+ before(() => {
7
+ console.log("init");
8
+ Redis.init("127.0.0.1", "default", "KS5UggH4LNyyLBdr");
9
+ });
10
10
 
11
- describe("get()", () => {
12
- it("return string", async () => {
13
- let testFunction = (a, b) => {
14
- console.log(a + b);
15
- return a + b + "";
16
- };
17
- let result = await Cache.get(
18
- "test",
19
- 20,
20
- testFunction,
21
- testFunction,
22
- 1,
23
- 2
24
- );
25
- console.log("result", result);
26
- assert(result === "3");
27
- });
28
- it("return object", async () => {
29
- let testFunction = (a, b) => {
30
- console.log(a + b);
31
- return { r: a + b };
32
- };
33
- try {
34
- let result = await Cache.get(
35
- "test2",
36
- 20,
37
- testFunction,
38
- testFunction,
39
- 1,
40
- 2
41
- );
42
- } catch (e) {
43
- assert(e.message === "只支持返回值是文本的方法");
44
- }
45
- });
46
- });
11
+ describe("get()", () => {
12
+ it("return string", async () => {
13
+ let testFunction = (a, b) => {
14
+ console.log(a + b);
15
+ return a + b + "";
16
+ };
17
+ let result = await Cache.get(
18
+ "test",
19
+ 20,
20
+ testFunction,
21
+ testFunction,
22
+ 1,
23
+ 2
24
+ );
25
+ console.log("result", result);
26
+ assert(result === "3");
27
+ });
28
+ it("return object", async () => {
29
+ let testFunction = (a, b) => {
30
+ console.log(a + b);
31
+ return { r: a + b };
32
+ };
33
+ try {
34
+ let result = await Cache.get(
35
+ "test2",
36
+ 20,
37
+ testFunction,
38
+ testFunction,
39
+ 1,
40
+ 2
41
+ );
42
+ } catch (e) {
43
+ assert(e.message === "只支持返回值是文本的方法");
44
+ }
45
+ });
46
+ });
47
+
48
+ describe("auto()", () => {
49
+ it("return string", async () => {
50
+ async function test(a, b) {
51
+ return await Cache.auto("test", 20, arguments, () => {
52
+ console.log(a + b);
53
+ return a + b + "";
54
+ });
55
+ }
56
+ const result = await test(1, 2);
57
+ console.log("result", result);
58
+ assert(result === "3");
59
+ });
60
+ it("return int", async () => {
61
+ async function test(a, b) {
62
+ let r = await Cache.auto("test2", 20, arguments, () => {
63
+ console.log(a + b);
64
+ return a + b;
65
+ });
66
+ return r;
67
+ }
68
+ const result = await test(1, 2);
69
+ console.log("result", result);
70
+ assert(result == 3);
71
+ });
72
+ it("return object", async () => {
73
+ async function test(a, b) {
74
+ return await Cache.auto("test3", 20, arguments, () => {
75
+ console.log(a, b);
76
+ return { a, b };
77
+ });
78
+ }
79
+ const result = await test(1, 2);
80
+ console.log("result", JSON.stringify(result));
81
+ const compare = { a: 1, b: 2 };
82
+ assert(result.a === compare.a);
83
+ });
84
+ });
47
85
  });