@@ -3,7 +3,7 @@ import { createHash, randomBytes, randomInt } from 'node:crypto'
33
44import consola from 'consola'
55
6- import { findKeyMatch , touchKey } from '~/lib/db'
6+ import { findKeyMatch , findStaleKeys , pruneKeys , touchKey , updateOrCreateKey } from '~/lib/db'
77import { ENV } from '~/lib/env'
88import { logger } from '~/lib/logger'
99import { encodeCacheKey } from '~/lib/storage/driver'
@@ -30,7 +30,7 @@ async function initializeStorageDriver() {
3030
3131 return < StorageAdapter > {
3232 async reserveCache ( key , version , cacheSize ) {
33- logger . debug ( 'Reserve: Trying to reserve cache for' , key , version , cacheSize )
33+ logger . debug ( 'Reserve: Reserving cache for' , key , version , cacheSize )
3434 const bufferKey = `${ key } :${ version } `
3535 const existingBuffer = uploadBuffers . get ( bufferKey )
3636 if ( existingBuffer ) {
@@ -59,7 +59,7 @@ async function initializeStorageDriver() {
5959 }
6060 } ,
6161 async getCacheEntry ( keys , version ) {
62- logger . debug ( 'Get: Trying to get cache entry for' , keys , version )
62+ logger . debug ( 'Get: Getting cache entry for' , keys , version )
6363 const primaryKey = keys [ 0 ]
6464 const restoreKeys = keys . length > 1 ? keys . slice ( 1 ) : undefined
6565
@@ -70,6 +70,8 @@ async function initializeStorageDriver() {
7070 return null
7171 }
7272
73+ await touchKey ( cacheKey . key , cacheKey . version )
74+
7375 const cacheFileName = encodeCacheKey ( cacheKey . key , cacheKey . version )
7476 const hashedKey = createHash ( 'sha256' )
7577 . update ( cacheFileName + DOWNLOAD_SECRET_KEY )
@@ -83,7 +85,7 @@ async function initializeStorageDriver() {
8385 }
8486 } ,
8587 async commitCache ( uploadId ) {
86- logger . debug ( 'Commit: Trying to commit cache for upload' , uploadId )
88+ logger . debug ( 'Commit: Committing cache for upload' , uploadId )
8789
8890 if ( commitLocks . has ( uploadId ) ) {
8991 logger . debug ( `Commit: Commit for upload ${ uploadId } already in progress. Ignoring...` )
@@ -109,7 +111,7 @@ async function initializeStorageDriver() {
109111 try {
110112 logger . debug ( 'Commit: Committing cache for id' , uploadId )
111113 await driver . upload ( buffer , cacheFileName )
112- await touchKey ( cacheKey . key , cacheKey . version )
114+ await updateOrCreateKey ( cacheKey . key , cacheKey . version )
113115 logger . debug ( 'Commit: Cache committed for id' , uploadId )
114116 } finally {
115117 cacheKeyByUploadId . delete ( uploadId )
@@ -118,11 +120,11 @@ async function initializeStorageDriver() {
118120 }
119121 } ,
120122 async download ( objectName ) {
121- logger . debug ( 'Download: Trying to download ' , objectName )
123+ logger . debug ( 'Download: Downloading ' , objectName )
122124 return driver . download ( objectName )
123125 } ,
124126 async uploadChunk ( uploadId , chunkStream , chunkStart ) {
125- logger . debug ( 'Upload: Trying to upload chunk for upload' , uploadId )
127+ logger . debug ( 'Upload: Uploading chunk for upload' , uploadId )
126128 const cacheKey = cacheKeyByUploadId . get ( uploadId )
127129 if ( ! cacheKey ) {
128130 logger . debug ( `Upload: No cache key found for upload ${ uploadId } . Ignoring...` )
@@ -148,9 +150,13 @@ async function initializeStorageDriver() {
148150 await chunkStream . pipeTo ( bufferWriteStream )
149151 logger . debug ( 'Upload: Chunks uploaded for id' , uploadId )
150152 } ,
151- async pruneCaches ( ) {
152- logger . debug ( 'Prune: Trying to prune caches' )
153- await driver . prune ( )
153+ async pruneCaches ( olderThanDays ) {
154+ logger . debug ( 'Prune: Pruning caches' )
155+
156+ const keys = await findStaleKeys ( olderThanDays )
157+ await driver . delete ( keys . map ( ( key ) => encodeCacheKey ( key . key , key . version ) ) )
158+ await pruneKeys ( keys )
159+
154160 logger . debug ( 'Prune: Caches pruned' )
155161 } ,
156162 }
0 commit comments