From: Chris Duncan Date: Wed, 27 May 2026 07:35:13 +0000 (-0700) Subject: Consolidate cache exports. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=a6393241aa7b69b270bc74b195a09d3d54e677fd;p=nano-pow.git Consolidate cache exports. --- diff --git a/src/utils/cache.ts b/src/utils/cache.ts index 022ee21..0d3b643 100644 --- a/src/utils/cache.ts +++ b/src/utils/cache.ts @@ -24,36 +24,33 @@ function eq (a: unknown, b: bigint): boolean { && BigInt(`0x${a}`) === b } -function clear (): void { - storage.removeItem(STORAGE_KEY) -} - -function get (hash: bigint, difficulty: bigint): WorkGenerateResponse | null { - const item = storage.getItem(STORAGE_KEY) - if (item == null) return null - const cache: WorkGenerateResponse[] = JSON.parse(item) - const match = cache.find(c => eq(c.hash, hash) && eq(c.difficulty, difficulty)) - return match ?? null -} - -function remove (hash: bigint): void { - const item = storage.getItem(STORAGE_KEY) - if (item == null) return - const cache = JSON.parse(item) as WorkGenerateResponse[] - for (let i = 0; i < cache.length; i++) { - if (eq(cache[i].hash, hash)) { - cache.splice(i, 1) +export const Cache = { + clear (): void { + storage.removeItem(STORAGE_KEY) + }, + delete (hash: bigint): void { + const item = storage.getItem(STORAGE_KEY) + if (item == null) return + const cache = JSON.parse(item) as WorkGenerateResponse[] + for (let i = 0; i < cache.length; i++) { + if (eq(cache[i].hash, hash)) { + cache.splice(i, 1) + } } + storage.setItem(STORAGE_KEY, JSON.stringify(cache)) + }, + search (hash: bigint, difficulty: bigint): WorkGenerateResponse | null { + const item = storage.getItem(STORAGE_KEY) + if (item == null) return null + const cache: WorkGenerateResponse[] = JSON.parse(item) + const match = cache.find(c => eq(c.hash, hash) && eq(c.difficulty, difficulty)) + return match ?? null + }, + store (result: WorkGenerateResponse): WorkGenerateResponse { + const item = storage.getItem(STORAGE_KEY) ?? '[]' + const cache: WorkGenerateResponse[] = JSON.parse(item) + if (cache.push(result) > 1000) cache.shift() + storage.setItem(STORAGE_KEY, JSON.stringify(cache)) + return result } - storage.setItem(STORAGE_KEY, JSON.stringify(cache)) } - -function set (result: WorkGenerateResponse): WorkGenerateResponse { - const item = storage.getItem(STORAGE_KEY) ?? '[]' - const cache: WorkGenerateResponse[] = JSON.parse(item) - if (cache.push(result) > 1000) cache.shift() - storage.setItem(STORAGE_KEY, JSON.stringify(cache)) - return result -} - -export const Cache = { clear, delete: remove, search: get, store: set }