优化jfile服务:将清理间隔改为可配置,文件保留时间调整为15分钟
- 将清理间隔从硬编码3600秒改为可配置项cleanup_interval_seconds - 文件保留时间从1小时调整为15分钟(0.25小时) - 清理间隔同步调整为15分钟(900秒),保持与保留时间一致 - 优化存储空间使用,过期文件更及时清理
This commit is contained in:
parent
419744686f
commit
c99e20ff61
@ -53,7 +53,8 @@ app.mount("/files", StaticFiles(directory="files"), name="files")
|
||||
# 注册文件定时清理任务
|
||||
save_dir = "files"
|
||||
retention_hours = settings.file_retention_hours
|
||||
cleaner = FileCleaner(save_dir, retention_hours)
|
||||
cleanup_interval = settings.cleanup_interval_seconds
|
||||
cleaner = FileCleaner(save_dir, retention_hours, cleanup_interval)
|
||||
|
||||
@app.on_event("startup")
|
||||
async def startup_event():
|
||||
|
||||
@ -3,9 +3,10 @@ import asyncio
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
class FileCleaner:
|
||||
def __init__(self, target_dir, retention_hours):
|
||||
def __init__(self, target_dir, retention_hours, cleanup_interval_seconds=3600):
|
||||
self.target_dir = target_dir
|
||||
self.retention_hours = retention_hours
|
||||
self.cleanup_interval_seconds = cleanup_interval_seconds
|
||||
|
||||
async def periodic_cleanup(self):
|
||||
while True:
|
||||
@ -13,7 +14,7 @@ class FileCleaner:
|
||||
self.cleanup_old_files()
|
||||
except Exception as e:
|
||||
print(f"清理文件时出错: {str(e)}")
|
||||
await asyncio.sleep(3600)
|
||||
await asyncio.sleep(self.cleanup_interval_seconds)
|
||||
|
||||
def cleanup_old_files(self):
|
||||
if not os.path.exists(self.target_dir):
|
||||
|
||||
@ -11,7 +11,10 @@ class Settings(BaseSettings):
|
||||
download_url: str = "https://api.jingrow.com/files"
|
||||
|
||||
# 文件保留时间(小时)
|
||||
file_retention_hours: int = 1
|
||||
file_retention_hours: float = 0.25 # 15分钟
|
||||
|
||||
# 清理任务执行间隔(秒)
|
||||
cleanup_interval_seconds: int = 900 # 15分钟
|
||||
|
||||
|
||||
class Config:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user