Gathering detailed insights and metrics for cachly
Gathering detailed insights and metrics for cachly
Gathering detailed insights and metrics for cachly
Gathering detailed insights and metrics for cachly
Type-safe, production-ready in-memory cache system for Node.js and TypeScript with advanced features.
npm install cachly
Typescript
Module System
Min. Node Version
Node Version
NPM Version
TypeScript (93.48%)
JavaScript (6.52%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
1 Stars
5 Commits
1 Branches
1 Contributors
Updated on Jul 07, 2025
Latest Version
1.0.1
Package Id
cachly@1.0.1
Unpacked Size
270.23 kB
Size
54.20 kB
File Count
74
NPM Version
11.4.2
Node Version
24.3.0
Published on
Jul 06, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
A type-safe, production-ready in-memory cache system for Node.js and TypeScript, featuring advanced dependency tracking, intelligent invalidation, TTL, stale-while-revalidate, async operations, event system, statistics, advanced eviction, partitioning, compression, circuit breaker, distributed support, tags, groups, bulk operations, decorators, middleware, CLI tools, and cache hit/miss hooks.
getOrCompute
, getOrComputeWithStale
)1npm install cachly
1import { Cachly } from 'cachly'; 2 3const cache = new Cachly({ 4 maxItems: 1000, 5 defaultTtl: 60000, 6 staleWhileRevalidate: true, 7 compression: { enabled: true, algorithm: 'gzip', threshold: 1024 }, 8}); 9 10// Set and get 11await cache.set('user:1', { id: 1, name: 'John' }); 12const user = await cache.get('user:1'); 13 14// Async computation 15const stats = await cache.getOrCompute('stats', async () => computeExpensiveStats(), { ttl: 30000 }); 16 17// Dependency tracking 18await cache.set('post:1', postData, { dependsOn: ['user:1'] }); 19cache.delete('user:1'); // Also invalidates post:1
You can run custom logic on every cache hit or miss, e.g. for logging, metrics, or tracing.
1const cache = new Cachly({ 2 onHit: (key) => { 3 console.log('Cache HIT:', key); 4 }, 5 onMiss: (key) => { 6 console.log('Cache MISS:', key); 7 } 8}); 9 10await cache.set('foo', 123); 11await cache.get('foo'); // logs: Cache HIT: foo 12await cache.get('bar'); // logs: Cache MISS: bar
1const cache = new Cachly(); 2 3cache.setHitHook((key) => { 4 // Custom logic for hit 5 console.log('HIT', key); 6}); 7 8cache.setMissHook((key) => { 9 // Custom logic for miss 10 console.log('MISS', key); 11});
1// Set with tags 2await cache.set('user:1', userData, { tags: ['users', 'active'] }); 3await cache.set('user:2', userData, { tags: ['users', 'inactive'] }); 4 5// Invalidate by tag 6await cache.invalidateByTag('users'); // Removes all user data 7 8// Bulk operations 9const results = await cache.bulk({ 10 get: ['key1', 'key2'], 11 set: [ 12 { key: 'key3', value: 'value3' }, 13 { key: 'key4', value: 'value4', options: { ttl: 5000 } } 14 ], 15 delete: ['old-key'], 16 invalidateByTag: ['expired-tag'] 17});
1// Create and manage groups 2const userGroup = cache.createGroup('users', { maxItems: 100 }); 3cache.addToGroup('users', 'user:1'); 4cache.addToGroup('users', 'user:2'); 5 6// Get group keys 7const userKeys = cache.getGroupKeys('users'); 8 9// Delete entire group 10cache.deleteGroup('users');
1// Pattern-based key discovery 2const userKeys = cache.getKeysByPattern('user:*'); 3const postKeys = cache.getKeysByPattern('post:*'); 4 5// Analytics 6const topKeys = cache.getTopKeys(10); 7const leastUsed = cache.getLeastUsedKeys(10); 8const oldestKeys = cache.getKeysByAge(10);
1import { cache, cacheWithTags, invalidateTags } from 'cachly'; 2 3class UserService { 4 @cache({ ttl: 30000 }) 5 async getUser(id: string) { 6 return await fetchUserFromDB(id); 7 } 8 9 @cacheWithTags(['users'], 60000) 10 async getUsers() { 11 return await fetchUsersFromDB(); 12 } 13 14 @invalidateTags(['users']) 15 async updateUser(id: string, data: any) { 16 return await updateUserInDB(id, data); 17 } 18}
1import express from 'express'; 2import { createCacheMiddleware, createInvalidateMiddleware } from 'cachly'; 3 4const app = express(); 5const cache = new Cachly(); 6 7// Cache middleware 8app.use('/api/users', createCacheMiddleware({ 9 cache, 10 ttl: 30000, 11 tags: (req) => ['users', req.params.id], 12 skip: (req) => req.method !== 'GET' 13})); 14 15// Invalidate middleware 16app.post('/api/users/:id', createInvalidateMiddleware({ 17 cache, 18 tags: ['users'] 19}));
1# Install globally 2npm install -g cachly 3 4# Start CLI 5cachly 6 7# Available commands 8cachly> help 9cachly> set user:1 "John Doe" 30000 10cachly> get user:1 11cachly> stats 12cachly> keys user:* 13cachly> top 10 14cachly> clear
1await cache.set<T>(key: string, value: T, options?: CacheOptions): Promise<void> 2await cache.get<T>(key: string): Promise<T | undefined> 3cache.has(key: string): boolean 4cache.delete(key: string): void 5cache.clear(): void
1await cache.getOrCompute<T>(key: string, loader: () => Promise<T>, options?: CacheOptions): Promise<T> 2await cache.getOrComputeWithStale<T>(key: string, loader: () => Promise<T>, options?: CacheOptions): Promise<T>
1await cache.invalidateByTag(tag: string): Promise<string[]> 2await cache.invalidateByTags(tags: string[]): Promise<Record<string, string[]>> 3cache.getKeysByTag(tag: string): string[] 4cache.getTagsByKey(key: string): string[] 5cache.createGroup(name: string, config?: Partial<CacheConfig>): CacheGroup 6cache.addToGroup(groupName: string, key: string): boolean 7cache.getGroupKeys(groupName: string): string[] 8cache.deleteGroup(groupName: string): boolean
1await cache.bulk(operation: BulkOperation): Promise<any>
1cache.getKeys(pattern?: string): string[] 2cache.getKeysByPattern(pattern: string): string[] 3cache.getTopKeys(limit?: number): Array<{ key: string; accessCount: number }> 4cache.getLeastUsedKeys(limit?: number): Array<{ key: string; accessCount: number }> 5cache.getKeysByAge(limit?: number): Array<{ key: string; age: number }>
1cache.on('hit', (key) => { /* ... */ }); 2cache.on('miss', (key) => { /* ... */ }); 3cache.on('set', (key, value) => { /* ... */ }); 4cache.on('delete', (key) => { /* ... */ }); 5cache.on('evict', (key, reason) => { /* ... */ }); 6cache.on('compress', (key, originalSize, compressedSize) => { /* ... */ }); 7cache.on('partitionHit', (partition, key) => { /* ... */ }); 8cache.on('tagInvalidated', (tag, affectedKeys) => { /* ... */ }); 9cache.on('bulkOperation', (operation, result) => { /* ... */ }); 10cache.on('groupCreated', (group) => { /* ... */ }); 11cache.on('groupDeleted', (groupName) => { /* ... */ });
1const stats = cache.stats(); 2// { hits, misses, evictions, memoryUsage, keyCount, totalAccesses, hitRate, missRate, avgLoadTime, compressionRatio } 3 4const metrics = cache.metrics(); 5// { hitRate, missRate, avgLoadTime, memoryEfficiency, compressionRatio, circuitBreakerTrips, partitionDistribution } 6 7const health = cache.health(); 8// { status, issues, lastCheck, uptime }
getPartitionInfo(partitionId)
, getAllPartitions()
, isBalanced()
getCircuitBreakerState()
warm(items)
, warmProgressive(items)
, warmWithDependencies(items)
1interface CacheConfig { 2 maxItems?: number; 3 maxMemory?: number; 4 defaultTtl?: number; 5 staleWhileRevalidate?: boolean; 6 log?: boolean; 7 namespace?: string; 8 persistence?: 'none' | PersistenceAdapter; 9 evictionPolicy?: 'lru' | 'ttl' | 'manual' | 'lfu'; 10 compression?: { enabled: boolean; algorithm: 'gzip' | 'brotli' | 'lz4'; threshold: number; }; 11 circuitBreaker?: { enabled: boolean; failureThreshold: number; recoveryTimeout: number; }; 12 partitioning?: { enabled: boolean; strategy: 'hash' | 'range' | 'custom'; partitions: number; }; 13 monitoring?: { enabled: boolean; metrics: string[]; }; 14 distributed?: { enabled: boolean; nodes: string[]; replication: boolean; consistency: 'eventual' | 'strong'; partitionStrategy: string; }; 15 onHit?: (key: string) => void; 16 onMiss?: (key: string) => void; 17}
1import { FSAdapter } from 'cachly'; 2const cache = new Cachly({ persistence: new FSAdapter('./cache') });
1import { CachlyNamespace } from 'cachly'; 2const userCache = CachlyNamespace.namespace('user'); 3const postCache = CachlyNamespace.namespace('post');
1await cache.warm([ 2 { key: 'config', loader: loadConfig, ttl: 120000 }, 3 { key: 'meta', loader: fetchMeta }, 4 { key: 'stats', loader: computeStats, dependsOn: ['config'] } 5]);
MIT
No vulnerabilities found.
No security vulnerabilities found.