Replace lru-cache dep with light util

This commit is contained in:
Kalle
2026-06-12 21:46:59 +03:00
parent 6bfa2ce6e2
commit ffa12b05cb
6 changed files with 117 additions and 6 deletions

67
app/modules/cache/index.test.ts vendored Normal file
View File

@@ -0,0 +1,67 @@
import { describe, expect, it } from "vitest";
import { LRUCache } from "./index";
describe("LRUCache", () => {
it("stores and retrieves values", () => {
const cache = new LRUCache<string, number>({ max: 2 });
cache.set("a", 1);
expect(cache.get("a")).toBe(1);
expect(cache.has("a")).toBe(true);
});
it("returns undefined for missing keys", () => {
const cache = new LRUCache<string, number>({ max: 2 });
expect(cache.get("missing")).toBeUndefined();
expect(cache.has("missing")).toBe(false);
});
it("evicts the least recently used entry once over capacity", () => {
const cache = new LRUCache<string, number>({ max: 2 });
cache.set("a", 1);
cache.set("b", 2);
cache.set("c", 3);
expect(cache.has("a")).toBe(false);
expect(cache.get("b")).toBe(2);
expect(cache.get("c")).toBe(3);
});
it("counts reads as recent use", () => {
const cache = new LRUCache<string, number>({ max: 2 });
cache.set("a", 1);
cache.set("b", 2);
cache.get("a");
cache.set("c", 3);
expect(cache.has("a")).toBe(true);
expect(cache.has("b")).toBe(false);
});
it("updates value without growing past capacity on re-set", () => {
const cache = new LRUCache<string, number>({ max: 2 });
cache.set("a", 1);
cache.set("a", 2);
expect(cache.get("a")).toBe(2);
});
it("deletes a single entry", () => {
const cache = new LRUCache<string, number>({ max: 2 });
cache.set("a", 1);
cache.delete("a");
expect(cache.has("a")).toBe(false);
});
it("clears all entries", () => {
const cache = new LRUCache<string, number>({ max: 2 });
cache.set("a", 1);
cache.set("b", 2);
cache.clear();
expect(cache.has("a")).toBe(false);
expect(cache.has("b")).toBe(false);
});
});

48
app/modules/cache/index.ts vendored Normal file
View File

@@ -0,0 +1,48 @@
/**
* A lightweight least-recently-used cache backed by a `Map`. Once `max` entries
* are stored, inserting a new key evicts the least recently used one. Reading a
* key via `get` marks it as most recently used.
*/
export class LRUCache<K, V> {
private readonly max: number;
private readonly map = new Map<K, V>();
constructor({ max }: { max: number }) {
this.max = max;
}
get(key: K): V | undefined {
if (!this.map.has(key)) return undefined;
const value = this.map.get(key) as V;
this.map.delete(key);
this.map.set(key, value);
return value;
}
has(key: K): boolean {
return this.map.has(key);
}
set(key: K, value: V): void {
if (this.map.has(key)) {
this.map.delete(key);
}
this.map.set(key, value);
if (this.map.size > this.max) {
const oldest = this.map.keys().next().value as K;
this.map.delete(oldest);
}
}
delete(key: K): void {
this.map.delete(key);
}
clear(): void {
this.map.clear();
}
}