Skip to content

Commit

Permalink
feat: cache Go used versions (#441)
Browse files Browse the repository at this point in the history
  • Loading branch information
x1unix authored Oct 23, 2024
1 parent 929fcf1 commit 084751f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Please refer to [CONTRIBUTING.md](CONTRIBUTING.md) and [HACKING.md](HACKING.md)

### Sponsoring

<a href="https://gno.land/?from=goplay-tools" target="_blank">
<a href="https://gno.land/?utm_source=sponsor&utm_medium=goplay&utm_campaign=DevAcquisition+&utm_content=Devtool" target="_blank">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="./docs/img/sponsors/gnoland-dark.svg" width="480">
<img alt="Gno.land logo" width="480" src="./docs/img/sponsors/gnoland-light.svg">
Expand Down
12 changes: 10 additions & 2 deletions web/src/components/layout/Header/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react'
import { addDays } from 'date-fns'
import { CommandBar, type ICommandBarItemProps, Stack } from '@fluentui/react'

import type { Snippet } from '~/services/examples'
Expand All @@ -11,6 +12,7 @@ import { ExamplesModal } from '~/components/features/examples/ExamplesModal'
import { RunTargetSelector } from '~/components/elements/inputs/RunTargetSelector'
import { SharePopup } from '~/components/utils/SharePopup'

import { keyValue } from '~/services/storage'
import { dispatchTerminalSettingsChange } from '~/store/terminal'
import {
dispatchFormatFile,
Expand All @@ -35,6 +37,12 @@ import './Header.css'
*/
const BTN_SHARE_CLASSNAME = 'Header__btn--share'

const goVersionsCacheEntry = {
key: 'api.go.versions',
ttl: () => addDays(new Date(), 7),
getInitialValue: async () => await apiClient.getBackendVersions(),
}

interface HeaderState {
showSettings?: boolean
showAbout?: boolean
Expand Down Expand Up @@ -67,8 +75,8 @@ class HeaderContainer extends ThemeableComponent<Props, HeaderState> {
}

componentDidMount(): void {
apiClient
.getBackendVersions()
keyValue
.getOrInsert(goVersionsCacheEntry)
.then((rsp) => {
this.setState({
goVersions: rsp,
Expand Down
29 changes: 29 additions & 0 deletions web/src/services/storage/kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ import type { CacheEntry, CacheStorage } from './types'

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

export interface CachedValueDescriptor<T> {
key: string
ttl: () => Date
validate?: (entry: CacheEntry<T>) => boolean
getInitialValue: () => Promise<T>
}

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

Expand All @@ -22,6 +29,28 @@ export class KeyValueStore implements CacheStorage {
return entry?.value as T | undefined
}

async getOrInsert<T>({ ttl, key, validate, getInitialValue }: CachedValueDescriptor<any>) {
let cachedVal: T | undefined
try {
cachedVal = await this.getItem<T>(key, validate)
} catch (err) {
console.error(`Failed to get cached record "${key}": ${err}, falling back to default value.`)
}

if (typeof cachedVal !== 'undefined') {
return cachedVal
}

const initVal = await getInitialValue()
try {
await this.setItem(key, initVal, ttl())
} catch (err) {
console.error(`Failed to save cached record "${key}": ${err}`)
}

return initVal
}

async deleteItem(key: string) {
const n = await this.db.keyValue.where({ key }).delete()
return n > 0
Expand Down

0 comments on commit 084751f

Please sign in to comment.