Skip to content

Commit

Permalink
Merge pull request #1111 from serlo/feat/enmeshed-cache
Browse files Browse the repository at this point in the history
feat(enmeshed): Set cache ttl to 20min
  • Loading branch information
kulla authored Nov 11, 2023
2 parents 666699b + 90e27c1 commit b596fe3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/server/src/internals/cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface Cache {
payload: {
key: string
source: string
ttlInSeconds?: number
priority?: Priority
} & FunctionOrValue<T>,
): Promise<void>
Expand Down Expand Up @@ -62,9 +63,10 @@ export function createCache({ timer }: { timer: Timer }): Cache {
key: string
source: string
priority?: Priority
ttlInSeconds?: number
} & FunctionOrValue<T>,
) {
const { key, priority = Priority.High, source } = payload
const { key, priority = Priority.High, source, ttlInSeconds } = payload
const lockManager = lockManagers[priority]

const lock = await lockManager.lock(key)
Expand Down Expand Up @@ -92,6 +94,10 @@ export function createCache({ timer }: { timer: Timer }): Cache {
}
const packedValue = msgpack.encode(valueWithTimestamp)
await client.set(key, packedValue)

if (ttlInSeconds != null) {
await client.expire(key, ttlInSeconds)
}
} catch (e) {
log.error(`Failed to set key "${key}":`, e)
throw e
Expand Down
2 changes: 2 additions & 0 deletions packages/server/src/internals/server/enmeshed-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -657,12 +657,14 @@ async function setSession(
await cache.set({
key: getSessionKey(sessionId),
value: session,
ttlInSeconds: 20 * 60,
source: 'enmeshed-middleware',
})
if (session.enmeshedId) {
await cache.set({
key: getIdentityKey(session.enmeshedId),
value: sessionId,
ttlInSeconds: 20 * 60,
source: 'enmeshed-middleware',
})
}
Expand Down

0 comments on commit b596fe3

Please sign in to comment.