""" 测试缓存功能 """ import time from core.cache import Cache print('测试缓存功能...') # 测试1: 基本操作 print('\n1. 测试基本操作:') cache = Cache() cache.set("test_key", "test_value") value = cache.get("test_key") print(f' 写入: test_key = test_value') print(f' 读取: {value}') print(f' 匹配: {value == "test_value"}') # 测试2: 缓存过期 print('\n2. 测试缓存过期:') cache.set("expire_key", "expire_value", ttl=1) value = cache.get("expire_key") print(f' 立即读取: {value}') time.sleep(1.5) value = cache.get("expire_key") print(f' 过期后读取: {value}') print(f' 过期正常: {value is None}') # 测试3: 缓存清理 print('\n3. 测试缓存清理:') cache.set("key1", "value1") cache.set("key2", "value2") print(f' 清理前数量: 2') cache.clear() value1 = cache.get("key1") value2 = cache.get("key2") print(f' 清理后: key1={value1}, key2={value2}') print(f' 清理正常: {value1 is None and value2 is None}') # 测试4: 缓存统计 print('\n4. 测试缓存统计:') cache.set("key1", "value1") cache.set("key2", "value2") cache.set("key3", "value3") stats = cache.get_stats() print(f' 统计信息: {stats}') print(f' 大小正确: {stats.get("size") == 3}') print('\n✅ 缓存功能测试通过!')