-
Notifications
You must be signed in to change notification settings - Fork 0
cache
awekrx edited this page May 29, 2026
·
1 revision
import { cache } from '@dev-suite/decorators/cache'
method
Cache expensive method results by input arguments.
- Manual per-method
Map+ TTL logic - Repeated memoization wrappers
class ProductService {
private cache = new Map<string, { value: Product; exp: number }>();
async getById(id: string) {
const now = Date.now();
const hit = this.cache.get(id);
if (hit && hit.exp > now) return hit.value;
const value = await this.repo.fetchById(id);
this.cache.set(id, { value, exp: now + 30_000 });
return value;
}
}import { cache } from '@dev-suite/decorators/cache';
class ProductService {
@cache({ ttlMs: 30_000, keyResolver: ([id]) => String(id) })
async getById(id: string) {
return this.repo.fetchById(id);
}
}- Eliminates inline cache bookkeeping from business methods.
- TTL and key strategy are explicit at declaration.
class FxService {
private byPair = new Map<string, number>();
async rate(base: string, quote: string) {
const key = base + ':' + quote;
if (this.byPair.has(key)) return this.byPair.get(key)!;
const value = await this.remote.rate(base, quote);
this.byPair.set(key, value);
return value;
}
}import { cache } from '@dev-suite/decorators/cache';
class FxService {
@cache({ keyResolver: ([base, quote]) => String(base) + ':' + String(quote) })
async rate(base: string, quote: string) {
return this.remote.rate(base, quote);
}
}- Works for composite keys without hand-written cache code.
- Behavior stays consistent across services.