Skip to content

Commit

Permalink
Refactor: detect deleted cached files subset on a timer (#1157)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmercm authored Jun 11, 2024
1 parent 52bfae1 commit a780983
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions src/types/files/fileCache.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Semaphore } from 'async-mutex';

import Constants from '../../constants.js';
import FsPoly from '../../polyfill/fsPoly.js';
import Timer from '../../timer.js';
import Cache from '../cache.js';
import Archive from './archives/archive.js';
import ArchiveEntry, { ArchiveEntryProps } from './archives/archiveEntry.js';
Expand Down Expand Up @@ -46,26 +45,21 @@ 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()]
Timer.setTimeout(async () => {
await Promise.all([...this.cache.keys()]
.map((cacheKey) => cacheKey.split('|')[1])
// Don't delete the key if it's for a disk that isn't mounted right now
.filter((cacheKeyFilePath) => disks.some((disk) => cacheKeyFilePath.startsWith(disk)))
// 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;
.slice(0, Constants.MAX_FS_THREADS)
.map(async (cacheKeyFilePath) => {
if (!await FsPoly.exists(cacheKeyFilePath)) {
// If the file no longer exists, then delete its key from the cache
await this.cache.delete(cacheKeyFilePath);
}
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);
}
});
}),
);
}));
}, 10_000);
}

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

0 comments on commit a780983

Please sign in to comment.