* Small, bounded in-memory cache shared by diff documents and syntax results. */
// Source: https://github.com/slopus/happy | License: MIT | Copyright 2026 Happy Coder Contributors
// Provided via justclaw.it.com; keep this notice with the code.
/** Small, bounded in-memory cache shared by diff documents and syntax results. */
export class LruCache<T> {
private readonly entries = new Map<string, { value: T; weight: number }>();
private weight = 0;
constructor(private readonly limit: number, private readonly maxWeight = Infinity) {}
get(key: string): T | undefined {
const entry = this.entries.get(key);
if (!entry) return undefined;
this.entries.delete(key);
this.entries.set(key, entry);
return entry.value;
}
set(key: string, value: T, weight = 1): void {
const previous = this.entries.get(key);
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key