File tree Expand file tree Collapse file tree 2 files changed +14
-3
lines changed Expand file tree Collapse file tree 2 files changed +14
-3
lines changed Original file line number Diff line number Diff line change 11import { isAfter } from 'date-fns'
22import type { DatabaseStorage } from './db'
3- import type { CacheStorage } from './types'
3+ import type { CacheEntry , CacheStorage } from './types'
4+
5+ type RecordValidator < T > = ( entry : CacheEntry < T > ) => boolean
46
57export class KeyValueStore implements CacheStorage {
68 constructor ( private readonly db : DatabaseStorage ) { }
79
8- async getItem < T > ( key : string ) : Promise < T | undefined > {
10+ async getItem < T > ( key : string , validate ?: RecordValidator < T > ) : Promise < T | undefined > {
911 const entry = await this . db . keyValue . get ( key )
1012 if ( entry ?. expireAt && isAfter ( new Date ( ) , entry . expireAt ) ) {
1113 void this . deleteItem ( key )
1214 return undefined
1315 }
1416
17+ if ( entry && validate && ! validate ( entry ) ) {
18+ void this . deleteItem ( key )
19+ return undefined
20+ }
21+
1522 return entry ?. value as T | undefined
1623 }
1724
Original file line number Diff line number Diff line change @@ -158,7 +158,11 @@ export class WorkerHandler {
158158 }
159159
160160 // TODO: add invalidation by Go version
161- const version = await this . keyValue . getItem < string > ( completionVersionKey )
161+ const version = await this . keyValue . getItem < string > ( completionVersionKey , ( entry ) => {
162+ // v2.2.0 didn't write TTL by mistake
163+ return typeof entry . expireAt !== 'undefined'
164+ } )
165+
162166 if ( ! version ) {
163167 await this . populateCache ( )
164168 return true
You can’t perform that action at this time.
0 commit comments