Skip to content

Commit

Permalink
Merge pull request #1155 from serlo/redis-set-ttl
Browse files Browse the repository at this point in the history
feat(cache): Set always a ttl for each key
  • Loading branch information
AndreasHuber authored Nov 30, 2023
2 parents d19ae2b + 3308738 commit a2d9009
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
4 changes: 1 addition & 3 deletions packages/server/src/internals/cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,7 @@ 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)
}
await client.expire(key, ttlInSeconds ?? 60 * 60 * 24 * 3)
} catch (e) {
log.error(`Failed to set key "${key}":`, e)
throw e
Expand Down
6 changes: 5 additions & 1 deletion packages/server/src/internals/data-source-helper/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as R from 'ramda'
import { InvalidCurrentValueError } from './common'
import { FunctionOrValue } from '../cache'
import { Environment } from '../environment'
import { Time } from '../swr-queue'
import { Time, timeToSeconds } from '../swr-queue'

/**
* Helper function to create a query in a data source. A query operation is a
Expand All @@ -16,6 +16,8 @@ export function createQuery<P, R>(
spec: QuerySpec<P, R>,
environment: Environment,
): Query<P, R> {
const ttlInSeconds = spec.maxAge ? timeToSeconds(spec.maxAge) : undefined

async function queryWithDecoder<S extends R>(
payload: P,
customDecoder?: t.Type<S, unknown>,
Expand Down Expand Up @@ -53,6 +55,7 @@ export function createQuery<P, R>(
key,
value: decoded.right,
source: 'API: From a call to a data source',
ttlInSeconds,
})

return value as S
Expand Down Expand Up @@ -89,6 +92,7 @@ export function createQuery<P, R>(
environment.cache.set({
key: spec.getKey(payload),
...args,
ttlInSeconds,
source: 'API: Cache update function after a mutation',
}),
),
Expand Down
9 changes: 7 additions & 2 deletions packages/server/src/internals/swr-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ export function createSwrQueueWorker({

await cache.set({
key,
ttlInSeconds: spec.maxAge ? timeToSeconds(spec.maxAge) : undefined,
source: 'SWR worker',
priority: Priority.Low,
getValue: async (current) => {
Expand Down Expand Up @@ -313,7 +314,7 @@ export interface Time {
seconds?: number
}

export function timeToMilliseconds({
export function timeToSeconds({
day = 0,
days = 0,
hour = 0,
Expand All @@ -323,7 +324,7 @@ export function timeToMilliseconds({
second = 0,
seconds = 0,
}: Time) {
const SECOND = 1000
const SECOND = 1
const MINUTE = 60 * SECOND
const HOUR = 60 * MINUTE
const DAY = 24 * HOUR
Expand All @@ -336,6 +337,10 @@ export function timeToMilliseconds({
)
}

export function timeToMilliseconds(time: Time) {
return timeToSeconds(time) * 1000
}

function reportError({
error,
jobStatus,
Expand Down

0 comments on commit a2d9009

Please sign in to comment.