A comprehensive, type-safe caching library with support for multiple backends, memoization,
TTL (Time To Live), statistics tracking, and extensible architecture.
Overview
The cache system provides a unified interface for different storage backends through the
ICache interface, with implementations for in-memory storage, file system persistence,
and extensibility for custom backends like Redis, DynamoDB, etc.
Key Features
🏭 Factory Pattern: Create cache instances using CacheFactory.create()
💾 Multiple Backends: Memory, filesystem, and extensible for custom implementations
⚡ Memoization: Function result caching with CacheUtils.memoize()
⏰ TTL Support: Automatic expiration of cache entries
📊 Statistics: Hit/miss rates and performance monitoring
🏠 Namespacing: Isolate cache entries by context
🔧 Type Safety: Full TypeScript support with generics
🛡️ Error Handling: Graceful degradation when cache operations fail
Pros: Extremely fast, no I/O overhead
Cons: Data lost on restart, limited by available RAM
Best for: Session data, temporary computations, frequently accessed data
File System Cache
Persistent storage using JSON files (requires ts_node package).
// Or via factory constfileCache2 = CacheFactory.create('filesystem', { cacheDir:'/var/cache/myapp' });
Pros: Persistent across restarts, unlimited storage
Cons: Slower than memory, file I/O overhead
Best for: API responses, computed results, data that needs persistence
Custom Backends
Extend BaseCache to implement Redis, DynamoDB, or other storage systems:
TidyScripts Cache System
A comprehensive, type-safe caching library with support for multiple backends, memoization, TTL (Time To Live), statistics tracking, and extensible architecture.
Overview
The cache system provides a unified interface for different storage backends through the
ICache
interface, with implementations for in-memory storage, file system persistence, and extensibility for custom backends like Redis, DynamoDB, etc.Key Features
CacheFactory.create()
CacheUtils.memoize()
Quick Start
Basic Cache Usage
Function Memoization
Cache Backends
Memory Cache
Fast in-memory storage using JavaScript Map. Data is lost when process exits.
Pros: Extremely fast, no I/O overhead Cons: Data lost on restart, limited by available RAM Best for: Session data, temporary computations, frequently accessed data
File System Cache
Persistent storage using JSON files (requires ts_node package).
Pros: Persistent across restarts, unlimited storage Cons: Slower than memory, file I/O overhead Best for: API responses, computed results, data that needs persistence
Custom Backends
Extend
BaseCache
to implement Redis, DynamoDB, or other storage systems:Advanced Usage Patterns
API Response Caching
Database Query Caching
Multi-Layer Caching
Batch Operations
Configuration Options
Cache Configuration
Memoization Options
Monitoring & Debugging
Statistics Tracking
Verbose Logging
Performance Considerations
Error Handling
The cache system is designed for graceful degradation:
Testing