Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 49 additions & 4 deletions src/caching/caching.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { Cache } from 'cache-manager';
import { MetricsCollectionService } from '../monitoring/metrics/metrics-collection.service';
import { DistributedLockService } from '../orchestration/locks/distributed-lock.service';

export interface CacheStats {
hits: number;
Expand All @@ -18,6 +19,7 @@
constructor(
@Inject(CACHE_MANAGER) private readonly cacheManager: Cache,
@Optional() private readonly metrics?: MetricsCollectionService,
@Optional() private readonly lockService?: DistributedLockService,
) {}

async get<T>(key: string): Promise<T | undefined> {
Expand All @@ -35,15 +37,58 @@
await this.cacheManager.set(key, value, ttlMs);
}

async getOrSet<T>(key: string, factory: () => Promise<T>, ttlSeconds?: number): Promise<T> {
private async sleep(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms));

Check failure on line 41 in src/caching/caching.service.ts

View workflow job for this annotation

GitHub Actions / validate

Replace `resolve` with `(resolve)`
}

async getOrSet<T>(
key: string,
factory: () => Promise<T>,
ttlSeconds?: number,
lockTimeoutMs = 5000,
pollIntervalMs = 100,
maxWaitMs = 30000,
): Promise<T> {
const cached = await this.get<T>(key);
if (cached !== undefined) {
return cached;
}

const value = await factory();
await this.set(key, value, ttlSeconds);
return value;
if (!this.lockService) {
this.logger.warn('DistributedLockService not available, proceeding without lock');
const value = await factory();
await this.set(key, value, ttlSeconds);
return value;
}

const lockKey = `lock:cache:${key}`;
const startTime = Date.now();

while (Date.now() - startTime < maxWaitMs) {
const acquired = await this.lockService.acquireLock(lockKey, lockTimeoutMs);
if (acquired) {
try {
let value = await this.get<T>(key);
if (value !== undefined) {
return value;
}
value = await factory();
await this.set(key, value, ttlSeconds);
return value;
} finally {
await this.lockService.releaseLock(lockKey);
}
} else {
const value = await this.get<T>(key);
if (value !== undefined) {
return value;
}
const jitter = Math.random() * pollIntervalMs;
await this.sleep(pollIntervalMs + jitter);
}
}

throw new Error(`Timeout waiting for cache key: ${key}`);
}

async delete(key: string): Promise<void> {
Expand Down
Loading