An asynchronous, Promise-wrapped IndexedDB caching engine for modern browsers, featuring native Redis-style TTL (Time-To-Live) expiration mechanics.
- ⚡ Async/Await Ready: Bridges the native event-driven IndexedDB API into standard modern Promises.
- ⏱️ Automatic Eviction: Self-cleaning architecture that purges expired keys from disk during read operations to save storage.
- 📦 Zero Dependencies: Pure JavaScript implementation that runs completely client-side.
- 🔒 High Capacity: Bypasses the strict 5MB quota limitation of
localStorageto securely house gigabytes of structured objects.
npm install @blackbirdjs/cacheCreate an instance of the cache. If the database does not exist on the user's machine, the browser will configure it automatically on the fly.
import { BlackbirdCache } from '@blackbirdjs/cache';
const cache = new BlackbirdCache('MyApplicationCache', 'api_store');You can save plain values, arrays, or deeply nested objects directly without manually calling JSON.stringify(). Pass an optional third parameter to set an expiration window in seconds.
// Permanent storage (survives page reloads indefinitely)
await cache.set('user:theme', 'dark');
// Expiring storage (automatically expires and deletes itself after 60 seconds)
const sessionToken = { token: 'xyz123', role: 'admin' };
await cache.set('user:session', sessionToken, 60);Retrieve your cached object with standard asynchronous operations. If a key's expiration window has passed, the cache will instantly return null and silently delete the stale entry from the user's hard drive to free up space.
const session = await cache.get('user:session');
if (session) {
console.log('Valid session found:', session.role);
} else {
console.log('Session has expired or does not exist.');
}Manually remove any single key out of your storage database block instantly.
await cache.del('user:theme');databaseName(String): The browser IndexedDB file group name. Defaults to'BlackbirdCacheDB'.storeName(String): The underlying table/bucket layout name. Defaults to'cache_store'.
key(String): The unique look-up string.value(any): Any structural JavaScript data payload (Objects, Arrays, Booleans, etc.).ttlInSeconds(Number): Optional. Time until data automatically expires.
- Returns a Promise resolving to the stored payload value, or
nullif the key is missing or expired.
- Returns a Promise resolving to
trueonce the target entry is erased from disk.
Distributed under the Apache License 2.0. See LICENSE for details.