mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2026-01-05 18:39:06 +08:00
* feat: pnpm new * Refactor build and release workflows, update dependencies Switch build scripts and workflows from npm to pnpm, update build and artifact paths, and simplify release workflow by removing version detection and changelog steps. Add new dependencies (silk-wasm, express, ws, node-pty-prebuilt-multiarch), update exports in package.json files, and add vite config for napcat-framework. Also, rename manifest.json for framework package and fix static asset copying in shell build config.
44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
export class LRUCache<K, V> {
|
|
private capacity: number;
|
|
public cache: Map<K, V>;
|
|
|
|
constructor (capacity: number) {
|
|
this.capacity = capacity;
|
|
this.cache = new Map<K, V>();
|
|
}
|
|
|
|
public get (key: K): V | undefined {
|
|
const value = this.cache.get(key);
|
|
if (value !== undefined) {
|
|
// Move the accessed key to the end to mark it as most recently used
|
|
this.cache.delete(key);
|
|
this.cache.set(key, value);
|
|
}
|
|
return value;
|
|
}
|
|
|
|
public put (key: K, value: V): void {
|
|
if (this.cache.has(key)) {
|
|
// If the key already exists, move it to the end to mark it as most recently used
|
|
this.cache.delete(key);
|
|
} else if (this.cache.size >= this.capacity) {
|
|
// If the cache is full, remove the least recently used key (the first one in the map)
|
|
const firstKey = this.cache.keys().next().value;
|
|
if (firstKey !== undefined) {
|
|
this.cache.delete(firstKey);
|
|
}
|
|
}
|
|
this.cache.set(key, value);
|
|
}
|
|
|
|
public resetCapacity (newCapacity: number): void {
|
|
this.capacity = newCapacity;
|
|
while (this.cache.size > this.capacity) {
|
|
const firstKey = this.cache.keys().next().value;
|
|
if (firstKey !== undefined) {
|
|
this.cache.delete(firstKey);
|
|
}
|
|
}
|
|
}
|
|
}
|