@less-is-more/less-js 1.4.33-0 → 1.4.33-2

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.4.33-0",
3
+ "version": "1.4.33-2",
4
4
  "description": "Fast develop kit for nodejs",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
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
- await Redis.exec(
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
- await Redis.exec(
67
- "setex",
68
- fullKey,
69
- timeSecond + "",
70
- zip ? Cache._zip(result.toString()) : result.toString()
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
- if (zip) {
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
- .gzipSync(Buffer.from(text, "utf-8"))
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(base64) {
97
- const buffer = Buffer.from(base64, "base64");
98
- const text = zlib.gunzipSync(buffer).toString();
89
+ static _unzip(content) {
90
+ // 判断content是否是buffer
91
+ let text = "";
92
+ const buffer = Buffer.from(content, "base64");
93
+ // 判断是否gzip
94
+ if (buffer.length > 2 && 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
  }
@@ -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
  });