1.0.0 • Published 6 months ago
@eli_hugi/nodejs-ts-cache-service v1.0.0
NodeJS TypeScript Cache Service
A lightweight, type-safe caching service implementation using node-cache for Node.js applications written in TypeScript.
Features
- Singleton pattern implementation
- Type-safe caching operations
- Optional deep cloning of cached objects
- TTL (Time To Live) support
- Simple and intuitive API
Installation
npm install @eli_hugi/nodejs-ts-cache-service
Usage
import { CacheService } from '@eli_hugi/nodejs-ts-cache-service';
// Get the singleton instance
const cacheService = CacheService.getInstance();
// Store a value with TTL of 600 seconds
cacheService.set('myKey', { data: 'example' }, 600);
// Retrieve a value (with optional deep cloning)
const value = cacheService.get('myKey', true);
// Check if a key exists
const exists = cacheService.has('myKey');
// Delete a key
cacheService.del('myKey');
API
getInstance()
Returns the singleton instance of the cache service.
get<T>(key: string, clone: boolean): T | undefined
Retrieves a value from the cache. If clone
is true, returns a deep clone of the cached value.
set<T>(key: string, value: T, ttl: number): boolean
Stores a value in the cache with the specified TTL (in seconds).
del(key: string): number
Deletes a value from the cache. Returns the number of deleted entries.
has(key: string): boolean
Checks if a key exists in the cache.
License
ISC
1.0.0
6 months ago