11import $ from '@/core/app' ;
22import {
33 HEADERS_RESOURCE_CACHE_KEY ,
4- CHR_EXPIRATION_TIME_KEY ,
4+ DEFAULT_HEADERS_CACHE_TTL ,
5+ SETTINGS_KEY ,
56} from '@/constants' ;
67
78class ResourceCache {
89 constructor ( ) {
9- this . expires = getExpiredTime ( ) ;
1010 if ( ! $ . read ( HEADERS_RESOURCE_CACHE_KEY ) ) {
1111 $ . write ( '{}' , HEADERS_RESOURCE_CACHE_KEY ) ;
1212 }
@@ -24,18 +24,15 @@ class ResourceCache {
2424 this . _cleanup ( ) ;
2525 }
2626
27- _cleanup ( ) {
28- // clear obsolete cached resource
27+ _cleanup ( prefix , ttl ) {
28+ const resolvedTTL = normalizeTTL ( ttl ) ?? 0 ;
2929 let clear = false ;
30+ const now = Date . now ( ) ;
3031 Object . entries ( this . resourceCache ) . forEach ( ( entry ) => {
31- const [ id , updated ] = entry ;
32- if ( ! updated . time ) {
33- // clear old version cache
34- delete this . resourceCache [ id ] ;
35- $ . delete ( `#${ id } ` ) ;
36- clear = true ;
37- }
38- if ( new Date ( ) . getTime ( ) - updated . time > this . expires ) {
32+ const [ id , cached ] = entry ;
33+ const shouldDelete =
34+ ! cached . time || cached . time < now + resolvedTTL ;
35+ if ( shouldDelete && ( prefix ? id . startsWith ( prefix ) : true ) ) {
3936 delete this . resourceCache [ id ] ;
4037 clear = true ;
4138 }
@@ -52,66 +49,55 @@ class ResourceCache {
5249 $ . write ( JSON . stringify ( this . resourceCache ) , HEADERS_RESOURCE_CACHE_KEY ) ;
5350 }
5451
55- get ( id ) {
56- const updated = this . resourceCache [ id ] && this . resourceCache [ id ] . time ;
57- if ( updated && new Date ( ) . getTime ( ) - updated <= this . expires ) {
58- return this . resourceCache [ id ] . data ;
52+ gettime ( id ) {
53+ const time = this . resourceCache [ id ] && this . resourceCache [ id ] . time ;
54+ if ( time && new Date ( ) . getTime ( ) <= time ) {
55+ return this . resourceCache [ id ] . time ;
5956 }
6057 return null ;
6158 }
6259
63- gettime ( id ) {
64- const updated = this . resourceCache [ id ] && this . resourceCache [ id ] . time ;
65- if ( updated && new Date ( ) . getTime ( ) - updated <= this . expires ) {
66- return this . resourceCache [ id ] . time ;
60+ get ( id , ttl , remove ) {
61+ const resolvedTTL = normalizeTTL ( ttl ) ?? 0 ;
62+ const cached = this . resourceCache [ id ] ;
63+ const time = cached && cached . time ;
64+ if ( time ) {
65+ if ( Date . now ( ) + resolvedTTL <= time ) return cached . data ;
66+ if ( remove ) {
67+ delete this . resourceCache [ id ] ;
68+ this . _persist ( ) ;
69+ }
6770 }
6871 return null ;
6972 }
7073
71- set ( id , value ) {
72- this . resourceCache [ id ] = { time : new Date ( ) . getTime ( ) , data : value } ;
74+ set ( id , value , ttl ) {
75+ const resolvedTTL = normalizeTTL ( ttl ) ?? getTTL ( ) ;
76+ this . resourceCache [ id ] = {
77+ time : Date . now ( ) + resolvedTTL ,
78+ data : value ,
79+ } ;
7380 this . _persist ( ) ;
7481 }
7582}
7683
77- function getExpiredTime ( ) {
78- // console.log($.read(CHR_EXPIRATION_TIME_KEY));
79- if ( ! $ . read ( CHR_EXPIRATION_TIME_KEY ) ) {
80- $ . write ( '6e4' , CHR_EXPIRATION_TIME_KEY ) ; // 1分钟
81- }
82- let expiration = 6e4 ;
83- if ( $ . env . isLoon ) {
84- const loont = {
85- // Loon 插件自义定
86- '1\u5206\u949f' : 6e4 ,
87- '5\u5206\u949f' : 3e5 ,
88- '10\u5206\u949f' : 6e5 ,
89- '30\u5206\u949f' : 18e5 , // "30分钟"
90- '1\u5c0f\u65f6' : 36e5 ,
91- '2\u5c0f\u65f6' : 72e5 ,
92- '3\u5c0f\u65f6' : 108e5 ,
93- '6\u5c0f\u65f6' : 216e5 ,
94- '12\u5c0f\u65f6' : 432e5 ,
95- '24\u5c0f\u65f6' : 864e5 ,
96- '48\u5c0f\u65f6' : 1728e5 ,
97- '72\u5c0f\u65f6' : 2592e5 , // "72小时"
98- '\u53c2\u6570\u4f20\u5165' : 'readcachets' , // "参数输入"
99- } ;
100- let intimed = $ . read (
101- '#\u54cd\u5e94\u5934\u7f13\u5b58\u6709\u6548\u671f' ,
102- ) ; // Loon #响应头缓存有效期
103- // console.log(intimed);
104- if ( intimed in loont ) {
105- expiration = loont [ intimed ] ;
106- if ( expiration === 'readcachets' ) {
107- expiration = intimed ;
108- }
84+ function normalizeTTL ( ttl ) {
85+ const value = Number ( ttl ) ;
86+ if ( ! isFinite ( value ) ) return null ;
87+ if ( value > 0 ) return value ;
88+ return null ;
89+ }
90+
91+ function getTTL ( ) {
92+ const settings = $ . read ( SETTINGS_KEY ) ;
93+ let ttl = settings ?. headersCacheTtl ;
94+ if ( ttl ) {
95+ ttl = Number ( ttl ) ;
96+ if ( isFinite ( ttl ) && ttl > 0 ) {
97+ return ttl * 1000 ;
10998 }
110- return expiration ;
111- } else {
112- expiration = $ . read ( CHR_EXPIRATION_TIME_KEY ) ;
113- return expiration ;
11499 }
100+ return DEFAULT_HEADERS_CACHE_TTL ;
115101}
116102
117103export default new ResourceCache ( ) ;
0 commit comments