Skip to content

Commit

Permalink
fix: fix cache invalidation (#437)
Browse files Browse the repository at this point in the history
  • Loading branch information
x1unix authored Oct 6, 2024
1 parent e8bb79b commit a5c99ab
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
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

0 comments on commit a5c99ab

Please sign in to comment.