A cache-manager compatible file-based cache using Links Notation (.lino) format instead of JSON.
- cache-manager compatible - Implements the full cache-manager store interface
- Links Notation storage - Uses lino-objects-codec for serialization
- Two storage modes:
- Folder mode - Each cache key stored in a separate
.linofile - Single-file mode - All cache entries in one
.linofile
- Folder mode - Each cache key stored in a separate
- TTL support - Time-to-live for automatic expiration
- Multi-runtime - Works with Node.js, Bun, and Deno
- TypeScript support - Full type definitions included
npm install lino-cacheimport { LinoCache, createLinoCache, linoStore } from 'lino-cache';
// Create a cache instance (folder mode by default)
const cache = new LinoCache({
basePath: '.cache',
ttl: 60000, // Default TTL: 1 minute
});
// Basic operations
await cache.set('user:1', { name: 'Alice', age: 30 });
const user = await cache.get('user:1');
console.log(user); // { name: 'Alice', age: 30 }
// Delete
await cache.del('user:1');
// Check existence
const exists = await cache.has('user:1'); // falseEach cache key is stored in a separate .lino file. Best for:
- Large number of cache entries
- Independent access to cache entries
- When you need to inspect individual cached values
const cache = new LinoCache({
mode: 'folder', // Default
basePath: '.cache',
});
await cache.set('key1', 'value1'); // Creates .cache/key1.lino
await cache.set('key2', 'value2'); // Creates .cache/key2.linoAll cache entries stored in one .lino file. Best for:
- Small number of cache entries
- When you want all cache data in one file
- Simpler file management
const cache = new LinoCache({
mode: 'file',
basePath: '.cache',
fileName: 'cache.lino',
});
await cache.set('key1', 'value1'); // Both stored in
await cache.set('key2', 'value2'); // .cache/cache.linointerface LinoCacheOptions {
ttl?: number; // Default TTL in milliseconds (0 = no expiration)
mode?: 'file' | 'folder'; // Storage mode (default: 'folder')
basePath?: string; // Cache directory (default: '.cache')
fileName?: string; // File name for single-file mode (default: 'cache.lino')
}Sets a value in the cache.
await cache.set('key', 'value');
await cache.set('key', 'value', 5000); // With 5 second TTLGets a value from the cache. Returns undefined if not found or expired.
const value = await cache.get('key');Deletes a value from the cache.
const deleted = await cache.del('key'); // true if deletedChecks if a key exists and is not expired.
const exists = await cache.has('key'); // booleanReturns all non-expired keys.
const allKeys = await cache.keys(); // ['key1', 'key2', ...]Clears all values from the cache.
await cache.clear();Sets multiple values at once.
await cache.mset([
{ key: 'key1', value: 'value1' },
{ key: 'key2', value: 'value2', ttl: 5000 },
]);Gets multiple values at once.
const values = await cache.mget(['key1', 'key2', 'key3']);
// [value1, value2, undefined]Deletes multiple values at once.
const deleted = await cache.mdel(['key1', 'key2']);Wraps a function with caching. Returns cached value if available, otherwise executes the function and caches the result.
const data = await cache.wrap(
'expensive-operation',
async () => {
// This only runs if not cached
return await fetchExpensiveData();
},
60000
);Gets the remaining TTL for a key in milliseconds.
const remaining = await cache.ttl('key');
// > 0: remaining time
// -1: no TTL set
// -2: key not foundCloses the cache and releases resources.
await cache.disconnect();Creates a new LinoCache instance.
const cache = createLinoCache({ basePath: '.cache' });Creates a cache-manager compatible store.
import { caching } from 'cache-manager';
import { linoStore } from 'lino-cache';
const cache = await caching(
linoStore({
basePath: '.cache',
ttl: 60000,
})
);Links Notation (.lino) is a human-readable serialization format that:
- Supports circular references natively
- Preserves object identity
- Is more compact for certain data structures
- Provides better debugging experience
Learn more: Links Notation
# Install dependencies
npm install
# Run tests
npm test
# Lint code
npm run lint
# Format code
npm run format
# Run all checks
npm run checkUnlicense - Public Domain