speedy-cache
v1.0.0
Published
A blazing-fast caching library for Node.js and browsers with auto-expiry, LRU cache, and persistent storage (Redis, LocalStorage, or File-based).
Maintainers
Readme
SpeedyCache
SpeedyCache is a blazing-fast caching library for Node.js and browsers with auto-expiry, LRU cache, and persistent storage (Redis, LocalStorage, or File-based).
Features
- Auto-expiry: Automatically removes expired items from the cache.
- LRU Cache: Implements Least Recently Used (LRU) eviction policy.
- Persistent Storage: Supports memory, Redis, LocalStorage, and file-based storage.
Installation
Install the package via npm:
npm install speedy-cacheUsage
Basic Usage
import SpeedyCache from 'speedy-cache';
const cache = new SpeedyCache({ maxSize: 100, expiry: 60000 });
cache.set('key', 'value');
console.log(cache.get('key')); // 'value'Options
maxSize: Maximum number of items in the cache (default: 100).expiry: Time in milliseconds before an item expires (default: 60000).storage: Type of storage ('memory', 'redis', 'localStorage', 'file').
Example with File-based Storage
import SpeedyCache from 'speedy-cache';
const cache = new SpeedyCache({ maxSize: 100, expiry: 60000, storage: 'file' });
cache.set('key', 'value');
console.log(cache.get('key')); // 'value'
// Reload the cache from file
const newCacheInstance = new SpeedyCache({ maxSize: 100, expiry: 60000, storage: 'file' });
console.log(newCacheInstance.get('key')); // 'value'Example with LRU Eviction
import SpeedyCache from 'speedy-cache';
const cache = new SpeedyCache({ maxSize: 2, expiry: 60000 });
cache.set('key1', 'value1');
cache.set('key2', 'value2');
cache.set('key3', 'value3');
console.log(cache.get('key1')); // null (evicted)
console.log(cache.get('key2')); // 'value2'
console.log(cache.get('key3')); // 'value3'API
set(key: string, value: any): void
Adds an item to the cache. If the cache exceeds the maxSize, the least recently used item will be evicted.
get(key: string): any
Retrieves an item from the cache. If the item has expired or does not exist, it returns null.
