Skip to content

Commit 03e8aa1

Browse files
committed
feat: add performance optimization components (PRs #19-22)
- Edge Cache Service with Cloudflare Cache API support - Sub-10ms cache access at the edge - Automatic cache invalidation and tag-based purging - Browser and edge TTL configuration - Cache warmup functionality - Performance Monitoring middleware - Universal performance tracking for all operations - P50, P95, P99 statistics calculation - @TrackPerformance decorator support - Integration with Express, Hono, Koa, Fastify - Memory monitoring for Node.js and browsers - Queue Service with batch processing - Platform-agnostic queue interface - Batch processing with configurable size - Retry logic with exponential backoff - Type-safe message handlers - Cloudflare Queues and in-memory adapters Benefits: - 🚀 Sub-10ms edge cache access (paid tier) - 📊 Comprehensive performance metrics - 🔄 Efficient batch processing for background tasks - 🎯 Production-tested patterns from Kogotochki bot - 💡 Platform-agnostic design for multi-cloud support All components are TypeScript strict mode compliant and tested.
1 parent ba381bc commit 03e8aa1

File tree

9 files changed

+1982
-0
lines changed

9 files changed

+1982
-0
lines changed

src/core/interfaces/cache.ts

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/**
2+
* Cache interfaces for multi-layer caching strategy
3+
*/
4+
5+
export interface CacheOptions {
6+
/** Time-to-live in seconds */
7+
ttl?: number;
8+
/** Browser cache TTL */
9+
browserTTL?: number;
10+
/** Edge cache TTL */
11+
edgeTTL?: number;
12+
/** Cache tags for invalidation */
13+
tags?: string[];
14+
/** Cache key prefix */
15+
prefix?: string;
16+
/** Whether to bypass cache */
17+
bypass?: boolean;
18+
}
19+
20+
export interface RouteCacheConfig {
21+
ttl: number;
22+
tags: string[];
23+
browserTTL?: number;
24+
edgeTTL?: number;
25+
}
26+
27+
export interface IEdgeCacheService {
28+
/**
29+
* Get value from edge cache
30+
*/
31+
get(key: string): Promise<Response | null>;
32+
33+
/**
34+
* Put value into edge cache
35+
*/
36+
put(key: string, response: Response, options?: CacheOptions): Promise<void>;
37+
38+
/**
39+
* Delete from edge cache
40+
*/
41+
delete(key: string): Promise<boolean | void>;
42+
43+
/**
44+
* Purge by tags
45+
*/
46+
purgeByTags(tags: string[]): Promise<void>;
47+
48+
/**
49+
* Match request against cache
50+
*/
51+
match(request: Request): Promise<Response | undefined>;
52+
53+
/**
54+
* Get cached response
55+
*/
56+
getCachedResponse?(request: Request): Promise<Response | null>;
57+
58+
/**
59+
* Cache response
60+
*/
61+
cacheResponse?(request: Request, response: Response, config: RouteCacheConfig): Promise<void>;
62+
63+
/**
64+
* Warm up cache
65+
*/
66+
warmUp?(
67+
keys:
68+
| Array<{
69+
key: string;
70+
factory: () => Promise<unknown>;
71+
options?: CacheOptions;
72+
}>
73+
| string[],
74+
): Promise<void>;
75+
}
76+
77+
export interface ICacheLayer {
78+
/**
79+
* Get value from cache
80+
*/
81+
get<T>(key: string): Promise<T | null>;
82+
83+
/**
84+
* Set value in cache
85+
*/
86+
set<T>(key: string, value: T, options?: CacheOptions): Promise<void>;
87+
88+
/**
89+
* Delete from cache
90+
*/
91+
delete(key: string): Promise<boolean>;
92+
93+
/**
94+
* Check if key exists
95+
*/
96+
has(key: string): Promise<boolean>;
97+
98+
/**
99+
* Clear all cache
100+
*/
101+
clear(): Promise<void>;
102+
}
103+
104+
export interface IMultiLayerCache {
105+
/**
106+
* Memory cache layer
107+
*/
108+
memory: ICacheLayer;
109+
110+
/**
111+
* KV cache layer
112+
*/
113+
kv: ICacheLayer;
114+
115+
/**
116+
* Edge cache layer
117+
*/
118+
edge?: IEdgeCacheService;
119+
120+
/**
121+
* Get with fallback through layers
122+
*/
123+
get<T>(key: string, fetcher?: () => Promise<T>): Promise<T | null>;
124+
125+
/**
126+
* Set in all layers
127+
*/
128+
set<T>(key: string, value: T, options?: CacheOptions): Promise<void>;
129+
}

0 commit comments

Comments
 (0)