* 清理缓存
()
| 689 | * 清理缓存 |
| 690 | */ |
| 691 | private cleanupCache(): void { |
| 692 | // 移除过期的缓存条目 |
| 693 | const now = Date.now(); |
| 694 | for (const [key, entry] of this._queryCache.entries()) { |
| 695 | if (now - entry.timestamp > this._cacheTimeout) { |
| 696 | this._queryCache.delete(key); |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | // 如果还是太满,移除最少使用的条目 |
| 701 | if (this._queryCache.size >= this._cacheMaxSize) { |
| 702 | let minHitCount = Infinity; |
| 703 | let oldestKey = ''; |
| 704 | let oldestTimestamp = Infinity; |
| 705 | |
| 706 | // 单次遍历找到最少使用或最旧的条目 |
| 707 | for (const [key, entry] of this._queryCache.entries()) { |
| 708 | if ( |
| 709 | entry.hitCount < minHitCount || |
| 710 | (entry.hitCount === minHitCount && entry.timestamp < oldestTimestamp) |
| 711 | ) { |
| 712 | minHitCount = entry.hitCount; |
| 713 | oldestKey = key; |
| 714 | oldestTimestamp = entry.timestamp; |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | if (oldestKey) { |
| 719 | this._queryCache.delete(oldestKey); |
| 720 | } |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | /** |
| 725 | * 清除所有查询缓存 |
no test coverage detected