Skip to content

Commit

Permalink
Refactor: limit the number of cached files checked for existence (#1153)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmercm committed Jun 11, 2024
1 parent 073a975 commit 860a9d2
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions src/types/files/fileCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,25 @@ export default class FileCache {
// Delete keys for deleted files
const disks = FsPoly.disksSync();
const semaphore = new Semaphore(Constants.MAX_FS_THREADS);
await Promise.all([...this.cache.keys()].map(async (cacheKey) => {
const cacheKeyFilePath = cacheKey.split('|')[1];
if (!disks.some((disk) => cacheKeyFilePath.startsWith(disk))) {
// Don't delete the key if it's for a disk that isn't mounted right now
return;
}
await semaphore.runExclusive(async () => {
if (!await FsPoly.exists(cacheKeyFilePath)) {
// If the file no longer exists, then delete its key from the cache
await this.cache.delete(cacheKeyFilePath);
}
});
}));
await Promise.all(
[...this.cache.keys()]
// Only process a reasonably sized subset of the keys
.sort(() => Math.random() - 0.5)
.slice(0, Constants.MAX_FS_THREADS * 100)
.map(async (cacheKey) => {
const cacheKeyFilePath = cacheKey.split('|')[1];
if (!disks.some((disk) => cacheKeyFilePath.startsWith(disk))) {
// Don't delete the key if it's for a disk that isn't mounted right now
return;
}
await semaphore.runExclusive(async () => {
if (!await FsPoly.exists(cacheKeyFilePath)) {
// If the file no longer exists, then delete its key from the cache
await this.cache.delete(cacheKeyFilePath);
}
});
}),
);
}

public static async save(): Promise<void> {
Expand Down

0 comments on commit 860a9d2

Please sign in to comment.