@@ -2,7 +2,6 @@ import { existsSync, createWriteStream } from "fs";
22import { unlink , rename , stat } from "fs/promises" ;
33import { pipeline } from "stream/promises" ;
44import { CacheService } from "./CacheService" ;
5- import { useStore } from "../store" ;
65import { cacheLog } from "../logger" ;
76import got from "got" ;
87
@@ -54,9 +53,7 @@ export class MusicCacheService {
5453 const items = await this . cacheService . list ( "music" ) ;
5554 // 查找以 id_ 开头且以 .sc 结尾的文件(排除 .tmp 文件)
5655 const prefix = `${ id } _` ;
57- const match = items . find (
58- ( item ) => item . key . startsWith ( prefix ) && item . key . endsWith ( ".sc" ) ,
59- ) ;
56+ const match = items . find ( ( item ) => item . key . startsWith ( prefix ) && item . key . endsWith ( ".sc" ) ) ;
6057 if ( match ) {
6158 return this . cacheService . getFilePath ( "music" , match . key ) ;
6259 }
@@ -74,30 +71,16 @@ export class MusicCacheService {
7471 * @returns 缓存后的本地文件路径
7572 */
7673 public async cacheMusic ( id : number | string , url : string , quality : string ) : Promise < string > {
77- const store = useStore ( ) ;
78- const limitSizeGB = store . get ( "cacheLimit" ) ?? 10 ;
79- const limitSizeBytes = limitSizeGB * 1024 * 1024 * 1024 ;
80-
81- // 如果设置为 0,则不限制
82- if ( limitSizeGB > 0 ) {
83- const currentSize = await this . cacheService . getSize ( ) ;
84-
85- if ( currentSize > limitSizeBytes ) {
86- // 腾出至少 100MB 空间
87- await this . cacheService . cleanOldCache (
88- "music" ,
89- currentSize - limitSizeBytes + 100 * 1024 * 1024 ,
90- ) ;
91- }
92- }
93-
9474 const key = this . getCacheKey ( id , quality ) ;
9575 const filePath = this . cacheService . getFilePath ( "music" , key ) ;
9676 const tempPath = `${ filePath } .tmp` ;
9777
9878 // 确保目录存在
9979 await this . cacheService . init ( ) ;
10080
81+ // 检查并清理超限缓存
82+ await this . cacheService . checkAndCleanCache ( ) ;
83+
10184 // 下载并写入
10285 try {
10386 const downloadStream = got . stream ( url ) ;
0 commit comments