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