aboutsummaryrefslogtreecommitdiff
path: root/src/services/file-cache.ts
diff options
context:
space:
mode:
authorMax Isom <[email protected]>2023-05-13 18:34:29 -0700
committerGitHub <[email protected]>2023-05-13 20:34:29 -0500
commitdd140b50fb79fd66c65aa8c22e0ebbb6109075f0 (patch)
treea968912dd9cbbcb33e2979ae5349f90c9c0633e3 /src/services/file-cache.ts
parentf54d7caa723b53a6a44a067977ed858cc753f7a5 (diff)
downloadmuse-dd140b50fb79fd66c65aa8c22e0ebbb6109075f0.tar.xz
muse-dd140b50fb79fd66c65aa8c22e0ebbb6109075f0.zip
Fix caching (#941)
Diffstat (limited to 'src/services/file-cache.ts')
-rw-r--r--src/services/file-cache.ts30
1 files changed, 13 insertions, 17 deletions
diff --git a/src/services/file-cache.ts b/src/services/file-cache.ts
index 98a079c..3906772 100644
--- a/src/services/file-cache.ts
+++ b/src/services/file-cache.ts
@@ -18,11 +18,11 @@ export default class FileCacheProvider {
}
/**
- * Returns path to cached file if it exists, otherwise throws an error.
+ * Returns path to cached file if it exists, otherwise returns null.
* Updates the `accessedAt` property of the cached file.
* @param hash lookup key
*/
- async getPathFor(hash: string): Promise<string> {
+ async getPathFor(hash: string): Promise<string | null> {
const model = await prisma.fileCache.findUnique({
where: {
hash,
@@ -30,7 +30,7 @@ export default class FileCacheProvider {
});
if (!model) {
- throw new Error('File is not cached');
+ return null;
}
const resolvedPath = path.join(this.config.CACHE_DIR, hash);
@@ -44,7 +44,7 @@ export default class FileCacheProvider {
},
});
- throw new Error('File is not cached');
+ return null;
}
await prisma.fileCache.update({
@@ -76,19 +76,15 @@ export default class FileCacheProvider {
const stats = await fs.stat(tmpPath);
if (stats.size !== 0) {
- try {
- await fs.rename(tmpPath, finalPath);
-
- await prisma.fileCache.create({
- data: {
- hash,
- accessedAt: new Date(),
- bytes: stats.size,
- },
- });
- } catch (error) {
- debug('Errored when moving a finished cache file:', error);
- }
+ await fs.rename(tmpPath, finalPath);
+
+ await prisma.fileCache.create({
+ data: {
+ hash,
+ accessedAt: new Date(),
+ bytes: stats.size,
+ },
+ });
}
await this.evictOldestIfNecessary();