-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
58 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import type { AsyncStore } from 'zarr/types/storage/types'; | ||
import QuickLRU from 'quick-lru'; | ||
|
||
export class LRUCacheStore<S extends AsyncStore<ArrayBuffer>> { | ||
private cache: QuickLRU<string, ArrayBuffer>; | ||
|
||
constructor(public store: S, public maxSize: number = 100) { | ||
this.cache = new QuickLRU({ maxSize }); | ||
} | ||
|
||
async getItem(...args: Parameters<S['getItem']>) { | ||
const [key, opts] = args; | ||
if (this.cache.has(key)) { | ||
return this.cache.get(key)!; | ||
} | ||
const value = await this.store.getItem(key, opts); | ||
this.cache.set(key, value); | ||
return value; | ||
} | ||
|
||
async containsItem(key: string) { | ||
return this.cache.has(key) || this.store.containsItem(key); | ||
} | ||
|
||
async keys() { | ||
return []; | ||
} | ||
|
||
deleteItem(key: string): never { | ||
throw new Error('deleteItem not implemented'); | ||
} | ||
|
||
setItem(key: string, value: ArrayBuffer): never { | ||
throw new Error('setItem not implemented'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters