@less-is-more/less-js 1.5.0-5 → 1.5.0-7

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.5.0-5",
3
+ "version": "1.5.0-7",
4
4
  "description": "Fast develop kit for nodejs",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/nas.js CHANGED
@@ -170,7 +170,7 @@ class Nas {
170
170
  }
171
171
 
172
172
  /**
173
- * 清空所有缓存
173
+ * 清空所有过期缓存
174
174
  */
175
175
  static flushAll() {
176
176
  try {
@@ -180,7 +180,24 @@ class Nas {
180
180
  for (const file of files) {
181
181
  if (file.endsWith(".cache")) {
182
182
  const filePath = path.join(cacheDir, file);
183
- fs.unlinkSync(filePath);
183
+ // 检查文件是否过期,如果过期则删除
184
+ try {
185
+ const content = fs.readFileSync(filePath, "utf8");
186
+ const lines = content.split("\n");
187
+
188
+ if (lines.length >= 1) {
189
+ const expireTimeString = lines[0];
190
+ const expireTime = parseInt(expireTimeString);
191
+
192
+ if (!isNaN(expireTime) && expireTime !== -1 && Date.now() > expireTime) {
193
+ console.log("删除过期缓存文件:", filePath);
194
+ // 文件已过期,删除文件
195
+ fs.unlinkSync(filePath);
196
+ }
197
+ }
198
+ } catch (e) {
199
+ console.warn("检查缓存文件过期时间失败:", filePath, e);
200
+ }
184
201
  }
185
202
  }
186
203
  }
package/test/test-nas.js CHANGED
@@ -92,17 +92,26 @@ describe("Nas Class Tests", function () {
92
92
  }, 1100);
93
93
  });
94
94
 
95
- it("should flush all cache entries", function () {
96
- Nas.set("flushTest1", "value1");
97
- Nas.set("flushTest2", "value2");
95
+ it("should flush only expired cache entries", function (done) {
96
+ // 设置一个永不过期的键
97
+ Nas.set("permanentKey", "permanentValue", -1);
98
+ // 设置一个会过期的键
99
+ Nas.set("expiringKey", "expiringValue", 0.5); // 0.5秒后过期
98
100
 
99
- assert.isTrue(Nas.exists("flushTest1"));
100
- assert.isTrue(Nas.exists("flushTest2"));
101
+ setTimeout(() => {
102
+ // 此时expiringKey已经过期,但是permanentKey仍然有效
103
+ Nas.flushAll(); // 应该只清除过期的键
101
104
 
102
- Nas.flushAll();
105
+ // 永不过期的键应该仍然存在
106
+ assert(Nas.get("permanentKey") == "permanentValue");
107
+ // 过期的键应该已经被清除
108
+ assert(Nas.get("expiringKey") == null);
109
+ assert(Nas.exists("expiringKey") == false);
103
110
 
104
- assert.isFalse(Nas.exists("flushTest1"));
105
- assert.isFalse(Nas.exists("flushTest2"));
111
+ // 清理测试数据
112
+ Nas.del("permanentKey");
113
+ done();
114
+ }, 1000); // 等待expiringKey过期后再测试
106
115
  });
107
116
 
108
117
  it("should handle non-expiring keys (timeout = -1)", function () {