Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: keep only a limited number of cache key mutexes #1237

Merged
merged 2 commits into from
Jul 25, 2024
Merged
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
20 changes: 20 additions & 0 deletions src/types/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ export interface CacheProps {
export default class Cache<V> {
private static readonly BUFFER_ENCODING: BufferEncoding = 'binary';

private static readonly KEY_MUTEXES_MAX_COUNT = 1000;

private keyOrder: Set<string> = new Set();

private keyValues = new Map<string, V>();

private readonly keyMutexes = new Map<string, Mutex>();

private keyMutexesLru: Set<string> = new Set();

private readonly keyMutexesMutex = new Mutex();

private hasChanged: boolean = false;
Expand Down Expand Up @@ -147,6 +151,7 @@ export default class Cache<V> {
this.keyOrder.delete(key);
this.keyValues.delete(key);
this.keyMutexes.delete(key);
this.keyMutexesLru.delete(key);
this.saveWithTimeout();
}

Expand All @@ -155,7 +160,22 @@ export default class Cache<V> {
const keyMutex = await this.keyMutexesMutex.runExclusive(() => {
if (!this.keyMutexes.has(key)) {
this.keyMutexes.set(key, new Mutex());
this.keyMutexesLru = new Set([key, ...this.keyMutexesLru]);

// Expire least recently used keys
[...this.keyMutexesLru]
.filter((lruKey) => !this.keyMutexes.get(lruKey)?.isLocked())
.slice(Cache.KEY_MUTEXES_MAX_COUNT)
.forEach((lruKey) => {
this.keyMutexes.delete(lruKey);
this.keyMutexesLru.delete(lruKey);
});
}

// Mark this key as recently used
this.keyMutexesLru.delete(key);
this.keyMutexesLru = new Set([key, ...this.keyMutexesLru]);

return this.keyMutexes.get(key) as Mutex;
});

Expand Down
Loading