@unikvs/lru-cache
How to use LruCache in @unikvs/lru-cache, an LRU storage that keeps arbitrary values with limits.
Overview
@unikvs/lru-cache is an LRU storage that keeps arbitrary values in memory with limits. It evicts least-recently-used entries first, so it works well as a cache.
nameisLruCache.- Requires
openandclose.closediscards all stored values. - Set limits with
maxEntriesandmaxBytes. Both are optional. - Use
sizefor the entry count andbytesfor the byte count (whenmaxBytesis set).
Install with:
npm install @unikvs/lru-cachepnpm add @unikvs/lru-cacheyarn add @unikvs/lru-cachebun add @unikvs/lru-cachenub add @unikvs/lru-cacheaube add @unikvs/lru-cacheUsage
LruCache implements IStorage. Pass it to UniKvs with persist().
import { LruCache } from "@unikvs/lru-cache";
Constructor arguments:
clone?(<T>(value: T) => T) | undefined
Function used to clone data. Uses structuredClone when omitted.
(<T>(value: T) => T) | undefinedstructuredClonemaxEntries?number | undefined
Maximum number of entries. Specify a non-negative integer.
number | undefinedmaxBytes?number | undefined
Maximum total bytes. Specify a non-negative finite number.
number | undefinedsizeOf?((value: any, key: string) => number) | undefined
Function deciding per-key bytes. Defaults to byteLength for Uint8Array and 1 otherwise.
((value: any, key: string) => number) | undefinedThe following example stores values with an entry-count limit.
import { LruCache } from "@unikvs/lru-cache";
import * as unikvs from "unikvs";
const kvs = new unikvs.Builder<{
greeting: unikvs.PlainValue<string>;
}>()
.persist(new LruCache({ maxEntries: 100 }))
.build();
await kvs.open();
await kvs.set("greeting", "hello");
const value = await kvs.get("greeting");
console.log(value); // "hello"
await kvs.close();
Data types
| Operation | Supported data type |
|---|---|
write/read |
Arbitrary values (strings, objects, etc.) |
getWritable/getReadable chunks |
Uint8Array<ArrayBuffer> |
- Stores arbitrary values. Handles values cloneable with
structuredClone, such as strings and objects. - Clones read/write values with
structuredCloneby default. Change it with the constructorcloneoption. - When
maxEntriesis exceeded, least-recently-used keys are evicted first. WithmaxEntries: 0, nothing is stored. - When
maxBytesis set, older keys are evicted so total bytes stay within the limit. - Stream chunks support only
Uint8Array<ArrayBuffer>. - Streams from
getWritableconcatenate written chunks and save them onclose. - Streams from
getReadableemit the stored value as a single chunk. ThrowsInvalidChunkTypeErrorwhen the stored value is not aUint8Array.
Errors
This package throws the following errors.
KeyNotFoundError
Meaning
Thrown when the specified key does not exist. A shared error from @unikvs/core.
How to fix
Check with exists before reading.
Example
The following example catches KeyNotFoundError.
import { KeyNotFoundError } from "@unikvs/lru-cache";
try {
await kvs.get("missing-key");
} catch (ex) {
if (ex instanceof KeyNotFoundError) {
console.log("Key not found");
} else {
throw ex;
}
}
InvalidChunkTypeError
Meaning
Thrown when a non-Uint8Array<ArrayBuffer> chunk is written to a stream, or when getReadable is called on a value that is not a Uint8Array.
How to fix
Write Uint8Array chunks to streams. Store byte sequences for values used with getReadable.
InvalidMaxEntriesError
actualunknown
The given maxEntries value.
unknownMeaning
Thrown when the constructor maxEntries is not a non-negative integer.
How to fix
Specify a non-negative integer for maxEntries.
InvalidMaxBytesError
actualunknown
The given maxBytes value.
unknownMeaning
Thrown when the constructor maxBytes is not a non-negative finite number.
How to fix
Specify a non-negative finite number for maxBytes.
InvalidSizeOfResultError
Meaning
Thrown when sizeOf returns a value that is not a non-negative finite number.
How to fix
Fix sizeOf so it returns a non-negative finite number.