Skip to content

Commit b40d6b4

Browse files
committed
Update TypeScript definitions to match implementation
1 parent f235aa9 commit b40d6b4

1 file changed

Lines changed: 35 additions & 23 deletions

File tree

types/lru.d.ts

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ export interface LRUItem<T> {
2424
value: T;
2525
}
2626

27+
/**
28+
* Represents the evicted item returned by setWithEvicted().
29+
*/
30+
export interface EvictedItem<T> {
31+
/** The key of the evicted item */
32+
key: any;
33+
/** The value of the evicted item */
34+
value: T;
35+
/** The expiration timestamp of the evicted item */
36+
expiry: number;
37+
}
38+
2739
/**
2840
* High-performance Least Recently Used (LRU) cache with optional TTL support.
2941
* All core operations (get, set, delete) are O(1).
@@ -37,7 +49,7 @@ export class LRU<T = any> {
3749
* @param resetTtl Whether to reset TTL when accessing existing items via get() (default: false)
3850
*/
3951
constructor(max?: number, ttl?: number, resetTtl?: boolean);
40-
52+
4153
/** Pointer to the least recently used item (first to be evicted) */
4254
readonly first: LRUItem<T> | null;
4355
/** Hash map for O(1) key-based access to cache nodes */
@@ -52,86 +64,86 @@ export class LRU<T = any> {
5264
readonly size: number;
5365
/** Time-to-live in milliseconds (0 = no expiration) */
5466
readonly ttl: number;
55-
67+
5668
/**
5769
* Removes all items from the cache.
5870
* @returns The LRU instance for method chaining
5971
*/
6072
clear(): this;
61-
73+
6274
/**
6375
* Removes an item from the cache by key.
6476
* @param key The key of the item to delete
6577
* @returns The LRU instance for method chaining
6678
*/
6779
delete(key: any): this;
68-
80+
6981
/**
7082
* Returns an array of [key, value] pairs for the specified keys.
7183
* Order follows LRU order (least to most recently used).
7284
* @param keys Array of keys to get entries for (defaults to all keys)
7385
* @returns Array of [key, value] pairs in LRU order
7486
*/
75-
entries(keys?: any[]): [any, T][];
76-
87+
entries(keys?: any[]): [any, T | undefined][];
88+
7789
/**
7890
* Removes the least recently used item from the cache.
79-
* @param bypass Whether to force eviction even when cache is empty
91+
* @param bypass Whether to force eviction even when cache is empty (default: false)
8092
* @returns The LRU instance for method chaining
8193
*/
8294
evict(bypass?: boolean): this;
83-
95+
8496
/**
8597
* Returns the expiration timestamp for a given key.
8698
* @param key The key to check expiration for
8799
* @returns The expiration timestamp in milliseconds, or undefined if key doesn't exist
88100
*/
89101
expiresAt(key: any): number | undefined;
90-
102+
91103
/**
92104
* Retrieves a value from the cache by key. Updates the item's position to most recently used.
93105
* @param key The key to retrieve
94106
* @returns The value associated with the key, or undefined if not found or expired
95107
*/
96108
get(key: any): T | undefined;
97-
109+
98110
/**
99-
* Checks if a key exists in the cache.
111+
* Checks if a key exists in the cache (not expired).
100112
* @param key The key to check for
101-
* @returns True if the key exists, false otherwise
113+
* @returns True if the key exists and is not expired, false otherwise
102114
*/
103115
has(key: any): boolean;
104-
116+
105117
/**
106118
* Returns an array of all keys in the cache, ordered from least to most recently used.
107119
* @returns Array of keys in LRU order
108120
*/
109121
keys(): any[];
110-
122+
111123
/**
112124
* Sets a value in the cache. Updates the item's position to most recently used.
113125
* @param key The key to set
114126
* @param value The value to store
115-
* @param bypass Internal parameter for setWithEvicted method
116-
* @param resetTtl Whether to reset the TTL for this operation
127+
* @param bypass Internal parameter for setWithEvicted method (default: false)
128+
* @param resetTtl Whether to reset the TTL for this operation (default: this.resetTtl)
117129
* @returns The LRU instance for method chaining
118130
*/
119131
set(key: any, value: T, bypass?: boolean, resetTtl?: boolean): this;
120-
132+
121133
/**
122134
* Sets a value in the cache and returns any evicted item.
123135
* @param key The key to set
124136
* @param value The value to store
125-
* @param resetTtl Whether to reset the TTL for this operation
126-
* @returns The evicted item (if any) or null
137+
* @param resetTtl Whether to reset the TTL for this operation (default: this.resetTtl)
138+
* @returns The evicted item (if any) with {key, value, expiry} or null
127139
*/
128-
setWithEvicted(key: any, value: T, resetTtl?: boolean): LRUItem<T> | null;
129-
140+
setWithEvicted(key: any, value: T, resetTtl?: boolean): EvictedItem<T> | null;
141+
130142
/**
131143
* Returns an array of all values in the cache for the specified keys.
132144
* Order follows LRU order (least to most recently used).
133145
* @param keys Array of keys to get values for (defaults to all keys)
134-
* @returns Array of values corresponding to the keys in LRU order
146+
* @returns Array of values corresponding to the keys (undefined for missing/expired keys)
135147
*/
136-
values(keys?: any[]): T[];
148+
values(keys?: any[]): (T | undefined)[];
137149
}

0 commit comments

Comments
 (0)