Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions web/src/services/storage/kv.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import { isAfter } from 'date-fns'
import type { DatabaseStorage } from './db'
import type { CacheStorage } from './types'
import type { CacheEntry, CacheStorage } from './types'

type RecordValidator<T> = (entry: CacheEntry<T>) => boolean

export class KeyValueStore implements CacheStorage {
constructor(private readonly db: DatabaseStorage) {}

async getItem<T>(key: string): Promise<T | undefined> {
async getItem<T>(key: string, validate?: RecordValidator<T>): Promise<T | undefined> {
const entry = await this.db.keyValue.get(key)
if (entry?.expireAt && isAfter(new Date(), entry.expireAt)) {
void this.deleteItem(key)
return undefined
}

if (entry && validate && !validate(entry)) {
void this.deleteItem(key)
return undefined
}

return entry?.value as T | undefined
}

Expand Down
6 changes: 5 additions & 1 deletion web/src/workers/language/language.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,11 @@ export class WorkerHandler {
}

// TODO: add invalidation by Go version
const version = await this.keyValue.getItem<string>(completionVersionKey)
const version = await this.keyValue.getItem<string>(completionVersionKey, (entry) => {
// v2.2.0 didn't write TTL by mistake
return typeof entry.expireAt !== 'undefined'
})

if (!version) {
await this.populateCache()
return true
Expand Down