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: limit the number of cached files checked for existence #1153

Merged
merged 3 commits into from
Jun 11, 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
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
Loading