加入图片缓存,每次从文件系统中提取相关的图片,加载成bytes,后续使用时直接从缓存中提取。减少IO读取次数,提高发送性能
This commit is contained in:
55
plugins/xiuren_image/test_cache.py
Normal file
55
plugins/xiuren_image/test_cache.py
Normal file
@@ -0,0 +1,55 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
测试图片缓存功能
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..'))
|
||||
|
||||
from images_cache import ImageCacheManager
|
||||
import time
|
||||
|
||||
def test_cache_functionality():
|
||||
"""测试缓存功能"""
|
||||
print("开始测试图片缓存功能...")
|
||||
|
||||
# 初始化缓存管理器
|
||||
image_folder = "/mnt/nfs_share" # 根据实际路径调整
|
||||
cache_size = 10
|
||||
|
||||
try:
|
||||
cache_manager = ImageCacheManager(image_folder, cache_size)
|
||||
print(f"缓存管理器初始化成功,缓存大小:{cache_size}")
|
||||
|
||||
# 测试获取缓存图片
|
||||
print("\n测试获取缓存图片...")
|
||||
for i in range(10):
|
||||
print(f"\n第 {i+1} 次获取图片:")
|
||||
|
||||
try:
|
||||
# 获取缓存图片
|
||||
cached_image = cache_manager.get_cached_image_bytes()
|
||||
if cached_image:
|
||||
print(f" ✓ 成功获取图片")
|
||||
print(f" 路径: {cached_image['path']}")
|
||||
print(f" 大小: {len(cached_image['bytes'])} bytes")
|
||||
print(f" 当前缓存数量: {cache_manager.get_cached_image_count()}")
|
||||
else:
|
||||
print(f" ✗ 获取图片失败")
|
||||
except Exception as e:
|
||||
print(f" ✗ 获取图片时出错: {e}")
|
||||
|
||||
# 模拟请求间隔
|
||||
time.sleep(0.5)
|
||||
|
||||
print("\n缓存测试完成!")
|
||||
|
||||
except Exception as e:
|
||||
print(f"测试过程中出现错误: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_cache_functionality()
|
||||
Reference in New Issue
Block a user