@less-is-more/less-js 1.5.0-8 → 1.5.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.
Files changed (3) hide show
  1. package/package.json +1 -1
  2. package/src/cache.js +28 -21
  3. package/src/nas.js +31 -19
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@less-is-more/less-js",
3
- "version": "1.5.0-8",
3
+ "version": "1.5.0",
4
4
  "description": "Fast develop kit for nodejs",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/cache.js CHANGED
@@ -74,39 +74,43 @@ module.exports = class Cache {
74
74
  if (savedData == null) {
75
75
  let result = await fn();
76
76
  if (!Param.isBlank(result)) {
77
- let redisContent = "";
77
+ let saveContent = "";
78
78
  // 处理JSON
79
79
  if (result instanceof Object) {
80
80
  if (result.success === false) {
81
81
  console.log("Do not cache fail result");
82
82
  } else {
83
- redisContent = JSON.stringify(result);
83
+ saveContent = JSON.stringify(result);
84
84
  }
85
85
  } else {
86
- redisContent = result.toString();
86
+ saveContent = result.toString();
87
87
  }
88
- if (redisContent != "") {
89
- if (Cache._needZip(redisContent, zip)) {
90
- redisContent = Cache._zip(redisContent);
88
+ if (saveContent != "") {
89
+ if (Cache._needZip(saveContent, zip)) {
90
+ saveContent = Cache._zip(saveContent);
91
91
  }
92
92
  if (useNas) {
93
- Nas.set(fullKey, result, timeSecond);
93
+ Nas.set(fullKey, saveContent, timeSecond);
94
94
  } else {
95
- await Redis.exec("setex", fullKey, timeSecond + "", redisContent);
95
+ await Redis.exec("setex", fullKey, timeSecond + "", saveContent);
96
96
  }
97
97
 
98
98
  // 本地缓存1分钟
99
- Cache._setLocal(fullKey, redisContent, 60000);
99
+ Cache._setLocal(fullKey, saveContent, 60000);
100
100
  }
101
101
  return result;
102
102
  }
103
103
  } else {
104
104
  console.log("Found cache" + (hasLocal ? " local" : ""), fullKey);
105
105
  savedData = Cache._unzip(savedData);
106
- // 优先转成json
107
- try {
108
- return JSON.parse(savedData);
109
- } catch (e) {
106
+ if (typeof savedData === "string") {
107
+ // 优先转成json
108
+ try {
109
+ return JSON.parse(savedData);
110
+ } catch (e) {
111
+ return savedData;
112
+ }
113
+ } else {
110
114
  return savedData;
111
115
  }
112
116
  }
@@ -118,16 +122,19 @@ module.exports = class Cache {
118
122
  }
119
123
 
120
124
  static _unzip(content) {
121
- // 判断content是否是buffer
122
- let text = "";
123
- const buffer = Buffer.from(content, "base64");
124
- // 判断是否gzip
125
- if (buffer.length > 2 && buffer[0] === 0x1f && buffer[1] === 0x8b) {
126
- text = zlib.gunzipSync(buffer).toString();
125
+ if (typeof content === "string") {
126
+ let text = "";
127
+ const buffer = Buffer.from(content, "base64");
128
+ // 判断是否gzip
129
+ if (buffer.length > 2 && buffer[0] === 0x1f && buffer[1] === 0x8b) {
130
+ text = zlib.gunzipSync(buffer).toString();
131
+ } else {
132
+ text = content;
133
+ }
134
+ return text;
127
135
  } else {
128
- text = content;
136
+ return content;
129
137
  }
130
- return text;
131
138
  }
132
139
 
133
140
  static _needZip(text, zip) {
package/src/nas.js CHANGED
@@ -115,8 +115,6 @@ class Nas {
115
115
  return valueStr;
116
116
  }
117
117
  } else {
118
- // 文件已过期,删除文件
119
- Nas._deleteCacheFile(key);
120
118
  return null;
121
119
  }
122
120
  }
@@ -141,11 +139,25 @@ class Nas {
141
139
  const filePath = Nas._getFilePath(key);
142
140
 
143
141
  try {
142
+ // 创建临时文件名,避免写入过程中的并发问题
143
+ const timestamp = Date.now();
144
+ const randomSuffix = Math.random().toFixed(4) * 10000;
145
+ const tempFilePath = `${filePath}.tmp.${timestamp}.${randomSuffix}`;
144
146
  // 写入过期时间戳和值,用换行符分隔
145
147
  const fileContent = `${expireTime}\n${content}`;
146
- fs.writeFileSync(filePath, fileContent, "utf8");
148
+ fs.writeFileSync(tempFilePath, fileContent, "utf8");
149
+ // 原子性地替换原文件
150
+ fs.renameSync(tempFilePath, filePath);
147
151
  } catch (e) {
148
152
  console.error("保存缓存文件失败:", filePath, e);
153
+ // 如果临时文件创建失败,清理临时文件
154
+ try {
155
+ if (fs.existsSync(tempFilePath)) {
156
+ fs.unlinkSync(tempFilePath);
157
+ }
158
+ } catch (cleanupError) {
159
+ // 忽略清理错误
160
+ }
149
161
  }
150
162
  }
151
163
 
@@ -186,23 +198,25 @@ class Nas {
186
198
  for (const file of files) {
187
199
  if (file.endsWith(".cache")) {
188
200
  const filePath = path.join(cacheDir, file);
189
- // 检查文件是否过期,如果过期则删除
201
+ // 检查文件是否过期超过一分钟,如果超过则删除
190
202
  try {
191
- const content = fs.readFileSync(filePath, "utf8");
192
- const lines = content.split("\n");
203
+ if (fs.existsSync(filePath)) {
204
+ const content = fs.readFileSync(filePath, "utf8");
205
+ const lines = content.split("\n");
193
206
 
194
- if (lines.length >= 1) {
195
- const expireTimeString = lines[0];
196
- const expireTime = parseInt(expireTimeString);
207
+ if (lines.length >= 1) {
208
+ const expireTimeString = lines[0];
209
+ const expireTime = parseInt(expireTimeString);
197
210
 
198
- if (
199
- !isNaN(expireTime) &&
200
- expireTime !== -1 &&
201
- Date.now() > expireTime
202
- ) {
203
- console.log("删除过期缓存文件:", filePath);
204
- // 文件已过期,删除文件
205
- fs.unlinkSync(filePath);
211
+ if (
212
+ !isNaN(expireTime) &&
213
+ expireTime !== -1 &&
214
+ // 仅删除过期超过一分钟的文件
215
+ Date.now() > expireTime + 60 * 1000
216
+ ) {
217
+ console.log("删除过期缓存文件:", filePath);
218
+ fs.unlinkSync(filePath);
219
+ }
206
220
  }
207
221
  }
208
222
  } catch (e) {
@@ -268,8 +282,6 @@ class Nas {
268
282
  const remainingTime = Math.floor((expireTime - Date.now()) / 1000);
269
283
  return Math.max(0, remainingTime); // 确保返回非负值
270
284
  } else {
271
- // 文件已过期,删除文件
272
- Nas._deleteCacheFile(key);
273
285
  return -1;
274
286
  }
275
287
  }