mirror of
https://activitypub.software/TransFem-org/Sharkey
synced 2024-11-25 15:35:12 +00:00
optimize cache GC by stopping early
This commit is contained in:
parent
672f1ea684
commit
ba09338268
1 changed files with 8 additions and 4 deletions
|
@ -200,7 +200,7 @@ export class RedisSingleCache<T> {
|
||||||
|
|
||||||
export class MemoryKVCache<T> {
|
export class MemoryKVCache<T> {
|
||||||
private readonly cache = new Map<string, { date: number; value: T; }>();
|
private readonly cache = new Map<string, { date: number; value: T; }>();
|
||||||
private readonly gcIntervalHandle = setInterval(() => this.gc(), 1000 * 60 * 3);
|
private readonly gcIntervalHandle = setInterval(() => this.gc(), 1000 * 60 * 3); // 3m
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly lifetime: number,
|
private readonly lifetime: number,
|
||||||
|
@ -289,12 +289,16 @@ export class MemoryKVCache<T> {
|
||||||
@bindThis
|
@bindThis
|
||||||
public gc(): void {
|
public gc(): void {
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
|
|
||||||
for (const [key, { date }] of this.cache.entries()) {
|
for (const [key, { date }] of this.cache.entries()) {
|
||||||
if ((now - date) > this.lifetime) {
|
// The map is ordered from oldest to youngest.
|
||||||
|
// We can stop once we find an entry that's still active, because all following entries must *also* be active.
|
||||||
|
const age = now - date;
|
||||||
|
if (age < this.lifetime) break;
|
||||||
|
|
||||||
this.cache.delete(key);
|
this.cache.delete(key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@bindThis
|
@bindThis
|
||||||
public dispose(): void {
|
public dispose(): void {
|
||||||
|
|
Loading…
Reference in a new issue