Repository navigation
Expand file tree
/
Copy pathcache.py
More file actions
2060 lines (1730 loc) · 77.2 KB
/
Copy pathcache.py
File metadata and controls
2060 lines (1730 loc) · 77.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
企业级智能缓存系统 - 高性能数据库缓存解决方案
完整的企业级缓存系统,集成了智能缓存管理、内存压力感知、多区域隔离、
性能监控、自适应优化、动态弱引用保护和分级缓存等高级特性。为MySQL数据库操作
提供高效、可靠的缓存支持,显著提升系统性能和响应速度。
@fileoverview 企业级智能缓存系统 - MySQL数据库高性能缓存解决方案
@author liyq
@version 1.0.0
@since 1.0.0
@updated 2025-09-26
@license MIT
"""
import asyncio
import time
from enum import Enum
from typing import Any, Dict, List, Optional, TypeVar, Generic, Callable, Tuple
from dataclasses import dataclass, field
from weakref import WeakValueDictionary, ref
from common_utils import TimeUtils
from constants import DefaultConfig, StringConstants
from config import CacheConfig
from logger import logger
from memory_pressure_manager import MemoryPressureObserver, memory_pressure_manager
T = TypeVar('T')
# =============================================================================
# 缓存相关类型定义
# =============================================================================
class CacheRegion(str, Enum):
"""
缓存区域枚举
定义了系统中不同类型的缓存区域,每个区域存储特定类型的数据:
- SCHEMA: 数据库模式信息缓存
- TABLE_EXISTS: 表存在性检查缓存
- INDEX: 索引信息缓存
- QUERY_RESULT: 查询结果缓存
"""
SCHEMA = "schema"
TABLE_EXISTS = "table_exists"
INDEX = "index"
QUERY_RESULT = "query_result"
class QueryType(str, Enum):
"""
查询类型枚举
定义了支持缓存的不同SQL查询类型:
- SELECT: 查询操作,优先级最高
- SHOW: 展示信息查询,如SHOW TABLES
- DESCRIBE: 描述表结构查询
- EXPLAIN: 查询执行计划
- INSERT/UPDATE/DELETE/CREATE/DROP/ALTER: 数据修改操作,通常不缓存或缓存时间较短
"""
SELECT = "SELECT"
INSERT = "INSERT"
UPDATE = "UPDATE"
DELETE = "DELETE"
CREATE = "CREATE"
DROP = "DROP"
ALTER = "ALTER"
SHOW = "SHOW"
DESCRIBE = "DESCRIBE"
EXPLAIN = "EXPLAIN"
class OperationType(str, Enum):
"""操作类型枚举"""
DDL = "DDL"
DML = "DML"
CREATE = "CREATE"
DROP = "DROP"
ALTER = "ALTER"
INSERT = "INSERT"
UPDATE = "UPDATE"
DELETE = "DELETE"
@dataclass
class CacheEntry(Generic[T]):
"""
缓存条目数据类
表示缓存中的单个条目,包含数据内容和元数据信息:
- data: 实际的缓存数据
- created_at: 条目创建时间戳
- access_count: 访问计数,用于LRU算法
- last_accessed: 最后访问时间戳
"""
data: T
created_at: float
access_count: int = 0
last_accessed: float = field(default_factory=time.time)
@dataclass
class QueryCacheEntry:
"""
查询缓存条目数据类
专门用于查询结果缓存的条目结构:
- data: 查询结果数据
- metadata: 查询元数据信息
- expires_at: 过期时间戳
"""
data: Any
metadata: 'QueryCacheMetadata'
expires_at: float
@dataclass
class QueryCacheMetadata:
"""
查询缓存元数据类
存储查询缓存的详细信息,用于缓存管理和失效策略:
- query_type: 查询类型(SELECT、SHOW等)
- tables: 涉及的表名列表
- complexity: 查询复杂度评分
- result_size: 结果数据大小
- created_at: 创建时间
- last_accessed: 最后访问时间
- access_count: 访问计数
"""
query_type: QueryType
tables: List[str]
complexity: int
result_size: int
created_at: float
last_accessed: float
access_count: int
@dataclass
class CacheRegionStats:
"""
缓存区域统计数据类
提供特定缓存区域的详细统计信息:
- region: 缓存区域类型
- size: 当前缓存条目数量
- max_size: 最大容量
- dynamic_max_size: 动态最大容量(根据内存压力调整)
- hit_count: 命中次数
- miss_count: 未命中次数
- hit_rate: 命中率
- ttl: 生存时间
"""
region: CacheRegion
size: int
max_size: int
dynamic_max_size: int
hit_count: int
miss_count: int
hit_rate: float
ttl: int
@dataclass
class QueryCacheStats:
"""
查询缓存统计数据类
提供查询缓存的详细统计信息:
- total_queries: 总查询次数
- cache_hits: 缓存命中次数
- cache_misses: 缓存未命中次数
- hit_rate: 缓存命中率
- skipped_queries: 被跳过的查询次数
- current_entries: 当前缓存条目数
- cache_size: 缓存大小(字节)
- type_stats: 按查询类型统计的详细数据
"""
total_queries: int = 0
cache_hits: int = 0
cache_misses: int = 0
hit_rate: float = 0.0
skipped_queries: int = 0
current_entries: int = 0
cache_size: int = 0
type_stats: Dict[QueryType, Dict[str, int]] = field(default_factory=dict)
class InvalidationStrategy:
"""
缓存失效策略类
定义缓存失效的策略和规则:
- operation_type: 触发失效的操作类型
- clear_all: 是否清除所有缓存
- regions: 需要清除的特定区域列表
- table_specific: 是否针对特定表进行失效
用于根据数据库操作智能地失效相关缓存数据
"""
def __init__(
self,
operation_type: OperationType,
clear_all: bool = False,
regions: Optional[List[CacheRegion]] = None,
table_specific: bool = False
):
self.operation_type = operation_type
self.clear_all = clear_all
self.regions = regions or []
self.table_specific = table_specific
class QueryCacheConfig:
"""
查询缓存配置类
定义查询缓存的行为和参数:
- enabled: 是否启用查询缓存
- default_ttl: 默认缓存时间(秒)
- max_size: 最大缓存条目数
- type_ttl: 按查询类型设置的TTL映射
- cacheable_patterns: 可缓存查询的正则表达式模式
- non_cacheable_patterns: 不可缓存查询的正则表达式模式
- max_result_size: 最大结果大小限制
- max_key_length: 最大缓存键长度
"""
def __init__(
self,
enabled: bool = True,
default_ttl: int = 300,
max_size: int = 1000,
type_ttl: Optional[Dict[QueryType, int]] = None,
cacheable_patterns: Optional[List[str]] = None,
non_cacheable_patterns: Optional[List[str]] = None,
max_result_size: int = 1024 * 1024,
max_key_length: int = 500
):
self.enabled = enabled
self.default_ttl = default_ttl
self.max_size = max_size
self.type_ttl = type_ttl or {
QueryType.SELECT: 300,
QueryType.SHOW: 600,
QueryType.DESCRIBE: 1800,
QueryType.EXPLAIN: 900,
QueryType.INSERT: 0,
QueryType.UPDATE: 0,
QueryType.DELETE: 0,
QueryType.CREATE: 0,
QueryType.DROP: 0,
QueryType.ALTER: 0
}
self.cacheable_patterns = cacheable_patterns or [
r'^SELECT\s+.*\s+FROM\s+\w+(\s+WHERE\s+.*)?(\s+ORDER\s+BY\s+.*)?(\s+LIMIT\s+\d+)?$',
r'^SHOW\s+(TABLES|COLUMNS|INDEX|STATUS)',
r'^DESCRIBE\s+\w+$',
r'^EXPLAIN\s+SELECT'
]
self.non_cacheable_patterns = non_cacheable_patterns or [
r'NOW\(\)|CURRENT_TIMESTAMP|RAND\(\)|UUID\(\)',
r'LAST_INSERT_ID\(\)',
r'CONNECTION_ID\(\)',
r'USER\(\)|DATABASE\(\)',
r'FOR\s+UPDATE',
r'LOCK\s+IN\s+SHARE\s+MODE'
]
self.max_result_size = max_result_size
self.max_key_length = max_key_length
self._compiled_patterns = self._compile_patterns()
def _compile_patterns(self) -> Tuple[List, List]:
"""编译正则表达式模式"""
import re
compiled_cacheable = [re.compile(pattern, re.IGNORECASE) for pattern in self.cacheable_patterns]
compiled_non_cacheable = [re.compile(pattern, re.IGNORECASE) for pattern in self.non_cacheable_patterns]
return compiled_cacheable, compiled_non_cacheable
# =============================================================================
# 智能缓存核心类
# =============================================================================
class SmartCache(Generic[T]):
"""
智能缓存类 - 企业级高性能缓存解决方案
提供完整的缓存功能,包括:
- 多层级缓存架构(L1/L2缓存)
- 弱引用保护机制
- 内存压力感知
- TTL动态调整
- 智能预取功能
- 缓存预热机制
- 批量操作支持
- 详细统计信息
支持泛型类型,适用于各种数据类型的缓存存储。
"""
def __init__(
self,
max_size: int,
ttl: int = DefaultConfig.CACHE_TTL,
enable_weak_ref_protection: bool = False,
enable_tiered_cache: bool = False
):
self.max_size = max_size
self.ttl = ttl
self.dynamic_max_size = max_size
self.cache: Dict[str, CacheEntry[T]] = {}
self.lock = asyncio.Lock()
self.hit_count = 0
self.miss_count = 0
self.enable_weak_ref_protection = enable_weak_ref_protection
# WeakRef防护相关
if enable_weak_ref_protection:
self.weak_cache = WeakValueDictionary()
self.weak_ref_registry: Dict[str, ref] = {}
# 分级缓存配置和状态
self.tiered_cache_config = {
'enabled': enable_tiered_cache,
'l1_size': max_size,
'l1_ttl': ttl,
'l2_size': max_size * 4, # L2缓存为L1的4倍
'l2_ttl': ttl * 4 # L2缓存TTL为L1的4倍
}
# L2缓存
if enable_tiered_cache:
self.l2_cache: Dict[str, CacheEntry[T]] = {}
# TTL动态调整配置
self.ttl_adjust_config = {
'enabled': False,
'min_ttl': max(1, ttl // 10), # 最小TTL为原TTL的1/10
'max_ttl': ttl * 4, # 最大TTL为原TTL的4倍
'factor': 1.5 # 调整因子
}
# 预取配置
self.prefetch_config = {
'enabled': False,
'threshold': 0.7, # 访问频率阈值
'max_prefetch_items': 10,
'data_loader': None
}
# 缓存预热状态
self.warmup_status = {
'is_warming': False,
'warmed_count': 0,
'last_warmup_time': 0
}
# WeakMap统计
self.weak_map_stats = {
'auto_collected_count': 0,
'last_cleanup_time': 0,
'memory_saved': 0
}
# 记录上次WeakMap清理时间
self.last_weak_map_cleanup_time: float = 0
self.weak_map_cleanup_min_interval: float = 30 * 1000 # 30秒
async def get(self, key: str, key_object: Optional[Any] = None) -> Optional[T]:
"""
从缓存获取值(支持分级缓存和动态调整)
首先检查L1缓存,如果未命中且启用分级缓存则检查L2缓存。
支持弱引用保护和自动失效检查。命中时会更新访问统计,
并根据配置进行TTL动态调整。
@param key: 缓存键
@param key_object: 用于弱引用的键对象(启用弱引用保护时使用)
@returns: 缓存值,如果未找到或已过期则返回None
"""
async with self.lock:
# 先检查L1缓存
entry = self.cache.get(key)
# 尝试从WeakMap获取(如果启用且提供了keyObject)
if not entry and self.enable_weak_ref_protection and key_object and hasattr(self, 'weak_cache'):
entry = self.weak_cache.get(key_object)
if entry:
# 从WeakMap恢复到主缓存
self.cache[key] = entry
# 检查WeakRef是否仍然有效
if not entry and self.enable_weak_ref_protection and hasattr(self, 'weak_ref_registry'):
weak_ref = self.weak_ref_registry.get(key)
if weak_ref:
obj = weak_ref()
if obj and hasattr(self, 'weak_cache'):
entry = self.weak_cache.get(obj)
if entry:
# 从WeakMap恢复到主缓存
self.cache[key] = entry
else:
# WeakRef失效,清理注册表
del self.weak_ref_registry[key]
self.weak_map_stats['auto_collected_count'] += 1
# 如果未在L1中找到,尝试从L2读取并提升到L1(如果启用分级缓存)
if not entry and self.tiered_cache_config['enabled'] and hasattr(self, 'l2_cache'):
l2_entry = self.l2_cache.get(key)
if l2_entry:
# 检查L2条目是否过期
if not self._is_expired(l2_entry):
# 从L2提升到L1
del self.l2_cache[key]
self.cache[key] = l2_entry
l2_entry.access_count += 1
l2_entry.last_accessed = TimeUtils.now()
self.hit_count += 1
# 保证L1大小受控
l1_limit = min(self.dynamic_max_size, self.tiered_cache_config['l1_size'])
while len(self.cache) > max(1, l1_limit):
self._evict_lru()
# 动态调整TTL(如果启用)
self._adjust_ttl(l2_entry)
return l2_entry.data
else:
# L2中的条目已过期,直接删除
del self.l2_cache[key]
# 缓存未命中
if not entry:
self.miss_count += 1
# 如果命中率低于阈值,触发智能预取
total = self.hit_count + self.miss_count
if total > 100 and self.hit_count / total < 0.5:
try:
# 检查是否有事件循环
loop = asyncio.get_running_loop()
asyncio.create_task(self._perform_prefetch())
except RuntimeError:
# 如果没有事件循环,则跳过预取
pass
return None
# 检查条目是否已过期
if self._is_expired(entry):
del self.cache[key]
if hasattr(self, 'weak_ref_registry'):
self.weak_ref_registry.pop(key, None)
self.miss_count += 1
return None
# 缓存命中:更新访问统计信息
entry.access_count += 1
entry.last_accessed = TimeUtils.now()
self.hit_count += 1
# 移动到末尾进行最近最少使用跟踪(标记为最近使用)
del self.cache[key]
self.cache[key] = entry
# 动态调整TTL(如果启用)
self._adjust_ttl(entry)
return entry.data
async def put(self, key: str, value: T, key_object: Optional[Any] = None) -> None:
"""
放入缓存(支持分级缓存和L2下沉)
将值存储在缓存中,支持L1/L2分级缓存架构。如果L1缓存已满,
则将最旧的条目下沉到L2缓存(如果启用分级缓存)。支持弱引用保护。
@param key: 缓存键
@param value: 要存储的值
@param key_object: 用于弱引用的键对象(启用弱引用保护时使用)
"""
async with self.lock:
# 更新现有条目并移动到末尾(最近使用)
if key in self.cache:
entry = self.cache[key]
entry.data = value
entry.created_at = TimeUtils.now()
entry.access_count = 0
entry.last_accessed = TimeUtils.now()
del self.cache[key]
self.cache[key] = entry
# 更新WeakMap(如果启用且提供了keyObject)
if self.enable_weak_ref_protection and key_object and hasattr(self, 'weak_cache'):
self.weak_cache[key_object] = entry
if hasattr(self, 'weak_ref_registry'):
self.weak_ref_registry[key] = ref(key_object)
return
# 如果启用分级缓存,则以L1大小作为第一层限制
l1_limit = (self.tiered_cache_config['enabled']
and self.tiered_cache_config['l1_size']
or self.dynamic_max_size)
# 如果L1已满(基于L1限制),将最旧的L1条目迁移到L2或直接淘汰
if len(self.cache) >= l1_limit:
# 取出要淘汰的键
oldest_key = next(iter(self.cache))
if oldest_key:
oldest_entry = self.cache[oldest_key]
# 如果启用了分级缓存并存在L2,则将条目下沉到L2
if (self.tiered_cache_config['enabled'] and
hasattr(self, 'l2_cache') and self.l2_cache is not None):
try:
# 放入L2
self.l2_cache[oldest_key] = oldest_entry
# 控制L2大小
l2_limit = max(1, self.tiered_cache_config['l2_size'])
while len(self.l2_cache) > l2_limit:
# 淘汰L2中最旧的
l2_oldest = next(iter(self.l2_cache))
if l2_oldest:
del self.l2_cache[l2_oldest]
except:
# 如果无法放入L2,则降级为直接删除
pass
# 从L1删除最旧项
self._evict_lru()
# 创建并添加新的缓存条目
entry = CacheEntry(
data=value,
created_at=TimeUtils.now(),
access_count=0,
last_accessed=TimeUtils.now()
)
self.cache[key] = entry
# 添加到WeakMap(如果启用且提供了keyObject)
if self.enable_weak_ref_protection and key_object and hasattr(self, 'weak_cache'):
self.weak_cache[key_object] = entry
if hasattr(self, 'weak_ref_registry'):
self.weak_ref_registry[key] = ref(key_object)
async def remove(self, key: str) -> bool:
"""移除缓存项"""
async with self.lock:
if key in self.cache:
del self.cache[key]
if hasattr(self, 'weak_ref_registry'):
self.weak_ref_registry.pop(key, None)
return True
return False
async def clear(self) -> None:
"""清空缓存"""
async with self.lock:
self.cache.clear()
if hasattr(self, 'weak_cache'):
self.weak_cache = WeakValueDictionary()
if hasattr(self, 'weak_ref_registry'):
self.weak_ref_registry.clear()
# 清理L2缓存
if hasattr(self, 'l2_cache'):
self.l2_cache.clear()
# 清理后触发智能预取分析(如果有事件循环)
try:
loop = asyncio.get_running_loop()
asyncio.create_task(self._perform_prefetch())
except RuntimeError:
# 如果没有事件循环,则跳过预取
pass
def _evict_lru(self) -> None:
"""淘汰最近最少使用的项目(支持分级缓存L2下沉)"""
if not self.cache:
return
# 映射表中的第一个条目就是最近最少使用的项目
oldest_key = next(iter(self.cache))
if not oldest_key:
return
oldest_entry = self.cache[oldest_key]
if not oldest_entry:
del self.cache[oldest_key]
return
# 如果启用分级缓存并存在L2,则将条目移到L2
if (self.tiered_cache_config['enabled'] and
hasattr(self, 'l2_cache') and self.l2_cache is not None):
try:
self.l2_cache[oldest_key] = oldest_entry
# 控制L2大小
l2_limit = max(1, self.tiered_cache_config['l2_size'])
while len(self.l2_cache) > l2_limit:
# 淘汰L2中最旧的
l2_oldest = next(iter(self.l2_cache))
if l2_oldest:
del self.l2_cache[l2_oldest]
except:
# 如果无法放入L2,则降级为直接删除
del self.cache[oldest_key]
return
# 从L1中删除条目
del self.cache[oldest_key]
def adjust_for_memory_pressure(self, pressure_level: float) -> None:
"""调整缓存大小以响应内存压力"""
scale_factor = max(0.1, 1 - pressure_level)
self.dynamic_max_size = max(1, int(self.max_size * scale_factor))
# 淘汰多余条目
while len(self.cache) > self.dynamic_max_size:
self._evict_lru()
def get_stats(self) -> Dict[str, Any]:
"""获取缓存统计信息(包含WeakMap统计和分级缓存信息)"""
total = self.hit_count + self.miss_count
base_stats = {
StringConstants.FIELD_SIZE: len(self.cache),
StringConstants.FIELD_MAX_SIZE: self.max_size,
StringConstants.FIELD_DYNAMIC_MAX_SIZE: self.dynamic_max_size,
StringConstants.FIELD_HIT_COUNT: self.hit_count,
StringConstants.FIELD_MISS_COUNT: self.miss_count,
StringConstants.FIELD_HIT_RATE: total > 0 and self.hit_count / total or 0,
StringConstants.FIELD_TTL: self.ttl
}
# 添加分级缓存统计
if self.tiered_cache_config['enabled'] and hasattr(self, 'l2_cache'):
base_stats.update({
'l2_cache_size': len(self.l2_cache),
'l2_cache_max_size': self.tiered_cache_config['l2_size'],
'l1_cache_size': self.tiered_cache_config['l1_size'],
'tiered_cache_enabled': True
})
else:
base_stats['tiered_cache_enabled'] = False
# 添加WeakMap统计(如果启用)
if self.enable_weak_ref_protection:
return {
**base_stats,
'weak_ref_enabled': True,
'weak_ref_auto_collected': self.weak_map_stats['auto_collected_count'],
'weak_ref_memory_saved': self.weak_map_stats['memory_saved'],
'weak_ref_last_cleanup': self.weak_map_stats['last_cleanup_time'],
'weak_ref_registry_size': len(getattr(self, 'weak_ref_registry', {}))
}
return {**base_stats, 'weak_ref_enabled': False}
def size(self) -> int:
"""获取缓存大小"""
return len(self.cache)
def keys(self) -> List[str]:
"""获取所有缓存键"""
return list(self.cache.keys())
def scan_entries(self) -> List[Tuple[str, CacheEntry[T]]]:
"""遍历当前缓存的条目"""
return list(self.cache.items())
def get_excess_keys(self) -> List[str]:
"""获取多余缓存键(超出动态最大大小的键)"""
keys = list(self.cache.keys())
if len(keys) <= self.dynamic_max_size:
return []
# 返回超出动态大小的键(按LRU顺序,即最前面的键)
return keys[:len(keys) - self.dynamic_max_size]
def clean_excess_keys(self) -> int:
"""清理多余缓存键"""
excess_keys = self.get_excess_keys()
for key in excess_keys:
del self.cache[key]
return len(excess_keys)
async def batch_put(self, entries: Dict[str, T]) -> None:
"""批量放入缓存"""
for key, value in entries.items():
await self.put(key, value)
async def batch_get(self, keys: List[str]) -> Dict[str, Optional[T]]:
"""批量获取缓存"""
results = {}
for key in keys:
results[key] = await self.get(key)
return results
def perform_weak_ref_cleanup(self) -> Dict[str, int]:
"""执行弱引用清理"""
if not self.enable_weak_ref_protection or not hasattr(self, 'weak_ref_registry'):
return {'cleaned_count': 0, 'memory_reclaimed': 0}
cleaned_count = 0
memory_reclaimed = 0
try:
# 清理失效的弱引用
keys_to_remove = []
for key, weak_ref in self.weak_ref_registry.items():
if weak_ref() is None:
keys_to_remove.append(key)
cleaned_count += 1
memory_reclaimed += 64 # 估算每个引用64字节
for key in keys_to_remove:
del self.weak_ref_registry[key]
self.weak_map_stats['auto_collected_count'] += cleaned_count
self.weak_map_stats['memory_saved'] += memory_reclaimed
self.weak_map_stats['last_cleanup_time'] = TimeUtils.now()
except Exception as error:
logger.warn(f"WeakRef cleanup failed: {error}")
return {'cleaned_count': cleaned_count, 'memory_reclaimed': memory_reclaimed}
def _is_expired(self, entry: CacheEntry[T]) -> bool:
"""检查条目是否已过期"""
return TimeUtils.get_duration_in_ms(entry.created_at) > self.ttl * 1000
def _adjust_ttl(self, entry: CacheEntry[T]) -> None:
"""动态调整条目的TTL"""
if not self.ttl_adjust_config['enabled']:
return
# 根据访问频率动态调整TTL
access_rate = entry.access_count / ((TimeUtils.now() - entry.created_at) / 1000)
new_ttl = self.ttl
if access_rate > 0.1: # 每10秒至少访问一次
# 增加TTL
new_ttl = min(
self.ttl_adjust_config['max_ttl'],
int(self.ttl * self.ttl_adjust_config['factor'])
)
elif access_rate < 0.01: # 每100秒不到一次访问
# 减少TTL
new_ttl = max(
self.ttl_adjust_config['min_ttl'],
int(self.ttl / self.ttl_adjust_config['factor'])
)
if new_ttl != self.ttl:
logger.debug(
f"动态调整TTL: {self.ttl}s -> {new_ttl}s (访问率: {access_rate:.4f}/s)",
metadata={
'access_count': entry.access_count,
'access_rate': access_rate,
'new_ttl': new_ttl
}
)
self.ttl = new_ttl
def configure_tiered_cache(
self,
enabled: bool,
config: Optional[Dict[str, int]] = None
) -> None:
"""
配置分级缓存
启用或禁用L1/L2分级缓存架构,支持动态配置L1和L2缓存的大小和TTL。
@param enabled: 是否启用分级缓存
@param config: 分级缓存配置,包含l1_size、l1_ttl、l2_size、l2_ttl等参数
"""
self.tiered_cache_config['enabled'] = enabled
if enabled:
if not hasattr(self, 'l2_cache'):
self.l2_cache = {}
if config:
if 'l1_size' in config:
self.tiered_cache_config['l1_size'] = max(1, config['l1_size'])
if 'l1_ttl' in config:
self.tiered_cache_config['l1_ttl'] = max(1, config['l1_ttl'])
if 'l2_size' in config:
self.tiered_cache_config['l2_size'] = max(
self.tiered_cache_config['l1_size'],
config['l2_size']
)
if 'l2_ttl' in config:
self.tiered_cache_config['l2_ttl'] = max(
self.tiered_cache_config['l1_ttl'],
config['l2_ttl']
)
elif hasattr(self, 'l2_cache'):
self.l2_cache.clear()
delattr(self, 'l2_cache')
logger.debug(
"分级缓存配置已更新",
metadata={
'enabled': enabled,
'config': self.tiered_cache_config
}
)
def configure_ttl_adjustment(
self,
enabled: bool,
config: Optional[Dict[str, float]] = None
) -> None:
"""
配置TTL动态调整
启用或禁用TTL动态调整功能,根据缓存条目的访问频率自动调整其生存时间。
访问频率高的条目TTL会增加,访问频率低的条目TTL会减少。
@param enabled: 是否启用TTL动态调整
@param config: TTL调整配置,包含min_ttl、max_ttl、factor等参数
"""
self.ttl_adjust_config['enabled'] = enabled
if enabled and config:
if 'min_ttl' in config:
self.ttl_adjust_config['min_ttl'] = max(1, config['min_ttl'])
if 'max_ttl' in config:
self.ttl_adjust_config['max_ttl'] = max(
self.ttl_adjust_config['min_ttl'],
config['max_ttl']
)
if 'factor' in config:
self.ttl_adjust_config['factor'] = max(1.1, config['factor'])
logger.debug(
"TTL动态调整配置已更新",
metadata={
'enabled': enabled,
'config': self.ttl_adjust_config
}
)
def configure_prefetch(
self,
enabled: bool,
threshold: Optional[float] = None,
max_items: Optional[int] = None,
data_loader: Optional[Callable[[str], Any]] = None
) -> None:
"""
配置预取功能
启用或禁用智能预取功能,基于访问模式分析主动预取可能需要的数据。
当缓存命中率低于阈值时会自动触发预取操作。
@param enabled: 是否启用预取功能
@param threshold: 访问频率阈值(0-1),超过此阈值的条目会被预取
@param max_items: 最大预取数量
@param data_loader: 数据加载器函数,用于预取数据
"""
self.prefetch_config['enabled'] = enabled
if threshold is not None:
self.prefetch_config['threshold'] = max(0, min(1, threshold))
if max_items is not None:
self.prefetch_config['max_prefetch_items'] = max(1, max_items)
if data_loader is not None:
self.prefetch_config['data_loader'] = data_loader
async def warmup(self, data: Dict[str, T]) -> None:
"""批量预热缓存"""
if self.warmup_status['is_warming']:
logger.warn("Cache warmup already in progress")
return
self.warmup_status['is_warming'] = True
self.warmup_status['warmed_count'] = 0
start_time = TimeUtils.now()
try:
for key, value in data.items():
if self.size() >= self.max_size:
break
await self.put(key, value)
self.warmup_status['warmed_count'] += 1
self.warmup_status['last_warmup_time'] = TimeUtils.now()
duration = TimeUtils.get_duration_in_ms(start_time)
logger.info(
f"Cache warmup completed: {self.warmup_status['warmed_count']} entries warmed up in {duration}ms",
"SmartCache",
{
'warmed_count': self.warmup_status['warmed_count'],
'duration': duration,
'cache_size': self.size()
}
)
except Exception as error:
logger.error(
"Cache warmup failed",
"SmartCache",
error,
{'error': str(error)}
)
raise error
finally:
self.warmup_status['is_warming'] = False
def get_warmup_status(self) -> Dict[str, Any]:
"""获取预热状态"""
return {**self.warmup_status}
async def _perform_prefetch(self) -> None:
"""执行智能预取"""
if not self.prefetch_config['enabled']:
return
access_patterns = {}
total_accesses = 0
# 分析访问模式
for key, entry in self.cache.items():
access_patterns[key] = entry.access_count
total_accesses += entry.access_count
# 没有足够的访问数据进行分析
if total_accesses < 10:
return
# 找出高频访问的键
high_freq_keys = [
key for key, count in access_patterns.items()
if count / total_accesses >= self.prefetch_config['threshold']
][:self.prefetch_config['max_prefetch_items']]
if high_freq_keys:
logger.debug(
f"智能预取分析:识别到 {len(high_freq_keys)} 个高频访问键",
metadata={
'high_freq_keys': high_freq_keys,
'threshold': self.prefetch_config['threshold'],
'total_accesses': total_accesses
}
)
# 启动异步预取,但不等待完成
try:
loop = asyncio.get_running_loop()
asyncio.create_task(self._prefetch_data(high_freq_keys))
except RuntimeError:
# 如果没有事件循环,则跳过预取
pass
async def _prefetch_data(self, keys: List[str]) -> None:
"""预取指定键的数据"""
if not self.prefetch_config['data_loader']:
logger.warn("未配置数据加载器,无法执行预取")
return
try:
# 记录预取开始时间
start_time = time.time()
# 只预取还没有缓存的键
keys_to_fetch = [key for key in keys if key not in self.cache]
if not keys_to_fetch:
return
# 并行加载数据
async def load_single_key(key: str):
try:
value = await self.prefetch_config['data_loader'](key)
if value is not None:
await self.put(key, value)
return True
return False
except Exception as error:
logger.warn(f"预取数据时发生错误: {key}", "SmartCache", {'error': str(error)})
return False
load_promises = [load_single_key(key) for key in keys_to_fetch]
results = await asyncio.gather(*load_promises)
success_count = sum(results)
duration = (time.time() - start_time) * 1000
logger.debug('智能预取完成', "SmartCache", {
'attempted_keys': len(keys),
'fetched_keys': len(keys_to_fetch),
'successful_fetches': success_count,
'duration': duration
})
except Exception as error:
logger.warn('智能预取过程中发生错误', "SmartCache", {'error': str(error)})
def perform_weak_map_cleanup(self) -> Dict[str, int]:
"""执行WeakMap清理"""
now = time.time()
memory_pressure = getattr(memory_pressure_manager, 'get_current_pressure', lambda: 0)()
# 根据内存压力动态调整清理频率
dynamic_interval = max(
self.weak_map_cleanup_min_interval * 0.1, # 最短间隔为最小间隔的10%