Skip to content

Commit dd4e0ce

Browse files
committed
feat: 扩展 scriptResourceCache 缓存, 详见 demo.js
1 parent b1618c3 commit dd4e0ce

3 files changed

Lines changed: 41 additions & 8 deletions

File tree

backend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sub-store",
3-
"version": "2.16.60",
3+
"version": "2.16.61",
44
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.",
55
"main": "src/main.js",
66
"scripts": {

backend/src/utils/script-resource-cache.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class ResourceCache {
2424
this._cleanup();
2525
}
2626

27-
_cleanup() {
27+
_cleanup(prefix, expires) {
2828
// clear obsolete cached resource
2929
let clear = false;
3030
Object.entries(this.resourceCache).forEach((entry) => {
@@ -35,7 +35,11 @@ class ResourceCache {
3535
$.delete(`#${id}`);
3636
clear = true;
3737
}
38-
if (new Date().getTime() - updated.time > this.expires) {
38+
if (
39+
new Date().getTime() - updated.time >
40+
(expires ?? this.expires) ||
41+
(prefix && id.startsWith(prefix))
42+
) {
3943
delete this.resourceCache[id];
4044
clear = true;
4145
}
@@ -52,10 +56,15 @@ class ResourceCache {
5256
$.write(JSON.stringify(this.resourceCache), SCRIPT_RESOURCE_CACHE_KEY);
5357
}
5458

55-
get(id) {
59+
get(id, expires, remove) {
5660
const updated = this.resourceCache[id] && this.resourceCache[id].time;
57-
if (updated && new Date().getTime() - updated <= this.expires) {
58-
return this.resourceCache[id].data;
61+
if (updated) {
62+
if (new Date().getTime() - updated <= (expires ?? this.expires))
63+
return this.resourceCache[id].data;
64+
if (remove) {
65+
delete this.resourceCache[id];
66+
this._persist();
67+
}
5968
}
6069
return null;
6170
}

scripts/demo.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,32 @@ function operator(proxies = [], targetPlatform, context) {
5252
// scriptResourceCache 缓存
5353
// 可参考 https://t.me/zhetengsha/1003
5454
// const cache = scriptResourceCache
55-
// cache.set(id, data)
56-
// cache.get(id)
55+
// 设置
56+
// cache.set('a:1', 1)
57+
// cache.set('a:2', 2)
58+
// 获取
59+
// cache.get('a:1')
60+
// 支持第二个参数: 自定义过期时间
61+
// 支持第三个参数: 是否删除过期项
62+
// cache.get('a:2', 1000, true)
63+
64+
// 清理
65+
// cache._cleanup()
66+
// 支持第一个参数: 匹配前缀的项也一起删除
67+
// 支持第二个参数: 自定义过期时间
68+
// cache._cleanup('a:', 1000)
69+
70+
// 关于缓存时长
71+
72+
// 拉取 Sub-Store 订阅时, 会自动拉取远程订阅
73+
74+
// 远程订阅缓存是 1 小时, 缓存的唯一 key 为 url+ user agent. 可通过前端的刷新按钮刷新缓存. 或使用参数 noCache 来禁用缓存. 例: 内部配置订阅链接时使用 http://a.com#noCache, 外部使用 sub-store 链接时使用 https://sub.store/download/1?noCache=true
75+
76+
// 当使用相关脚本时, 若在对应的脚本中使用参数开启缓存, 可设置持久化缓存 sub-store-csr-expiration-time 的值来自定义默认缓存时长, 默认为 172800000 (48 * 3600 * 1000, 即 48 小时)
77+
78+
// 🎈Loon 可在插件中设置
79+
80+
// 其他平台同理, 持久化缓存数据在 JSON 里
5781

5882
// ProxyUtils 为节点处理工具
5983
// 可参考 https://t.me/zhetengsha/1066

0 commit comments

Comments
 (0)