Skip to content

Commit

Permalink
feat(server): server info lookup cache (#3808)
Browse files Browse the repository at this point in the history
  • Loading branch information
gjedlicska authored Jan 14, 2025
1 parent 37ede3b commit 9636a56
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
18 changes: 16 additions & 2 deletions packages/server/modules/core/graph/resolvers/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,18 @@ import {
getServerInfoFactory,
updateServerInfoFactory,
getPublicRolesFactory,
getPublicScopesFactory
getPublicScopesFactory,
getServerInfoFromCacheFactory,
storeServerInfoInCacheFactory
} from '@/modules/core/repositories/server'
import { db } from '@/db/knex'
import { Resolvers } from '@/modules/core/graph/generated/graphql'
import { LRUCache } from 'lru-cache'
import { ServerInfo } from '@/modules/core/helpers/types'

const cache = new LRUCache<string, ServerInfo>({ max: 1, ttl: 60 * 1000 })
const getServerInfoFromCache = getServerInfoFromCacheFactory({ cache })
const storeServerInfoInCache = storeServerInfoInCacheFactory({ cache })
const getServerInfo = getServerInfoFactory({ db })
const updateServerInfo = updateServerInfoFactory({ db })
const getPublicRoles = getPublicRolesFactory({ db })
Expand All @@ -22,7 +29,11 @@ const getPublicScopes = getPublicScopesFactory({ db })
export = {
Query: {
async serverInfo() {
return await getServerInfo()
const cachedServerInfo = getServerInfoFromCache()
if (cachedServerInfo) return cachedServerInfo
const serverInfo = await getServerInfo()
storeServerInfoInCache({ serverInfo })
return serverInfo
}
},
ServerInfo: {
Expand Down Expand Up @@ -58,6 +69,9 @@ export = {

const update = removeNullOrUndefinedKeys(args.info)
await updateServerInfo(update)
// we're currently going to ignore, that this should be propagated to all
// backend instances, and going to rely on the TTL in the cache to propagate the changes
cache.clear()
return true
},
serverInfoMutations: () => ({})
Expand Down
16 changes: 16 additions & 0 deletions packages/server/modules/core/repositories/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
getServerVersion
} from '@/modules/shared/helpers/envHelper'
import { Knex } from 'knex'
import { LRUCache } from 'lru-cache'

const ServerConfig = buildTableHelper('server_config', [
'id',
Expand All @@ -38,6 +39,21 @@ const tables = {
scopes: (db: Knex) => db<ScopeRecord>(Scopes.name)
}

const SERVER_CONFIG_CACHE_KEY = 'server_config'

export const getServerInfoFromCacheFactory =
({ cache }: { cache: LRUCache<string, ServerInfo> }) =>
() => {
const serverInfo = cache.get(SERVER_CONFIG_CACHE_KEY)
return serverInfo ?? null
}

export const storeServerInfoInCacheFactory =
({ cache }: { cache: LRUCache<string, ServerInfo> }) =>
({ serverInfo }: { serverInfo: ServerInfo }) => {
cache.set(SERVER_CONFIG_CACHE_KEY, serverInfo)
}

export const getServerInfoFactory =
(deps: { db: Knex }): GetServerInfo =>
async () => {
Expand Down

0 comments on commit 9636a56

Please sign in to comment.