Skip to content

Commit a5c99ab

Browse files
authored
fix: fix cache invalidation (#437)
1 parent e8bb79b commit a5c99ab

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

web/src/services/storage/kv.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
import { isAfter } from 'date-fns'
22
import 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

57
export 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

web/src/workers/language/language.worker.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)