@sunacchi/cache-kit-indexeddb
v1.0.2
Published
IndexedDB cache adapter for @sunacchi/cache-kit — browsers
Maintainers
Readme
@sunacchi/cache-kit-indexeddb
IndexedDB cache adapter for @sunacchi/cache-kit. Provides persistent, high-capacity caching in the browser using the native IndexedDB API.
Part of the Cache Kit monorepo.
Install
npm install @sunacchi/cache-kit @sunacchi/cache-kit-indexeddbUsage
import { Cache } from '@sunacchi/cache-kit';
import { IndexedDbAdapter } from '@sunacchi/cache-kit-indexeddb';
const cache = new Cache<string>({
adapter: new IndexedDbAdapter(),
defaultTtl: 600_000, // 10 minutes
});
await cache.set('user:profile', userData);
const profile = await cache.get('user:profile');Options
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| dbName | string | 'cache-kit' | Name of the IndexedDB database. |
| storeName | string | 'cache' | Name of the object store within the database. |
const adapter = new IndexedDbAdapter({
dbName: 'my-app',
storeName: 'api-cache',
});How It Works
- Uses the raw IndexedDB API (no wrapper libraries)
- Database and object store are created automatically on first use
- Each key-value pair is stored as a record in the object store
- All operations are fully async and non-blocking
- Call
close()when done to release the database connection
Why IndexedDB?
| Feature | localStorage | IndexedDB | |---------|-------------|-----------| | Capacity | ~5-10 MB | Hundreds of MB+ | | API | Synchronous (blocks UI) | Asynchronous | | Data types | Strings only | Structured data | | Cross-tab | Yes | Yes |
Cleanup
The adapter holds an open database connection. Call close() when you no longer need it (e.g., on page unload or component teardown).
const adapter = new IndexedDbAdapter();
const cache = new Cache({ adapter });
// When done:
adapter.close();Example
import { Cache } from '@sunacchi/cache-kit';
import { IndexedDbAdapter } from '@sunacchi/cache-kit-indexeddb';
const adapter = new IndexedDbAdapter({ dbName: 'my-app' });
const cache = new Cache<Record<string, unknown>>({
adapter,
defaultTtl: 300_000,
maxSize: 500,
eviction: 'lru',
namespace: 'api',
});
const user = await cache.getOrCreate('user:me', async () => {
const res = await fetch('/api/me');
return await res.json();
});
// On cleanup
adapter.close();Exports
export { IndexedDbAdapter } from './indexeddb-adapter.js';
export type { IndexedDbAdapterOptions } from './indexeddb-adapter.js';License
MIT
