From 3335c698f75bb0269a5d7db3b21ee327356b2bcf Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Mon, 25 Aug 2025 07:50:01 -0700 Subject: [PATCH] Add utility method to clear work cache. --- src/main.ts | 2 +- src/utils/cache.ts | 25 +++++++++++++++++-------- src/utils/index.ts | 3 +++ 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/main.ts b/src/main.ts index ae702fd..4e920d9 100644 --- a/src/main.ts +++ b/src/main.ts @@ -34,4 +34,4 @@ export class NanoPow { } export { NanoPow as default } -export { stats } from '#utils' +export { clearCache, stats } from '#utils' diff --git a/src/utils/cache.ts b/src/utils/cache.ts index 76d647e..8c77c0b 100644 --- a/src/utils/cache.ts +++ b/src/utils/cache.ts @@ -5,14 +5,9 @@ import { WorkGenerateResponse } from "#types" import { bigintFrom } from "#utils" export class Cache { - static #storage: { [key: string]: string } = {} - static #getItem (key: string): string | null { - if (globalThis?.localStorage) return globalThis.localStorage.getItem(key) - return this.#storage[key] - } - static #setItem (key: string, item: string): void { - if (globalThis?.localStorage) return globalThis.localStorage.setItem(key, item) - this.#storage[key] = item + + static clear (): void { + this.#removeItem('NanoPowCache') } static search (hash: unknown, difficulty: bigint): WorkGenerateResponse | null { @@ -36,4 +31,18 @@ export class Cache { this.#setItem('NanoPowCache', JSON.stringify(cache)) return result } + + static #storage: { [key: string]: string } = {} + static #getItem (key: string): string | null { + if (globalThis?.localStorage) return globalThis.localStorage.getItem(key) + return this.#storage[key] + } + static #removeItem (key: string): void { + if (globalThis?.localStorage) return globalThis.localStorage.removeItem(key) + this.#storage = {} + } + static #setItem (key: string, item: string): void { + if (globalThis?.localStorage) return globalThis.localStorage.setItem(key, item) + this.#storage[key] = item + } } diff --git a/src/utils/index.ts b/src/utils/index.ts index 795e7d0..0015a04 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,6 +1,9 @@ //! SPDX-FileCopyrightText: 2025 Chris Duncan //! SPDX-License-Identifier: GPL-3.0-or-later +import { Cache } from '.' +export const clearCache = Cache.clear + export * from './api-support' export * from './bigint' export * from './cache' -- 2.47.3