Registry / storage / lru-cache-idb

lru-cache-idb

JSON →
library0.5.2jsnpmunverified

lru-cache-idb v0.5.2 is an LRU cache for web applications backed by IndexedDB. It batches writes to reduce I/O (default write interval 15s), batches eviction (default 1 min), and optionally keeps a subset of items in memory for faster reads. Designed for multi-tab safety and cross-session persistence, it implements a modern LRU eviction policy. Unlike most LRU caches (e.g., lru-cache for Node), lru-cache-idb is browser-first, uses no third-party dependencies, and is fully typed in TypeScript as an ESM-only module. Developed with low-frequency updates, it is stable for use cases requiring persistent client-side cache.

npm install lru-cache-idb
INSTALL
IMPORT
SIG · LRU-CACHE-IDB
L
lru-cache-idb
storagejavascriptv0.5.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

createCacheIdb
import { createCacheIdb } from 'lru-cache-idb'
import createCacheIdb from 'lru-cache-idb'
createCacheIdb is a named export, not default. ESM-only.
LruIdbCache
import type { LruIdbCache } from 'lru-cache-idb'
import { LruIdbCache } from 'lru-cache-idb'
LruIdbCache is a TypeScript interface used for type annotations only. Import it with `import type` for clarity.
CacheConfig
import type { CacheConfig } from 'lru-cache-idb'
import { CacheConfig } from 'lru-cache-idb'
CacheConfig is a TypeScript interface for configuration objects. Use `import type`.

Creates an LRU cache with IndexedDB persistence, sets and gets an object item, logs it, deletes it, and clears the cache.

import { createCacheIdb } from 'lru-cache-idb'; const cache = createCacheIdb({ maxItems: 1000, memoryConfig: { maxItemsInMemory: 100 } }); await cache.set('key1', { value: 'hello' }); const item = await cache.get('key1'); console.log(item); // { value: 'hello' } await cache.delete('key1'); await cache.clear();
Debug
Known issues
gotchaThe default write interval (15s) means that writes to IndexedDB are batched and may be lost if the browser tab is closed before the interval elapses.
fix
Configure a shorter writeIntervalMs in the options, or call .flush() explicitly before navigating away.
affects: >=0.1.0
gotchaThe default eviction interval (1 minute) means the cache may temporarily exceed maxItems until the next batch eviction runs.
fix
Set a shorter evictionIntervalMs or call .evict() manually if memory constraints are strict.
affects: >=0.1.0
breakingVersion 0.5.0 changed the export from a default export to a named export, breaking existing require() or default import patterns.
fix
Update imports: use `import { createCacheIdb } from 'lru-cache-idb'` instead of `import createCacheIdb from 'lru-cache-idb'`.
affects: >=0.5.0
gotchaMultiple caches on the same page must have different database names to avoid data clashes.
fix
Specify a unique `database` option per cache instance.
affects: >=0.1.0
Errors
Common errors & fixes
TypeError: lru_cache_idb_1.default is not a function
Using default import instead of named import in ESM environment.
fix
Replace `import createCacheIdb from 'lru-cache-idb'` with `import { createCacheIdb } from 'lru-cache-idb'`.
Uncaught TypeError: Cannot read properties of undefined (reading 'set')
Forgetting to await the promise returned by createCacheIdb or using cache before it is fully initialized.
fix
Ensure `const cache = await createCacheIdb(...)` and wait for initialization before calling cache methods.
Uncaught DOMException: Failed to execute 'transaction' on 'IDBDatabase': The database connection is closing.
Multiple tabs with the same database name causing conflicts, or rapid clear()/close() calls.
fix
Use unique database names per page instance, and avoid concurrent destructive operations.
Upgrade
Version history
0.5.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
22 hits · last 30 days
node
22
Resources