&& 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 }