graphwork-cache
v2.0.1
Published
Cache management for GraphWork Framework 2.0
Maintainers
Readme
Cache
Cache management for GraphWork Framework 2.0
Overview
The Cache module provides efficient caching mechanisms for the GraphWork Framework. It includes implementations of LRU (Least Recently Used) cache and memory cache to optimize performance and reduce redundant computations.
Installation
npm install graphwork-cacheFeatures
- LRU Cache: Implements Least Recently Used eviction policy
- Memory Cache: Simple in-memory caching solution
- TTL Support: Time-to-live expiration for cached items
- Performance Optimized: Efficient memory usage and fast access times
- TypeScript Support: Full TypeScript definitions included
Usage
LRU Cache
import { LRUCache } from 'graphwork-cache';
// Create an LRU cache with a maximum size of 100 items
const cache = new LRUCache<string>({
maxSize: 100,
ttl: 3600000 // 1 hour in milliseconds
});
// Set a value
cache.set('key1', 'value1');
// Get a value
const value = cache.get('key1');
// Check if a key exists
const exists = cache.has('key1');
// Delete a key
cache.delete('key1');
// Clear all entries
cache.clear();Memory Cache
import { MemoryCache } from 'graphwork-cache';
// Create a memory cache
const cache = new MemoryCache<string>({
ttl: 1800000 // 30 minutes in milliseconds
});
// Set a value
cache.set('key1', 'value1');
// Get a value
const value = cache.get('key1');Cache Manager
import { CacheManager } from 'graphwork-cache';
// Create a cache manager
const cacheManager = new CacheManager({
maxSize: 1000
});
// Create different cache instances
const userCache = cacheManager.createLRUCache<User>('users');
const productCache = cacheManager.createMemoryCache<Product>('products');
// Use caches
userCache.set('user1', { id: 'user1', name: 'John Doe' });
const user = userCache.get('user1');API
LRUCache
Constructor
new LRUCache<T>(config: CacheConfig)Methods
set(key: string, value: T): void- Sets a value in the cacheget(key: string): T | undefined- Gets a value from the cachehas(key: string): boolean- Checks if a key exists in the cachedelete(key: string): boolean- Deletes a key from the cacheclear(): void- Clears all entries from the cachesize(): number- Returns the number of entries in the cachemaxSize(): number- Returns the maximum size of the cache
MemoryCache
Constructor
new MemoryCache<T>(config: CacheConfig)Methods
set(key: string, value: T): void- Sets a value in the cacheget(key: string): T | undefined- Gets a value from the cachehas(key: string): boolean- Checks if a key exists in the cachedelete(key: string): boolean- Deletes a key from the cacheclear(): void- Clears all entries from the cachesize(): number- Returns the number of entries in the cache
CacheManager
Constructor
new CacheManager(config: CacheManagerConfig)Methods
createLRUCache<T>(name: string, config?: CacheConfig): LRUCache<T>- Creates a new LRU cachecreateMemoryCache<T>(name: string, config?: CacheConfig): MemoryCache<T>- Creates a new memory cachegetCache(name: string): Cache | undefined- Gets a cache by namedeleteCache(name: string): boolean- Deletes a cache by nameclearAll(): void- Clears all cachesgetAllStats(): Record<string, CacheStats>- Gets statistics for all caches
Configuration
CacheConfig
interface CacheConfig {
maxSize?: number; // Maximum number of items (for LRU cache)
ttl?: number; // Time to live in milliseconds
}CacheManagerConfig
interface CacheManagerConfig {
maxSize?: number; // Default maximum size for caches
}Performance
The cache implementations are optimized for:
- Fast O(1) access times
- Efficient memory usage
- Automatic cleanup of expired entries
- Minimal garbage collection overhead
Contributing
See our Contributing Guide for information on how to contribute to this package.
License
This package is licensed under the MIT License. See the LICENSE file for details.
