mirror of
https://github.com/sub-store-org/Sub-Store.git
synced 2026-11-28 07:24:12 +00:00
feat: 重构缓存逻辑, 支持配置各项缓存 TTL 配置(前端 >= 2.16.0), 不再支持其他方式自定义; scriptResourceCache 缓存操作支持传入自定义 TTL
This commit is contained in:
8 files changed
+184
-163
No files matched your search
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "sub-store",
|
||||
"version": "2.20.84",
|
||||
"version": "2.21.1",
|
||||
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.",
|
||||
"main": "src/main.js",
|
||||
"scripts": {
|
||||
|
||||
@@ -12,7 +12,7 @@ export const GIST_BACKUP_FILE_NAME = 'Sub-Store';
|
||||
export const ARTIFACT_REPOSITORY_KEY = 'Sub-Store Artifacts Repository';
|
||||
export const RESOURCE_CACHE_KEY = '#sub-store-cached-resource';
|
||||
export const HEADERS_RESOURCE_CACHE_KEY = '#sub-store-cached-headers-resource';
|
||||
export const CHR_EXPIRATION_TIME_KEY = '#sub-store-chr-expiration-time'; // Custom expiration time key; (Loon|Surge) Default write 1 min
|
||||
export const CACHE_EXPIRATION_TIME_MS = 60 * 60 * 1000; // 1 hour
|
||||
export const SCRIPT_RESOURCE_CACHE_KEY = '#sub-store-cached-script-resource'; // cached-script-resource CSR
|
||||
export const CSR_EXPIRATION_TIME_KEY = '#sub-store-csr-expiration-time'; // Custom expiration time key; (Loon|Surge) Default write 48 hour
|
||||
export const SCRIPT_RESOURCE_CACHE_KEY = '#sub-store-cached-script-resource';
|
||||
export const DEFAULT_CACHE_TTL = 60 * 60 * 1000; // 1 hour
|
||||
export const DEFAULT_HEADERS_CACHE_TTL = 60 * 1000; // 1 min
|
||||
export const DEFAULT_SCRIPT_CACHE_TTL = 48 * 3600 * 1000; // 48 hours
|
||||
@@ -42,6 +42,18 @@ async function updateSettings(req, res) {
|
||||
...settings,
|
||||
...req.body,
|
||||
};
|
||||
[
|
||||
'defaultTimeout',
|
||||
'cacheThreshold',
|
||||
'resourceCacheTtl',
|
||||
'headersCacheTtl',
|
||||
'scriptCacheTtl',
|
||||
].map((key) => {
|
||||
let value = Number(newSettings[key]);
|
||||
if (!isFinite(value) || value <= 0) {
|
||||
delete newSettings[key];
|
||||
}
|
||||
});
|
||||
$.write(newSettings, SETTINGS_KEY);
|
||||
if (
|
||||
req.body.githubUser ||
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import $ from '@/core/app';
|
||||
import {
|
||||
HEADERS_RESOURCE_CACHE_KEY,
|
||||
CHR_EXPIRATION_TIME_KEY,
|
||||
DEFAULT_HEADERS_CACHE_TTL,
|
||||
SETTINGS_KEY,
|
||||
} from '@/constants';
|
||||
|
||||
class ResourceCache {
|
||||
constructor() {
|
||||
this.expires = getExpiredTime();
|
||||
if (!$.read(HEADERS_RESOURCE_CACHE_KEY)) {
|
||||
$.write('{}', HEADERS_RESOURCE_CACHE_KEY);
|
||||
}
|
||||
@@ -24,18 +24,15 @@ class ResourceCache {
|
||||
this._cleanup();
|
||||
}
|
||||
|
||||
_cleanup() {
|
||||
// clear obsolete cached resource
|
||||
_cleanup(prefix, ttl) {
|
||||
const resolvedTTL = normalizeTTL(ttl) ?? 0;
|
||||
let clear = false;
|
||||
const now = Date.now();
|
||||
Object.entries(this.resourceCache).forEach((entry) => {
|
||||
const [id, updated] = entry;
|
||||
if (!updated.time) {
|
||||
// clear old version cache
|
||||
delete this.resourceCache[id];
|
||||
$.delete(`#${id}`);
|
||||
clear = true;
|
||||
}
|
||||
if (new Date().getTime() - updated.time > this.expires) {
|
||||
const [id, cached] = entry;
|
||||
const shouldDelete =
|
||||
!cached.time || cached.time < now + resolvedTTL;
|
||||
if (shouldDelete && (prefix ? id.startsWith(prefix) : true)) {
|
||||
delete this.resourceCache[id];
|
||||
clear = true;
|
||||
}
|
||||
@@ -52,66 +49,55 @@ class ResourceCache {
|
||||
$.write(JSON.stringify(this.resourceCache), HEADERS_RESOURCE_CACHE_KEY);
|
||||
}
|
||||
|
||||
get(id) {
|
||||
const updated = this.resourceCache[id] && this.resourceCache[id].time;
|
||||
if (updated && new Date().getTime() - updated <= this.expires) {
|
||||
return this.resourceCache[id].data;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
gettime(id) {
|
||||
const updated = this.resourceCache[id] && this.resourceCache[id].time;
|
||||
if (updated && new Date().getTime() - updated <= this.expires) {
|
||||
const time = this.resourceCache[id] && this.resourceCache[id].time;
|
||||
if (time && new Date().getTime() <= time) {
|
||||
return this.resourceCache[id].time;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
set(id, value) {
|
||||
this.resourceCache[id] = { time: new Date().getTime(), data: value };
|
||||
get(id, ttl, remove) {
|
||||
const resolvedTTL = normalizeTTL(ttl) ?? 0;
|
||||
const cached = this.resourceCache[id];
|
||||
const time = cached && cached.time;
|
||||
if (time) {
|
||||
if (Date.now() + resolvedTTL <= time) return cached.data;
|
||||
if (remove) {
|
||||
delete this.resourceCache[id];
|
||||
this._persist();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
set(id, value, ttl) {
|
||||
const resolvedTTL = normalizeTTL(ttl) ?? getTTL();
|
||||
this.resourceCache[id] = {
|
||||
time: Date.now() + resolvedTTL,
|
||||
data: value,
|
||||
};
|
||||
this._persist();
|
||||
}
|
||||
}
|
||||
|
||||
function getExpiredTime() {
|
||||
// console.log($.read(CHR_EXPIRATION_TIME_KEY));
|
||||
if (!$.read(CHR_EXPIRATION_TIME_KEY)) {
|
||||
$.write('6e4', CHR_EXPIRATION_TIME_KEY); // 1分钟
|
||||
}
|
||||
let expiration = 6e4;
|
||||
if ($.env.isLoon) {
|
||||
const loont = {
|
||||
// Loon 插件自义定
|
||||
'1\u5206\u949f': 6e4,
|
||||
'5\u5206\u949f': 3e5,
|
||||
'10\u5206\u949f': 6e5,
|
||||
'30\u5206\u949f': 18e5, // "30分钟"
|
||||
'1\u5c0f\u65f6': 36e5,
|
||||
'2\u5c0f\u65f6': 72e5,
|
||||
'3\u5c0f\u65f6': 108e5,
|
||||
'6\u5c0f\u65f6': 216e5,
|
||||
'12\u5c0f\u65f6': 432e5,
|
||||
'24\u5c0f\u65f6': 864e5,
|
||||
'48\u5c0f\u65f6': 1728e5,
|
||||
'72\u5c0f\u65f6': 2592e5, // "72小时"
|
||||
'\u53c2\u6570\u4f20\u5165': 'readcachets', // "参数输入"
|
||||
};
|
||||
let intimed = $.read(
|
||||
'#\u54cd\u5e94\u5934\u7f13\u5b58\u6709\u6548\u671f',
|
||||
); // Loon #响应头缓存有效期
|
||||
// console.log(intimed);
|
||||
if (intimed in loont) {
|
||||
expiration = loont[intimed];
|
||||
if (expiration === 'readcachets') {
|
||||
expiration = intimed;
|
||||
}
|
||||
function normalizeTTL(ttl) {
|
||||
const value = Number(ttl);
|
||||
if (!isFinite(value)) return null;
|
||||
if (value > 0) return value;
|
||||
return null;
|
||||
}
|
||||
|
||||
function getTTL() {
|
||||
const settings = $.read(SETTINGS_KEY);
|
||||
let ttl = settings?.headersCacheTtl;
|
||||
if (ttl) {
|
||||
ttl = Number(ttl);
|
||||
if (isFinite(ttl) && ttl > 0) {
|
||||
return ttl * 1000;
|
||||
}
|
||||
return expiration;
|
||||
} else {
|
||||
expiration = $.read(CHR_EXPIRATION_TIME_KEY);
|
||||
return expiration;
|
||||
}
|
||||
return DEFAULT_HEADERS_CACHE_TTL;
|
||||
}
|
||||
|
||||
export default new ResourceCache();
|
||||
@@ -1,9 +1,12 @@
|
||||
import $ from '@/core/app';
|
||||
import { CACHE_EXPIRATION_TIME_MS, RESOURCE_CACHE_KEY } from '@/constants';
|
||||
import {
|
||||
RESOURCE_CACHE_KEY,
|
||||
DEFAULT_CACHE_TTL,
|
||||
SETTINGS_KEY,
|
||||
} from '@/constants';
|
||||
|
||||
class ResourceCache {
|
||||
constructor(expires) {
|
||||
this.expires = expires;
|
||||
constructor() {
|
||||
if (!$.read(RESOURCE_CACHE_KEY)) {
|
||||
$.write('{}', RESOURCE_CACHE_KEY);
|
||||
}
|
||||
@@ -21,18 +24,15 @@ class ResourceCache {
|
||||
this._cleanup();
|
||||
}
|
||||
|
||||
_cleanup() {
|
||||
// clear obsolete cached resource
|
||||
_cleanup(prefix, ttl) {
|
||||
const resolvedTTL = normalizeTTL(ttl) ?? 0;
|
||||
let clear = false;
|
||||
const now = Date.now();
|
||||
Object.entries(this.resourceCache).forEach((entry) => {
|
||||
const [id, updated] = entry;
|
||||
if (!updated.time) {
|
||||
// clear old version cache
|
||||
delete this.resourceCache[id];
|
||||
$.delete(`#${id}`);
|
||||
clear = true;
|
||||
}
|
||||
if (new Date().getTime() - updated.time > this.expires) {
|
||||
const [id, cached] = entry;
|
||||
const shouldDelete =
|
||||
!cached.time || cached.time < now + resolvedTTL;
|
||||
if (shouldDelete && (prefix ? id.startsWith(prefix) : true)) {
|
||||
delete this.resourceCache[id];
|
||||
clear = true;
|
||||
}
|
||||
@@ -49,18 +49,55 @@ class ResourceCache {
|
||||
$.write(JSON.stringify(this.resourceCache), RESOURCE_CACHE_KEY);
|
||||
}
|
||||
|
||||
get(id) {
|
||||
const updated = this.resourceCache[id] && this.resourceCache[id].time;
|
||||
if (updated && new Date().getTime() - updated <= this.expires) {
|
||||
return this.resourceCache[id].data;
|
||||
gettime(id) {
|
||||
const time = this.resourceCache[id] && this.resourceCache[id].time;
|
||||
if (time && new Date().getTime() <= time) {
|
||||
return this.resourceCache[id].time;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
set(id, value) {
|
||||
this.resourceCache[id] = { time: new Date().getTime(), data: value };
|
||||
get(id, ttl, remove) {
|
||||
const resolvedTTL = normalizeTTL(ttl) ?? 0;
|
||||
const cached = this.resourceCache[id];
|
||||
const time = cached && cached.time;
|
||||
if (time) {
|
||||
if (Date.now() + resolvedTTL <= time) return cached.data;
|
||||
if (remove) {
|
||||
delete this.resourceCache[id];
|
||||
this._persist();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
set(id, value, ttl) {
|
||||
const resolvedTTL = normalizeTTL(ttl) ?? getTTL();
|
||||
this.resourceCache[id] = {
|
||||
time: Date.now() + resolvedTTL,
|
||||
data: value,
|
||||
};
|
||||
this._persist();
|
||||
}
|
||||
}
|
||||
|
||||
export default new ResourceCache(CACHE_EXPIRATION_TIME_MS);
|
||||
function normalizeTTL(ttl) {
|
||||
const value = Number(ttl);
|
||||
if (!isFinite(value)) return null;
|
||||
if (value > 0) return value;
|
||||
return null;
|
||||
}
|
||||
|
||||
function getTTL() {
|
||||
const settings = $.read(SETTINGS_KEY);
|
||||
let ttl = settings?.resourceCacheTtl;
|
||||
if (ttl) {
|
||||
ttl = Number(ttl);
|
||||
if (isFinite(ttl) && ttl > 0) {
|
||||
return ttl * 1000;
|
||||
}
|
||||
}
|
||||
return DEFAULT_CACHE_TTL;
|
||||
}
|
||||
|
||||
export default new ResourceCache();
|
||||
@@ -1,12 +1,12 @@
|
||||
import $ from '@/core/app';
|
||||
import {
|
||||
SCRIPT_RESOURCE_CACHE_KEY,
|
||||
CSR_EXPIRATION_TIME_KEY,
|
||||
DEFAULT_SCRIPT_CACHE_TTL,
|
||||
SETTINGS_KEY,
|
||||
} from '@/constants';
|
||||
|
||||
class ResourceCache {
|
||||
constructor() {
|
||||
this.expires = getExpiredTime();
|
||||
if (!$.read(SCRIPT_RESOURCE_CACHE_KEY)) {
|
||||
$.write('{}', SCRIPT_RESOURCE_CACHE_KEY);
|
||||
}
|
||||
@@ -24,22 +24,15 @@ class ResourceCache {
|
||||
this._cleanup();
|
||||
}
|
||||
|
||||
_cleanup(prefix, expires) {
|
||||
// clear obsolete cached resource
|
||||
_cleanup(prefix, ttl) {
|
||||
const resolvedTTL = normalizeTTL(ttl) ?? 0;
|
||||
let clear = false;
|
||||
const now = Date.now();
|
||||
Object.entries(this.resourceCache).forEach((entry) => {
|
||||
const [id, updated] = entry;
|
||||
if (!updated.time) {
|
||||
// clear old version cache
|
||||
delete this.resourceCache[id];
|
||||
$.delete(`#${id}`);
|
||||
clear = true;
|
||||
}
|
||||
if (
|
||||
new Date().getTime() - updated.time >
|
||||
(expires ?? this.expires) ||
|
||||
(prefix && id.startsWith(prefix))
|
||||
) {
|
||||
const [id, cached] = entry;
|
||||
const shouldDelete =
|
||||
!cached.time || cached.time < now + resolvedTTL;
|
||||
if (shouldDelete && (prefix ? id.startsWith(prefix) : true)) {
|
||||
delete this.resourceCache[id];
|
||||
clear = true;
|
||||
}
|
||||
@@ -56,11 +49,20 @@ class ResourceCache {
|
||||
$.write(JSON.stringify(this.resourceCache), SCRIPT_RESOURCE_CACHE_KEY);
|
||||
}
|
||||
|
||||
get(id, expires, remove) {
|
||||
const updated = this.resourceCache[id] && this.resourceCache[id].time;
|
||||
if (updated) {
|
||||
if (new Date().getTime() - updated <= (expires ?? this.expires))
|
||||
return this.resourceCache[id].data;
|
||||
gettime(id) {
|
||||
const time = this.resourceCache[id] && this.resourceCache[id].time;
|
||||
if (time && new Date().getTime() <= time) {
|
||||
return this.resourceCache[id].time;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
get(id, ttl, remove) {
|
||||
const resolvedTTL = normalizeTTL(ttl) ?? 0;
|
||||
const cached = this.resourceCache[id];
|
||||
const time = cached && cached.time;
|
||||
if (time) {
|
||||
if (Date.now() + resolvedTTL <= time) return cached.data;
|
||||
if (remove) {
|
||||
delete this.resourceCache[id];
|
||||
this._persist();
|
||||
@@ -69,56 +71,33 @@ class ResourceCache {
|
||||
return null;
|
||||
}
|
||||
|
||||
gettime(id) {
|
||||
const updated = this.resourceCache[id] && this.resourceCache[id].time;
|
||||
if (updated && new Date().getTime() - updated <= this.expires) {
|
||||
return this.resourceCache[id].time;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
set(id, value) {
|
||||
this.resourceCache[id] = { time: new Date().getTime(), data: value };
|
||||
set(id, value, ttl) {
|
||||
const resolvedTTL = normalizeTTL(ttl) ?? getTTL();
|
||||
this.resourceCache[id] = {
|
||||
time: Date.now() + resolvedTTL,
|
||||
data: value,
|
||||
};
|
||||
this._persist();
|
||||
}
|
||||
}
|
||||
|
||||
function getExpiredTime() {
|
||||
// console.log($.read(CSR_EXPIRATION_TIME_KEY));
|
||||
if (!$.read(CSR_EXPIRATION_TIME_KEY)) {
|
||||
$.write('1728e5', CSR_EXPIRATION_TIME_KEY); // 48 * 3600 * 1000
|
||||
}
|
||||
let expiration = 1728e5;
|
||||
if ($.env.isLoon) {
|
||||
const loont = {
|
||||
// Loon 插件自义定
|
||||
'1\u5206\u949f': 6e4,
|
||||
'5\u5206\u949f': 3e5,
|
||||
'10\u5206\u949f': 6e5,
|
||||
'30\u5206\u949f': 18e5, // "30分钟"
|
||||
'1\u5c0f\u65f6': 36e5,
|
||||
'2\u5c0f\u65f6': 72e5,
|
||||
'3\u5c0f\u65f6': 108e5,
|
||||
'6\u5c0f\u65f6': 216e5,
|
||||
'12\u5c0f\u65f6': 432e5,
|
||||
'24\u5c0f\u65f6': 864e5,
|
||||
'48\u5c0f\u65f6': 1728e5,
|
||||
'72\u5c0f\u65f6': 2592e5, // "72小时"
|
||||
'\u53c2\u6570\u4f20\u5165': 'readcachets', // "参数输入"
|
||||
};
|
||||
let intimed = $.read('#\u8282\u70b9\u7f13\u5b58\u6709\u6548\u671f'); // Loon #节点缓存有效期
|
||||
// console.log(intimed);
|
||||
if (intimed in loont) {
|
||||
expiration = loont[intimed];
|
||||
if (expiration === 'readcachets') {
|
||||
expiration = intimed;
|
||||
}
|
||||
function normalizeTTL(ttl) {
|
||||
const value = Number(ttl);
|
||||
if (!isFinite(value)) return null;
|
||||
if (value > 0) return value;
|
||||
return null;
|
||||
}
|
||||
|
||||
function getTTL() {
|
||||
const settings = $.read(SETTINGS_KEY);
|
||||
let ttl = settings?.scriptCacheTtl;
|
||||
if (ttl) {
|
||||
ttl = Number(ttl);
|
||||
if (isFinite(ttl) && ttl > 0) {
|
||||
return ttl * 1000;
|
||||
}
|
||||
return expiration;
|
||||
} else {
|
||||
expiration = $.read(CSR_EXPIRATION_TIME_KEY);
|
||||
return expiration;
|
||||
}
|
||||
return DEFAULT_SCRIPT_CACHE_TTL;
|
||||
}
|
||||
|
||||
export default new ResourceCache();
|
||||
@@ -4,8 +4,6 @@
|
||||
#!author=Peng-YM
|
||||
#!homepage=https://github.com/sub-store-org/Sub-Store
|
||||
#!icon=https://raw.githubusercontent.com/58xinian/icon/master/Sub-Store1.png
|
||||
#!select = 节点缓存有效期,1分钟,5分钟,10分钟,30分钟,1小时,2小时,3小时,6小时,12小时,24小时,48小时,72小时,参数传入
|
||||
#!select = 响应头缓存有效期,1分钟,5分钟,10分钟,30分钟,1小时,2小时,3小时,6小时,12小时,24小时,48小时,72小时,参数传入
|
||||
|
||||
[Argument]
|
||||
cron=input, "55 23 * * *", tag=定时参数, desc=这里需要输入符合CRON表达式的参数
|
||||
|
||||
+20
-11
@@ -103,32 +103,41 @@ function operator(proxies = [], targetPlatform, context) {
|
||||
// scriptResourceCache 缓存
|
||||
// 可参考 https://t.me/zhetengsha/1003
|
||||
// const cache = scriptResourceCache
|
||||
// 设置
|
||||
// cache.set('a:1', 1)
|
||||
// 写入
|
||||
// 第三个参数为自定义过期时间(单位: 毫秒)
|
||||
// cache.set('a:1', 1, 1000)
|
||||
// cache.set('a:2', 2)
|
||||
// 获取
|
||||
// cache.get('a:1')
|
||||
// 支持第二个参数: 自定义过期时间
|
||||
// 获取到期时间
|
||||
// cache.gettime('a:1')
|
||||
// 支持第二个参数: 自定义过期时间(单位: 毫秒)
|
||||
// 支持第三个参数: 是否删除过期项
|
||||
// 下面的例子意思是原来是看 a:2 现在有没有到期的, 加了自定义过期时间后是看 +1000ms 会不会过期, 如果过期就删除
|
||||
// cache.get('a:2', 1000, true)
|
||||
|
||||
// 清理
|
||||
// 本来是内部的 反正也能用...先这么用吧...
|
||||
// 清理所有过期的
|
||||
// cache._cleanup()
|
||||
// 支持第一个参数: 匹配前缀的项也一起删除
|
||||
// 支持第二个参数: 自定义过期时间
|
||||
// cache._cleanup('a:', 1000)
|
||||
// 支持第一个参数: 匹配前缀的项
|
||||
// 支持第二个参数: 自定义过期时间(单位: 毫秒)
|
||||
// 只清理 a: 开头的过期项
|
||||
// cache._cleanup('a:')
|
||||
// 如果想删除所有的 a: 开头的过期项, 目前先传一个大的过期时间吧...
|
||||
// cache._cleanup(undefined, 48 * 3600 * 1000)
|
||||
// 下面的例子意思是原来是看现在有没有到期的, 加了自定义过期时间后是看 +1000ms 会不会过期, 如果过期就删除
|
||||
// cache._cleanup(undefined, 1000)
|
||||
|
||||
// 关于缓存时长
|
||||
|
||||
// 拉取 Sub-Store 订阅时, 会自动拉取远程订阅
|
||||
|
||||
// 远程订阅缓存是 1 小时, 缓存的唯一 key 为 url+ user agent. 可通过前端的刷新按钮刷新缓存. 或使用参数 noCache 来禁用缓存. 例: 内部配置订阅链接时使用 http://a.com#noCache, 外部使用 sub-store 链接时使用 https://sub.store/download/1?noCache=true
|
||||
// 通过链接下载资源时, 缓存的唯一 key 为 url+ user agent. 可通过前端的刷新按钮刷新缓存. 或使用参数 noCache 来禁用缓存. 例: 内部配置订阅链接时使用 http://a.com#noCache, 外部使用 sub-store 链接时使用 https://sub.store/download/1?noCache=true
|
||||
|
||||
// 当使用相关脚本时, 若在对应的脚本中使用参数开启缓存, 可设置持久化缓存 sub-store-csr-expiration-time 的值来自定义默认缓存时长, 默认为 172800000 (48 * 3600 * 1000, 即 48 小时)
|
||||
// 前端(>= 2.16.0) 后端(>= 2.21.0) 支持自定义各种缓存的 TTL 配置
|
||||
|
||||
// 🎈Loon 可在插件中设置
|
||||
|
||||
// 其他平台同理, 持久化缓存数据在 JSON 里
|
||||
// 持久化缓存数据在 JSON 里
|
||||
|
||||
// 当配合脚本使用时, 可以在脚本的前面添加一个脚本操作, 实现保留 1 小时的缓存. 这样比较灵活
|
||||
|
||||
|
||||
Reference in new issue
Block a user