@less-is-more/less-js 1.4.36 → 1.4.37-0

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.36",
3
+ "version": "1.4.37-0",
4
4
  "description": "Fast develop kit for nodejs",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/cache.js CHANGED
@@ -46,7 +46,18 @@ module.exports = class Cache {
46
46
  for (let i = 0; i < args.length; i++) {
47
47
  fullKey += ":" + args[i].toString();
48
48
  }
49
- let savedData = await Redis.exec("get", fullKey);
49
+ // 先从本地缓存取
50
+ let savedData = Cache._getLocal(fullKey);
51
+ let hasLocal = false;
52
+ if (Param.isBlank(savedData)) {
53
+ savedData = await Redis.exec("get", fullKey);
54
+ if (!Param.isBlank(savedData)) {
55
+ // 本地缓存1分钟
56
+ Cache._setLocal(fullKey, savedData, 60000);
57
+ }
58
+ } else {
59
+ hasLocal = true;
60
+ }
50
61
  if (savedData == null) {
51
62
  let result = await fn();
52
63
  if (!Param.isBlank(result)) {
@@ -66,11 +77,13 @@ module.exports = class Cache {
66
77
  redisContent = Cache._zip(redisContent);
67
78
  }
68
79
  await Redis.exec("setex", fullKey, timeSecond + "", redisContent);
80
+ // 本地缓存1分钟
81
+ Cache._setLocal(fullKey, redisContent, 60000);
69
82
  }
70
83
  return result;
71
84
  }
72
85
  } else {
73
- console.log("Found cache", fullKey);
86
+ console.log("Found cache" + (hasLocal ? " local" : ""), fullKey);
74
87
  savedData = Cache._unzip(savedData);
75
88
  // 优先转成json
76
89
  if (savedData.startsWith("{") || savedData.startsWith("[")) {
@@ -106,4 +119,24 @@ module.exports = class Cache {
106
119
  static setWebCache(res, expireSecond) {
107
120
  res.setHeader("Cache-Control", "max-age=" + expireSecond);
108
121
  }
122
+
123
+ // 本地缓存
124
+ static localCache = {};
125
+
126
+ static _setLocal(key, value, ttl) {
127
+ const expireTime = ttl ? Date.now() + ttl : null;
128
+ Cache.localCache[key] = { value, expireTime };
129
+ }
130
+
131
+ static _getLocal(key) {
132
+ const item = Cache.localCache[key];
133
+ if (!item) return null;
134
+
135
+ if (item.expireTime && Date.now() > item.expireTime) {
136
+ delete Cache.localCache[key];
137
+ return null;
138
+ }
139
+
140
+ return item.value;
141
+ }
109
142
  };
@@ -154,5 +154,19 @@ describe("cache.js", () => {
154
154
  console.log("result", result);
155
155
  assert(result === text);
156
156
  });
157
+
158
+ it("local cache", async () => {
159
+ async function test(a, b) {
160
+ return await Cache.auto("test3", 20, arguments, () => {
161
+ console.log(a, b);
162
+ return { a, b };
163
+ });
164
+ }
165
+ const result = await test(1, 2);
166
+ console.log("cache", Cache.localCache);
167
+ console.log("result", JSON.stringify(result));
168
+ const compare = { a: 1, b: 2 };
169
+ assert(result.a === compare.a);
170
+ });
157
171
  });
158
172
  });