-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: in-memory cache file CRC32 calculation (#430)
- Loading branch information
Showing
3 changed files
with
65 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Mutex } from 'async-mutex'; | ||
|
||
export default class Cache<K, V> { | ||
private readonly keyOrder: K[] = []; | ||
|
||
private readonly keyValues = new Map<K, V>(); | ||
|
||
private readonly keyMutexes = new Map<K, Mutex>(); | ||
|
||
private readonly keyMutexesMutex = new Mutex(); | ||
|
||
private readonly maxSize: number; | ||
|
||
constructor(maxSize: number) { | ||
this.maxSize = maxSize; | ||
} | ||
|
||
public async getOrCompute(key: K, runnable: () => (V | Promise<V>)): Promise<V> { | ||
// Get a mutex for `key` | ||
const keyMutex = await this.keyMutexesMutex.runExclusive(() => { | ||
if (!this.keyMutexes.has(key)) { | ||
this.keyMutexes.set(key, new Mutex()); | ||
} | ||
return this.keyMutexes.get(key) as Mutex; | ||
}); | ||
|
||
// Only allow one concurrent fetch/compute for `key` | ||
return keyMutex.runExclusive(async () => { | ||
if (this.keyValues.has(key)) { | ||
return this.keyValues.get(key) as V; | ||
} | ||
|
||
const val = await runnable(); | ||
this.set(key, val); | ||
return val; | ||
}); | ||
} | ||
|
||
private set(key: K, val: V): void { | ||
if (!this.keyValues.has(key)) { | ||
this.keyOrder.push(key); | ||
} | ||
this.keyValues.set(key, val); | ||
|
||
// Delete old values | ||
if (this.keyValues.size > this.maxSize) { | ||
const staleKey = this.keyOrder.splice(0, 1)[0]; | ||
this.keyValues.delete(staleKey); | ||
this.keyMutexes.delete(staleKey); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters