enhance swap check

This commit is contained in:
shenjack 2023-11-05 19:01:45 +08:00
parent 1a345ff726
commit e9b8fd28b1
Signed by: shenjack
GPG Key ID: 7B1134A979775551

View File

@ -173,7 +173,11 @@ class FileCacheOutputStream(BaseOutputStream):
def check_flush(self) -> Path: def check_flush(self) -> Path:
current_file = self.get_file_path() current_file = self.get_file_path()
if self.file_swap: # 获取当前文件的路径
if not self.file_swap:
# 不需要 swap 直接返回
return current_file
# 检查是否需要 swap
size_pass = True size_pass = True
if self.file_size_limit > 0: if self.file_size_limit > 0:
file_size = current_file.stat().st_size / 1024 # kb file_size = current_file.stat().st_size / 1024 # kb
@ -184,11 +188,14 @@ class FileCacheOutputStream(BaseOutputStream):
file_time = round(time.time()) - current_file.stat().st_mtime file_time = round(time.time()) - current_file.stat().st_mtime
if file_time > self.file_time_limit: if file_time > self.file_time_limit:
time_pass = False time_pass = False
if self.file_swap_on_both: if (self.file_swap_on_both and size_pass and time_pass) or \
(not self.file_swap_on_both and (size_pass or time_pass)):
# 两个都满足
# 或者只有一个满足
if size_pass and time_pass: if size_pass and time_pass:
self.file_swap_counter += 1 self.file_swap_counter += 1
# 生成新的文件名 # 生成新的文件名
return current_file return self.get_file_path()
def flush(self) -> None: def flush(self) -> None:
new_cache = io.StringIO() # 创建新的缓存 new_cache = io.StringIO() # 创建新的缓存