Skip to content

Commit ab6843b

Browse files
committed
refactor: simplify set() and setWithEvicted() API by removing resetTtl parameter
1 parent 5d9ed36 commit ab6843b

4 files changed

Lines changed: 28 additions & 40 deletions

File tree

coverage.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
ℹ file | line % | branch % | funcs % | uncovered lines
44
ℹ ----------------------------------------------------------
55
ℹ src | | | |
6-
ℹ lru.js | 100.00 | 94.67 | 100.00 |
6+
ℹ lru.js | 99.60 | 93.15 | 100.00 | 342-343
77
ℹ ----------------------------------------------------------
8-
ℹ all files | 100.00 | 94.67 | 100.00 |
8+
ℹ all files | 99.60 | 93.15 | 100.00 |
99
ℹ ----------------------------------------------------------
1010
ℹ end of coverage report

docs/API.md

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ console.log(cache.keys()); // ['b', 'c', 'a']
288288

289289
---
290290

291-
### `set(key, value, bypass?, resetTtl?)`
291+
### `set(key, value)`
292292

293293
Stores value and moves to most recently used.
294294

@@ -299,18 +299,16 @@ console.log(cache.keys()); // ['a', 'b', 'c']
299299

300300
**Parameters:**
301301

302-
| Name | Type | Default | Description |
303-
| ---------- | --------- | --------------- | ---------------------------------- |
304-
| `key` | `string` | - | Item key |
305-
| `value` | `*` | - | Item value |
306-
| `bypass` | `boolean` | `false` | Internal flag for `setWithEvicted` |
307-
| `resetTtl` | `boolean` | `this.resetTtl` | Reset TTL for this operation |
302+
| Name | Type | Description |
303+
| ----- | ----- | ----------- |
304+
| `key` | `string` | Item key |
305+
| `value` | `*` | Item value |
308306

309307
**Returns:** `LRU` - this instance (for chaining)
310308

311309
---
312310

313-
### `setWithEvicted(key, value, resetTtl?)`
311+
### `setWithEvicted(key, value)`
314312

315313
Stores value and returns evicted item if cache was full.
316314

@@ -325,11 +323,10 @@ console.log(cache.keys()); // ['b', 'c']
325323

326324
**Parameters:**
327325

328-
| Name | Type | Default | Description |
329-
| ---------- | --------- | --------------- | ---------------------------- |
330-
| `key` | `string` | - | Item key |
331-
| `value` | `*` | - | Item value |
332-
| `resetTtl` | `boolean` | `this.resetTtl` | Reset TTL for this operation |
326+
| Name | Type | Description |
327+
| ----- | ----- | ----------- |
328+
| `key` | `string` | Item key |
329+
| `value` | `*` | Item value |
333330

334331
**Returns:** `{ key, value, expiry } | null` - Evicted item or null
335332

docs/TECHNICAL_DOCUMENTATION.md

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -159,17 +159,17 @@ The LRU cache maintains a doubly-linked list $L$ and a hash table $H$ for O(1) o
159159

160160
**Note:** The mathematical notation uses `create(k, v)` to represent the item creation logic that is inline in the actual implementation.
161161

162-
#### Set Operation: $set(k, v, bypass = false, resetTtl = resetTtl) \rightarrow \text{LRU}$
162+
#### Set Operation: $set(k, v) \rightarrow \text{LRU}$
163163

164164
$$
165165
\begin{align}
166-
set(k, v, bypass, resetTtl) &= \begin{cases}
167-
update(k, v, bypass, resetTtl) & \text{if } k \in H \\
166+
set(k, v) &= \begin{cases}
167+
update(k, v) & \text{if } k \in H \\
168168
insert(k, v) & \text{if } k \notin H
169169
\end{cases} \\
170-
update(k, v, bypass, resetTtl) &= H[k].value \leftarrow v \land moveToEnd(H[k]) \\
170+
update(k, v) &= H[k].value \leftarrow v \land moveToEnd(H[k]) \\
171171
& \quad \land \begin{cases}
172-
H[k].expiry \leftarrow t_{now} + ttl & \text{if } bypass = false \land resetTtl = true \land ttl > 0 \\
172+
H[k].expiry \leftarrow t_{now} + ttl & \text{if } resetTtl = true \land ttl > 0 \\
173173
\text{no-op} & \text{otherwise}
174174
\end{cases} \\
175175
insert(k, v) &= \begin{cases}
@@ -187,16 +187,16 @@ $$
187187

188188
**Time Complexity:** $O(1)$ amortized
189189

190-
#### Set With Evicted Operation: $setWithEvicted(k, v, resetTtl = resetTtl) \rightarrow \{key: K, value: V, expiry: \mathbb{N}_0\} \cup \{\bot\}$
190+
#### Set With Evicted Operation: $setWithEvicted(k, v) \rightarrow \{key: K, value: V, expiry: \mathbb{N}_0\} \cup \{\bot\}$
191191

192192
$$
193193
\begin{align}
194-
setWithEvicted(k, v, resetTtl) &= \begin{cases}
195-
update(k, v, resetTtl) \land \bot & \text{if } k \in H \\
194+
setWithEvicted(k, v) &= \begin{cases}
195+
update(k, v) \land \bot & \text{if } k \in H \\
196196
evicted \land create(k, v) & \text{if } k \notin H \land max > 0 \land size = max \\
197197
\bot \land create(k, v) & \text{if } k \notin H \land (max = 0 \lor size < max)
198198
\end{cases} \\
199-
update(k, v, resetTtl) &= H[k].value \leftarrow v \land moveToEnd(H[k]) \\
199+
update(k, v) &= H[k].value \leftarrow v \land moveToEnd(H[k]) \\
200200
& \quad \land \begin{cases}
201201
H[k].expiry \leftarrow t_{now} + ttl & \text{if } resetTtl = true \land ttl > 0 \\
202202
\text{no-op} & \text{otherwise}
@@ -208,8 +208,6 @@ H[k].expiry \leftarrow t_{now} + ttl & \text{if } resetTtl = true \land ttl > 0
208208
\end{align}
209209
$$
210210

211-
**Note:** Unlike `set()`, `setWithEvicted()` does not use a `bypass` parameter, so TTL is reset when `resetTtl = true`.
212-
213211
**Time Complexity:** $O(1)$ amortized
214212

215213
#### Get Operation: $get(k) \rightarrow V \cup \{\bot\}$
@@ -289,9 +287,8 @@ $$
289287

290288
**TTL Reset Behavior:**
291289

292-
- TTL is only reset during `set()` operations when `resetTtl = true` and `bypass = false`
290+
- TTL is reset during `set()` and `setWithEvicted()` operations when `resetTtl = true`
293291
- `get()` operations never reset TTL, regardless of the `resetTtl` setting
294-
- `setWithEvicted()` operations reset TTL when `resetTtl = true` (does not use bypass parameter)
295292

296293
### Space Complexity
297294

@@ -341,12 +338,8 @@ export class LRU<T> {
341338
get(key: any): T | undefined;
342339
has(key: any): boolean;
343340
keys(): any[];
344-
set(key: any, value: T, bypass?: boolean, resetTtl?: boolean): this;
345-
setWithEvicted(
346-
key: any,
347-
value: T,
348-
resetTtl?: boolean,
349-
): { key: any; value: T; expiry: number } | null;
341+
set(key: any, value: T): this;
342+
setWithEvicted(key: any, value: T): { key: any; value: T; expiry: number } | null;
350343
values(keys?: any[]): T[];
351344
}
352345

src/lru.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,6 @@ export class LRU {
323323
* @memberof LRU
324324
* @param {string} key - The key to set.
325325
* @param {*} value - The value to store.
326-
* @param {boolean} [resetTtl=this.resetTtl] - Whether to reset the TTL for this operation.
327326
* @returns {Object|null} The evicted item (if any) with shape {key, value, expiry, prev, next}, or null.
328327
* @example
329328
* const cache = new LRU(2);
@@ -333,13 +332,13 @@ export class LRU {
333332
* @see {@link LRU#evict}
334333
* @since 11.3.0
335334
*/
336-
setWithEvicted(key, value, resetTtl = this.resetTtl) {
335+
setWithEvicted(key, value) {
337336
let evicted = null;
338337
let item = this.items[key];
339338

340339
if (item !== undefined) {
341340
item.value = value;
342-
if (resetTtl) {
341+
if (this.resetTtl) {
343342
item.expiry = this.ttl > 0 ? Date.now() + this.ttl : this.ttl;
344343
}
345344
this.moveToEnd(item);
@@ -380,7 +379,6 @@ export class LRU {
380379
* @memberof LRU
381380
* @param {string} key - The key to set.
382381
* @param {*} value - The value to store.
383-
* @param {boolean} [resetTtl=this.resetTtl] - Whether to reset the TTL for this operation.
384382
* @returns {LRU} The LRU instance for method chaining.
385383
* @example
386384
* cache.set('key1', 'value1')
@@ -390,13 +388,13 @@ export class LRU {
390388
* @see {@link LRU#setWithEvicted}
391389
* @since 1.0.0
392390
*/
393-
set(key, value, resetTtl = this.resetTtl) {
391+
set(key, value) {
394392
let item = this.items[key];
395393

396394
if (item !== undefined) {
397395
item.value = value;
398396

399-
if (resetTtl) {
397+
if (this.resetTtl) {
400398
item.expiry = this.ttl > 0 ? Date.now() + this.ttl : this.ttl;
401399
}
402400

0 commit comments

Comments
 (0)