]> git.codecow.com Git - nano-pow.git/commitdiff
Add utility method to clear work cache.
authorChris Duncan <chris@zoso.dev>
Mon, 25 Aug 2025 14:50:01 +0000 (07:50 -0700)
committerChris Duncan <chris@zoso.dev>
Mon, 25 Aug 2025 14:50:01 +0000 (07:50 -0700)
src/main.ts
src/utils/cache.ts
src/utils/index.ts

index ae702fd73aa73454bed6a4e40fc23d2eb1d7e53c..4e920d9c69570cf96a3d8cfab6af84f149f211d4 100644 (file)
@@ -34,4 +34,4 @@ export class NanoPow {
 }
 
 export { NanoPow as default }
-export { stats } from '#utils'
+export { clearCache, stats } from '#utils'
index 76d647e33afcf7789fc9ca8d645f6fa0d3e310b5..8c77c0b1a05fdad91346fece5c99f1ffed3e8c0b 100644 (file)
@@ -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
+       }
 }
index 795e7d0e7824d26d3c527d2128739aa0e39987ca..0015a049e4146be38ec7b875038ae6a6b74c9fbf 100644 (file)
@@ -1,6 +1,9 @@
 //! SPDX-FileCopyrightText: 2025 Chris Duncan <chris@zoso.dev>
 //! 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'