Path: blob/main/extensions/copilot/src/extension/chatSessions/common/ttlCache.ts
13399 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45/**6* A simple TTL (time-to-live) cache that stores key-value pairs with expiration.7* Entries are evicted lazily on access when their TTL has elapsed.8*/9export class TtlCache<V> {10private readonly _entries = new Map<string, { value: V; timestamp: number; ttlMs?: number }>();1112/**13* @param _ttlMs The time-to-live in milliseconds for cache entries.14*/15constructor(private readonly _ttlMs: number) { }1617/**18* Returns the cached value if it exists and has not expired, otherwise `undefined`.19*/20get(key: string): V | undefined {21const entry = this._entries.get(key);22if (!entry) {23return undefined;24}25const ttl = entry.ttlMs ?? this._ttlMs;26if (Date.now() - entry.timestamp >= ttl) {27this._entries.delete(key);28return undefined;29}30return entry.value;31}3233/**34* Stores a value in the cache with the current timestamp.35* @param ttlMs Optional per-entry TTL override in milliseconds. If not provided, the cache-wide TTL is used.36*/37set(key: string, value: V, ttlMs?: number): void {38this._entries.set(key, { value, timestamp: Date.now(), ttlMs });39}4041/**42* Removes a single entry from the cache.43*/44delete(key: string): void {45this._entries.delete(key);46}4748/**49* Removes all entries from the cache.50*/51clear(): void {52this._entries.clear();53}5455/**56* Returns `true` if the cache has a non-expired entry for the given key.57*/58has(key: string): boolean {59return this.get(key) !== undefined;60}61}6263/**64* A single-slot TTL cache that stores one value with an associated key and expiration.65* Useful for caching a computed result (e.g. the full options object for a given repo context).66*/67export class SingleSlotTtlCache<V> {68private _entry: { value: V; timestamp: number; key: string } | undefined;6970/**71* @param _ttlMs The time-to-live in milliseconds for the cached entry.72*/73constructor(private readonly _ttlMs: number) { }7475/**76* Returns the cached value if the key matches and the TTL has not expired, otherwise `undefined`.77*/78get(key: string): V | undefined {79if (!this._entry || this._entry.key !== key) {80return undefined;81}82if (Date.now() - this._entry.timestamp >= this._ttlMs) {83this._entry = undefined;84return undefined;85}86return this._entry.value;87}8889/**90* Stores a value in the cache, replacing any previous entry.91*/92set(key: string, value: V): void {93this._entry = { value, timestamp: Date.now(), key };94}9596/**97* Removes the cached entry.98*/99clear(): void {100this._entry = undefined;101}102103/**104* Returns `true` if the cache has a non-expired entry for the given key.105*/106has(key: string): boolean {107return this.get(key) !== undefined;108}109}110111112