]> git.codecow.com Git - nano-pow.git/commitdiff
Consolidate cache exports.
authorChris Duncan <chris@codecow.com>
Wed, 27 May 2026 07:35:13 +0000 (00:35 -0700)
committerChris Duncan <chris@codecow.com>
Wed, 27 May 2026 07:35:13 +0000 (00:35 -0700)
src/utils/cache.ts

index 022ee2196543ea67ebe2faf4d03073b65b0aabe0..0d3b643e6d34b0edbbf69ef91a60226dd672b00b 100644 (file)
@@ -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 }