chore: cleanup
This commit is contained in:
+15
-3
@@ -1,3 +1,4 @@
|
||||
import { readdirSync } from "node:fs";
|
||||
import { mkdir, readdir } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { file, hash, write } from "bun";
|
||||
@@ -11,6 +12,7 @@ export class AssetManager {
|
||||
private cache = new Map<string, ArrayBuffer>();
|
||||
private deduper = new Deduplicator<ArrayBuffer>();
|
||||
private MAX = 300;
|
||||
private approximateSize = 0;
|
||||
|
||||
async getMissingAssets() {
|
||||
const avatars = await getEnkaAvatarStore();
|
||||
@@ -112,16 +114,26 @@ export class AssetManager {
|
||||
|
||||
// a simple LRU
|
||||
private touch(key: string, value: ArrayBuffer) {
|
||||
if (!this.cache.has(key)) this.approximateSize += value.byteLength;
|
||||
// move to newest
|
||||
this.cache.delete(key);
|
||||
this.cache.set(key, value);
|
||||
|
||||
// purge oldest
|
||||
if (this.cache.size > this.MAX) {
|
||||
const oldest = this.cache.keys().next().value;
|
||||
this.cache.delete(oldest!);
|
||||
// purge oldest
|
||||
const oldest = this.cache.keys().next().value!;
|
||||
this.approximateSize -= this.cache.get(oldest)?.byteLength ?? 0;
|
||||
this.cache.delete(oldest);
|
||||
}
|
||||
}
|
||||
|
||||
get health() {
|
||||
return {
|
||||
lru: this.cache.size,
|
||||
lruSize: `${(this.approximateSize / 1000).toFixed(2)}Kb`,
|
||||
disk: readdirSync("store").length,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export async function getEnkaStores() {
|
||||
|
||||
Reference in New Issue
Block a user