feat(api): add Dakao integration
This commit is contained in:
1 parent
9ebdc9b61f
commit
ba3894cba2
10 files changed
+1257
No files matched your search
@@ -0,0 +1,35 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
const TTL_MS = 5 * 60 * 1000;
|
||||
const MAX_ENTRIES = 5000;
|
||||
|
||||
@Injectable()
|
||||
export class IdempotencyCache {
|
||||
private readonly entries = new Map<string, number>();
|
||||
|
||||
public has(key: string): boolean {
|
||||
const expiresAt = this.entries.get(key);
|
||||
if (expiresAt === undefined) return false;
|
||||
if (expiresAt < Date.now()) {
|
||||
this.entries.delete(key);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public remember(key: string): void {
|
||||
this.evictExpired();
|
||||
if (this.entries.size >= MAX_ENTRIES) {
|
||||
const oldestKey = this.entries.keys().next().value;
|
||||
if (oldestKey !== undefined) this.entries.delete(oldestKey);
|
||||
}
|
||||
this.entries.set(key, Date.now() + TTL_MS);
|
||||
}
|
||||
|
||||
private evictExpired(): void {
|
||||
const now = Date.now();
|
||||
for (const [key, expiresAt] of this.entries) {
|
||||
if (expiresAt < now) this.entries.delete(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user