pre-tiptap
This commit is contained in:
54
src/server/cache.ts
Normal file
54
src/server/cache.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
interface CacheEntry<T> {
|
||||
data: T;
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
class SimpleCache {
|
||||
private cache: Map<string, CacheEntry<any>> = new Map();
|
||||
|
||||
get<T>(key: string, ttlMs: number): T | null {
|
||||
const entry = this.cache.get(key);
|
||||
if (!entry) return null;
|
||||
|
||||
const now = Date.now();
|
||||
if (now - entry.timestamp > ttlMs) {
|
||||
this.cache.delete(key);
|
||||
return null;
|
||||
}
|
||||
|
||||
return entry.data as T;
|
||||
}
|
||||
|
||||
set<T>(key: string, data: T): void {
|
||||
this.cache.set(key, {
|
||||
data,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
}
|
||||
|
||||
clear(): void {
|
||||
this.cache.clear();
|
||||
}
|
||||
|
||||
delete(key: string): void {
|
||||
this.cache.delete(key);
|
||||
}
|
||||
}
|
||||
|
||||
export const cache = new SimpleCache();
|
||||
|
||||
// Helper function to wrap async operations with caching
|
||||
export async function withCache<T>(
|
||||
key: string,
|
||||
ttlMs: number,
|
||||
fn: () => Promise<T>
|
||||
): Promise<T> {
|
||||
const cached = cache.get<T>(key, ttlMs);
|
||||
if (cached !== null) {
|
||||
return cached;
|
||||
}
|
||||
|
||||
const result = await fn();
|
||||
cache.set(key, result);
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user