@less-is-more/less-js 1.4.32 → 1.4.33-1
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 +27 -22
- package/test/test-cache.js +14 -0
package/package.json
CHANGED
package/src/cache.js
CHANGED
|
@@ -50,33 +50,28 @@ module.exports = class Cache {
|
|
|
50
50
|
if (savedData == null) {
|
|
51
51
|
let result = await fn();
|
|
52
52
|
if (!Param.isBlank(result)) {
|
|
53
|
+
let redisContent = "";
|
|
53
54
|
// 处理JSON
|
|
54
55
|
if (result instanceof Object) {
|
|
55
56
|
if (result.success === false) {
|
|
56
57
|
console.log("Do not cache fail result");
|
|
57
58
|
} else {
|
|
58
|
-
|
|
59
|
-
"setex",
|
|
60
|
-
fullKey,
|
|
61
|
-
timeSecond + "",
|
|
62
|
-
zip ? Cache._zip(JSON.stringify(result)) : JSON.stringify(result)
|
|
63
|
-
);
|
|
59
|
+
redisContent = JSON.stringify(result);
|
|
64
60
|
}
|
|
65
61
|
} else {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
62
|
+
redisContent = result.toString();
|
|
63
|
+
}
|
|
64
|
+
if (redisContent != "") {
|
|
65
|
+
if (Cache._needZip(redisContent, zip)) {
|
|
66
|
+
redisContent = Cache._zip(redisContent);
|
|
67
|
+
}
|
|
68
|
+
await Redis.exec("setex", fullKey, timeSecond + "", redisContent);
|
|
72
69
|
}
|
|
73
70
|
return result;
|
|
74
71
|
}
|
|
75
72
|
} else {
|
|
76
73
|
console.log("Found cache", fullKey);
|
|
77
|
-
|
|
78
|
-
savedData = Cache._unzip(savedData);
|
|
79
|
-
}
|
|
74
|
+
savedData = Cache._unzip(savedData);
|
|
80
75
|
// 优先转成json
|
|
81
76
|
if (savedData.startsWith("{") || savedData.startsWith("[")) {
|
|
82
77
|
return JSON.parse(savedData);
|
|
@@ -87,18 +82,28 @@ module.exports = class Cache {
|
|
|
87
82
|
}
|
|
88
83
|
|
|
89
84
|
static _zip(text) {
|
|
90
|
-
const zipData = zlib
|
|
91
|
-
|
|
92
|
-
.toString("base64");
|
|
93
|
-
return zipData;
|
|
85
|
+
const zipData = zlib.gzipSync(Buffer.from(text));
|
|
86
|
+
return zipData.toString("base64");
|
|
94
87
|
}
|
|
95
88
|
|
|
96
|
-
static _unzip(
|
|
97
|
-
|
|
98
|
-
|
|
89
|
+
static _unzip(content) {
|
|
90
|
+
// 判断content是否是buffer
|
|
91
|
+
let text = "";
|
|
92
|
+
const buffer = Buffer.from(content, "base64");
|
|
93
|
+
// 判断是否gzip
|
|
94
|
+
if (buffer[0] === 0x1f && buffer[1] === 0x8b) {
|
|
95
|
+
console.log("unzip");
|
|
96
|
+
text = zlib.gunzipSync(buffer).toString();
|
|
97
|
+
} else {
|
|
98
|
+
text = content;
|
|
99
|
+
}
|
|
99
100
|
return text;
|
|
100
101
|
}
|
|
101
102
|
|
|
103
|
+
static _needZip(text, zip) {
|
|
104
|
+
return zip || Buffer.byteLength(text) / 1024 > 1;
|
|
105
|
+
}
|
|
106
|
+
|
|
102
107
|
static setWebCache(res, expireSecond) {
|
|
103
108
|
res.setHeader("Cache-Control", "max-age=" + expireSecond);
|
|
104
109
|
}
|
package/test/test-cache.js
CHANGED
|
@@ -140,5 +140,19 @@ describe("cache.js", () => {
|
|
|
140
140
|
console.log("result", result);
|
|
141
141
|
assert(result === "3");
|
|
142
142
|
});
|
|
143
|
+
|
|
144
|
+
it("auto long text default zip", async () => {
|
|
145
|
+
let text = "";
|
|
146
|
+
// 循环1000次
|
|
147
|
+
for (let i = 0; i < 2000; i++) {
|
|
148
|
+
text += "a";
|
|
149
|
+
}
|
|
150
|
+
console.log(text);
|
|
151
|
+
let result = await Cache.auto("test", 20, [], async () => {
|
|
152
|
+
return text;
|
|
153
|
+
});
|
|
154
|
+
console.log("result", result);
|
|
155
|
+
assert(result === text);
|
|
156
|
+
});
|
|
143
157
|
});
|
|
144
158
|
});
|