@less-is-more/less-js 1.5.0-4 → 1.5.0-6
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 +10 -3
- package/src/nas.js +24 -2
- package/test/test-nas.js +17 -8
package/package.json
CHANGED
package/src/cache.js
CHANGED
|
@@ -43,7 +43,14 @@ module.exports = class Cache {
|
|
|
43
43
|
* @param {boolean} useNas 是否使用Nas缓存
|
|
44
44
|
* @returns 优先返回对象
|
|
45
45
|
*/
|
|
46
|
-
static async auto(
|
|
46
|
+
static async auto(
|
|
47
|
+
keyPrefix,
|
|
48
|
+
timeSecond,
|
|
49
|
+
args,
|
|
50
|
+
fn,
|
|
51
|
+
zip = false,
|
|
52
|
+
useNas = false
|
|
53
|
+
) {
|
|
47
54
|
let fullKey = keyPrefix;
|
|
48
55
|
for (let i = 0; i < args.length; i++) {
|
|
49
56
|
fullKey += ":" + args[i].toString();
|
|
@@ -97,9 +104,9 @@ module.exports = class Cache {
|
|
|
97
104
|
console.log("Found cache" + (hasLocal ? " local" : ""), fullKey);
|
|
98
105
|
savedData = Cache._unzip(savedData);
|
|
99
106
|
// 优先转成json
|
|
100
|
-
|
|
107
|
+
try {
|
|
101
108
|
return JSON.parse(savedData);
|
|
102
|
-
}
|
|
109
|
+
} catch (e) {
|
|
103
110
|
return savedData;
|
|
104
111
|
}
|
|
105
112
|
}
|
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,29 @@ class Nas {
|
|
|
180
180
|
for (const file of files) {
|
|
181
181
|
if (file.endsWith(".cache")) {
|
|
182
182
|
const filePath = path.join(cacheDir, file);
|
|
183
|
-
|
|
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
|
+
// 文件已过期,删除文件
|
|
194
|
+
fs.unlinkSync(filePath);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
} catch (e) {
|
|
198
|
+
console.warn("检查缓存文件过期时间失败:", filePath, e);
|
|
199
|
+
// 如果检查失败,尝试直接删除文件
|
|
200
|
+
try {
|
|
201
|
+
fs.unlinkSync(filePath);
|
|
202
|
+
} catch (deleteError) {
|
|
203
|
+
console.warn("删除缓存文件失败:", filePath, deleteError);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
184
206
|
}
|
|
185
207
|
}
|
|
186
208
|
}
|
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
|
|
96
|
-
|
|
97
|
-
Nas.set("
|
|
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
|
-
|
|
100
|
-
|
|
101
|
+
setTimeout(() => {
|
|
102
|
+
// 此时expiringKey已经过期,但是permanentKey仍然有效
|
|
103
|
+
Nas.flushAll(); // 应该只清除过期的键
|
|
101
104
|
|
|
102
|
-
|
|
105
|
+
// 永不过期的键应该仍然存在
|
|
106
|
+
assert(Nas.get("permanentKey") == "permanentValue");
|
|
107
|
+
// 过期的键应该已经被清除
|
|
108
|
+
assert(Nas.get("expiringKey") == null);
|
|
109
|
+
assert(Nas.exists("expiringKey") == false);
|
|
103
110
|
|
|
104
|
-
|
|
105
|
-
|
|
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 () {
|