* 设置缓存项
(key: string, data: T, ttl?: number)
| 26 | * 设置缓存项 |
| 27 | */ |
| 28 | set<T>(key: string, data: T, ttl?: number): void { |
| 29 | const now = Date.now(); |
| 30 | const item: CacheItem<T> = { |
| 31 | data, |
| 32 | timestamp: now, |
| 33 | accessCount: 0, |
| 34 | lastAccess: now, |
| 35 | ttl: ttl || this.defaultTTL, |
| 36 | }; |
| 37 | |
| 38 | // 如果缓存已满,删除最不常用的项 |
| 39 | if (this.cache.size >= this.maxSize && !this.cache.has(key)) { |
| 40 | this.evictLeastUsed(); |
| 41 | } |
| 42 | |
| 43 | this.cache.set(key, item); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * 获取缓存项 |
no test coverage detected