@less-is-more/less-js 1.2.22 → 1.2.25
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/cache.js +11 -7
- package/src/ret.js +20 -21
- package/test/test-cache.js +22 -0
- package/test/test-ret.js +1 -0
package/package.json
CHANGED
package/src/cache.js
CHANGED
|
@@ -50,12 +50,16 @@ module.exports = class Cache {
|
|
|
50
50
|
if (!Param.isBlank(result)) {
|
|
51
51
|
// 处理JSON
|
|
52
52
|
if (result instanceof Object) {
|
|
53
|
-
|
|
54
|
-
"
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
53
|
+
if (result.success === false) {
|
|
54
|
+
console.log("Do not cache fail result");
|
|
55
|
+
} else {
|
|
56
|
+
await Redis.exec(
|
|
57
|
+
"setex",
|
|
58
|
+
fullKey,
|
|
59
|
+
timeSecond + "",
|
|
60
|
+
JSON.stringify(result)
|
|
61
|
+
);
|
|
62
|
+
}
|
|
59
63
|
} else {
|
|
60
64
|
await Redis.exec(
|
|
61
65
|
"setex",
|
|
@@ -69,7 +73,7 @@ module.exports = class Cache {
|
|
|
69
73
|
} else {
|
|
70
74
|
console.log("Found cache", fullKey);
|
|
71
75
|
// 优先转成json
|
|
72
|
-
if (savedData.startsWith("{")) {
|
|
76
|
+
if (savedData.startsWith("{") || savedData.startsWith("[")) {
|
|
73
77
|
return JSON.parse(savedData);
|
|
74
78
|
} else {
|
|
75
79
|
return savedData;
|
package/src/ret.js
CHANGED
|
@@ -6,65 +6,64 @@ module.exports = class Ret {
|
|
|
6
6
|
static CODE_SYSTEM = 1004;
|
|
7
7
|
|
|
8
8
|
constructor(success, message, code) {
|
|
9
|
-
this
|
|
9
|
+
this.success = success;
|
|
10
10
|
if (message) {
|
|
11
|
-
this
|
|
11
|
+
this.message = message;
|
|
12
12
|
}
|
|
13
13
|
if (code) {
|
|
14
|
-
this
|
|
14
|
+
this.code = code;
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
#instance = {};
|
|
19
18
|
ok() {
|
|
20
|
-
this
|
|
19
|
+
this.success = true;
|
|
21
20
|
return this;
|
|
22
21
|
}
|
|
23
|
-
isOk(){
|
|
24
|
-
return this
|
|
22
|
+
isOk() {
|
|
23
|
+
return this.success;
|
|
25
24
|
}
|
|
26
25
|
fail() {
|
|
27
|
-
this
|
|
26
|
+
this.success = false;
|
|
28
27
|
return this;
|
|
29
28
|
}
|
|
30
|
-
isFail(){
|
|
31
|
-
return !this
|
|
29
|
+
isFail() {
|
|
30
|
+
return !this.success;
|
|
32
31
|
}
|
|
33
32
|
setMessage(message) {
|
|
34
|
-
this
|
|
33
|
+
this.message = message;
|
|
35
34
|
return this;
|
|
36
35
|
}
|
|
37
36
|
getMessage() {
|
|
38
|
-
return this
|
|
37
|
+
return this.message;
|
|
39
38
|
}
|
|
40
39
|
setCode(code) {
|
|
41
|
-
this
|
|
40
|
+
this.code = code;
|
|
42
41
|
return this;
|
|
43
42
|
}
|
|
44
43
|
setData(data) {
|
|
45
|
-
this
|
|
44
|
+
this.data = data;
|
|
46
45
|
return this;
|
|
47
46
|
}
|
|
48
47
|
getData() {
|
|
49
|
-
return this
|
|
48
|
+
return this.data;
|
|
50
49
|
}
|
|
51
50
|
toString() {
|
|
52
|
-
return JSON.stringify(this
|
|
51
|
+
return JSON.stringify(this);
|
|
53
52
|
}
|
|
54
53
|
getJson() {
|
|
55
|
-
return this
|
|
54
|
+
return this;
|
|
56
55
|
}
|
|
57
56
|
toJson() {
|
|
58
|
-
return this
|
|
57
|
+
return this;
|
|
59
58
|
}
|
|
60
59
|
|
|
61
60
|
filter(props = []) {
|
|
62
61
|
if (props.length > 0) {
|
|
63
|
-
const data = this
|
|
62
|
+
const data = this.data;
|
|
64
63
|
if (data instanceof Array) {
|
|
65
|
-
this
|
|
64
|
+
this.data = this._filterArray(data, props);
|
|
66
65
|
} else {
|
|
67
|
-
this
|
|
66
|
+
this.data = this._filterObject(data, props);
|
|
68
67
|
}
|
|
69
68
|
}
|
|
70
69
|
return this;
|
package/test/test-cache.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
const assert = require("assert");
|
|
2
2
|
const Cache = require("../src/cache.js");
|
|
3
3
|
const Redis = require("../src/redis.js");
|
|
4
|
+
const Ret = require("../src/ret.js");
|
|
4
5
|
|
|
5
6
|
describe("cache.js", () => {
|
|
6
7
|
before(() => {
|
|
@@ -81,5 +82,26 @@ describe("cache.js", () => {
|
|
|
81
82
|
const compare = { a: 1, b: 2 };
|
|
82
83
|
assert(result.a === compare.a);
|
|
83
84
|
});
|
|
85
|
+
it("return array", async () => {
|
|
86
|
+
async function test(a, b) {
|
|
87
|
+
return await Cache.auto("test4", 20, arguments, () => {
|
|
88
|
+
console.log(a, b);
|
|
89
|
+
return [{ a, b }];
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
const result = await test(1, 2);
|
|
93
|
+
console.log("result", JSON.stringify(result));
|
|
94
|
+
assert(result.length === 1);
|
|
95
|
+
});
|
|
96
|
+
it("no cache fail", async () => {
|
|
97
|
+
async function test(a, b) {
|
|
98
|
+
return await Cache.auto("test5", 20, arguments, () => {
|
|
99
|
+
return new Ret(false);
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
const result = await test(1, 2);
|
|
103
|
+
console.log("result", JSON.stringify(result));
|
|
104
|
+
assert(result.success === false);
|
|
105
|
+
});
|
|
84
106
|
});
|
|
85
107
|
});
|