feat: implement caching for file info, add rate limiting, and enhance upload handling with retry logic
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
// Simple in-memory cache with TTL support
|
||||
interface CacheEntry<T> {
|
||||
value: T;
|
||||
expiresAt: number;
|
||||
}
|
||||
|
||||
class Cache<T> {
|
||||
private store = new Map<string, CacheEntry<T>>();
|
||||
private ttlMs: number;
|
||||
|
||||
constructor(ttlSeconds: number = 3600) {
|
||||
this.ttlMs = ttlSeconds * 1000;
|
||||
}
|
||||
|
||||
set(key: string, value: T): void {
|
||||
this.store.set(key, {
|
||||
value,
|
||||
expiresAt: Date.now() + this.ttlMs,
|
||||
});
|
||||
}
|
||||
|
||||
get(key: string): T | null {
|
||||
const entry = this.store.get(key);
|
||||
if (!entry) return null;
|
||||
|
||||
if (Date.now() > entry.expiresAt) {
|
||||
this.store.delete(key);
|
||||
return null;
|
||||
}
|
||||
|
||||
return entry.value;
|
||||
}
|
||||
|
||||
has(key: string): boolean {
|
||||
return this.get(key) !== null;
|
||||
}
|
||||
|
||||
delete(key: string): void {
|
||||
this.store.delete(key);
|
||||
}
|
||||
|
||||
clear(): void {
|
||||
this.store.clear();
|
||||
}
|
||||
|
||||
size(): number {
|
||||
return this.store.size;
|
||||
}
|
||||
|
||||
// Cleanup expired entries
|
||||
cleanup(): number {
|
||||
let removed = 0;
|
||||
const now = Date.now();
|
||||
|
||||
for (const [key, entry] of this.store.entries()) {
|
||||
if (now > entry.expiresAt) {
|
||||
this.store.delete(key);
|
||||
removed++;
|
||||
}
|
||||
}
|
||||
|
||||
return removed;
|
||||
}
|
||||
}
|
||||
|
||||
// File info cache (1 hour TTL)
|
||||
export const fileInfoCache = new Cache<{
|
||||
file_size: number;
|
||||
mime_type: string;
|
||||
file_path: string;
|
||||
}>(3600);
|
||||
|
||||
// Cleanup expired cache entries every 5 minutes
|
||||
setInterval(() => {
|
||||
const removed = fileInfoCache.cleanup();
|
||||
if (removed > 0) {
|
||||
console.log(`Cleaned up ${removed} expired cache entries`);
|
||||
}
|
||||
}, 5 * 60 * 1000);
|
||||
|
||||
export { Cache };
|
||||
Reference in New Issue
Block a user