@@ -169,39 +169,62 @@ export function handleClearLogs() {
169169
170170/**
171171 * 处理清理缓存的请求
172- * @returns {Response } 表示操作成功的响应
172+ * @param {Request } [req] 可选请求体 { items: string[] },用于指定待清理项;未提供时清理全部已知项
173+ * @returns {Response } 表示操作结果的响应
173174 */
174- export async function handleClearCache ( ) {
175- try {
176- // 清理 globals 中的缓存数据
177- globals . animes = [ ] ;
178- globals . episodeIds = [ ] ;
179- globals . episodeNum = 10001 ; // 重置为初始值
180- globals . lastSelectMap = new Map ( ) ; // 重新创建 Map 对象
181- globals . reqRecords = [ ] ; // 清空请求记录
182- globals . todayReqNum = 0 ; // 重置今日请求次数
183-
175+ export async function handleClearCache ( req ) {
176+ // 各清理项对应的重置逻辑;请求记录与今日计数归入「请求历史记录」项
177+ const clearActions = {
178+ animes : ( ) => { globals . animes = [ ] ; } ,
179+ episodeIds : ( ) => { globals . episodeIds = [ ] ; } ,
180+ episodeNum : ( ) => { globals . episodeNum = 10001 ; } , // 重置为初始值
181+ lastSelectMap : ( ) => { globals . lastSelectMap = new Map ( ) ; } , // 重新创建 Map 对象
184182 // 清理搜索和弹幕缓存
185- globals . searchCache = new Map ( ) ;
186- globals . commentCache = new Map ( ) ;
187- globals . requestHistory = new Map ( ) ;
183+ searchCache : ( ) => { globals . searchCache = new Map ( ) ; } ,
184+ commentCache : ( ) => { globals . commentCache = new Map ( ) ; } ,
185+ requestHistory : ( ) => {
186+ globals . requestHistory = new Map ( ) ;
187+ globals . reqRecords = [ ] ; // 清空请求记录
188+ globals . todayReqNum = 0 ; // 重置今日请求次数
189+ } ,
190+ bangumiData : ( ) => {
191+ try {
192+ clearBangumiDataCache ( true ) ; // 清理 Bangumi-Data 内存与磁盘缓存
193+ if ( globals . useBangumiData ) {
194+ // 触发异步数据重载
195+ initBangumiData ( globals . deployPlatform , false ) . catch ( e => {
196+ log ( "warn" , `[system] [server] Bangumi-Data background reload failed: ${ e . message } ` ) ;
197+ } ) ;
198+ }
199+ } catch ( e ) {
200+ log ( "error" , `[system] [server] Failed to clear Bangumi-Data cache: ${ e . message } ` ) ;
201+ }
202+ }
203+ } ;
204+ const allItems = Object . keys ( clearActions ) ;
188205
206+ let effectiveItems ;
207+ if ( req ) {
208+ let parsed = null ;
189209 try {
190- // 清理 Bangumi-Data 内存与磁盘缓存
191- clearBangumiDataCache ( true ) ;
192-
193- // 触发异步数据重载
194- if ( globals . useBangumiData ) {
195- initBangumiData ( globals . deployPlatform , false ) . catch ( e => {
196- log ( "warn" , `[system] [server] Bangumi-Data background reload failed: ${ e . message } ` ) ;
197- } ) ;
198- }
210+ parsed = await req . json ( ) ;
199211 } catch ( e ) {
200- log ( "error" , `[system] [server] Failed to clear Bangumi-Data cache: ${ e . message } ` ) ;
212+ parsed = null ;
201213 }
202-
214+ const items = parsed && Array . isArray ( parsed . items ) ? parsed . items : null ;
215+ // 仅保留自身属性键,排除 __proto__ 等原型链键,避免误放行导致整次清理失败
216+ effectiveItems = items ? items . filter ( key => Object . prototype . hasOwnProperty . call ( clearActions , key ) ) : allItems ;
217+ } else {
218+ effectiveItems = allItems ;
219+ }
220+
221+ try {
222+ for ( const key of effectiveItems ) {
223+ clearActions [ key ] ( ) ;
224+ }
225+
203226 log ( "info" , `[system] [server] Memory cache cleared successfully` ) ;
204-
227+
205228 // 同步清理本地缓存和Redis缓存
206229 try {
207230 // 如果本地缓存有效,更新本地缓存
@@ -213,7 +236,7 @@ export async function handleClearCache() {
213236 } catch ( localError ) {
214237 log ( "warn" , `[system] [server] Local cache may not be available: ${ localError . message } ` ) ;
215238 }
216-
239+
217240 try {
218241 // 如果Redis有效,更新Redis缓存
219242 if ( globals . redisValid ) {
@@ -235,19 +258,22 @@ export async function handleClearCache() {
235258 } catch ( redisError ) {
236259 log ( "warn" , `[system] [server] LocalRedis may not be available: ${ redisError . message } ` ) ;
237260 }
238-
239- log ( "info" , `[system] [server] All caches cleared successfully` ) ;
240- return jsonResponse ( { success : true , message : "Cache cleared successfully" , clearedItems : {
241- animes : 0 ,
242- episodeIds : 0 ,
243- episodeNum : 10001 ,
244- lastSelectMap : 0 ,
245- searchCache : 0 ,
246- commentCache : 0 ,
247- requestHistory : 0 ,
248- reqRecords : 0 ,
249- todayReqNum : 0
250- } } , 200 ) ;
261+
262+ const clearedItems = { } ;
263+ for ( const key of effectiveItems ) {
264+ if ( key === "requestHistory" ) {
265+ clearedItems . requestHistory = 0 ;
266+ clearedItems . reqRecords = 0 ;
267+ clearedItems . todayReqNum = 0 ;
268+ } else if ( key === "episodeNum" ) {
269+ clearedItems . episodeNum = 10001 ;
270+ } else {
271+ clearedItems [ key ] = 0 ;
272+ }
273+ }
274+
275+ log ( "info" , `[system] [server] Selected caches cleared successfully` ) ;
276+ return jsonResponse ( { success : true , message : "Cache cleared successfully" , clearedItems } , 200 ) ;
251277 } catch ( error ) {
252278 log ( "error" , `[system] [server] Cache clear failed: ${ error . message } ` ) ;
253279 return jsonResponse ( { success : false , message : `Cache clear failed: ${ error . message } ` } , 500 ) ;
0 commit comments