Caching in redis with

This is the code:
const getUploadUrl = Effect.fn("files.get-upload-url")(function* (input: {
  hashedKey: string
  visibility: "private" | "public"
  duration?: Duration.Duration
}) {
  if (input.visibility === "public") {
    return `${cdnUrl}/uploads/${input.hashedKey}`
  }

  const storage = yield* (yield* Persistence.ResultPersistence).make({
    storeId: "files",
    timeToLive: (_, exit) => (Exit.isSuccess(exit) ? (input.duration ?? Duration.hours(2)) : Duration.seconds(0)),
  })

  const cachedToken = yield* storage.get(input.hashedKey)
  if (Option.isSome(cachedToken)) {
    return `${cdnUrl}/uploads/${input.hashedKey}?token=${cachedToken.value}`
  }

  const token = yield* jwt.sign({
    payload: {
      type: "file_access",
      fileHashedKey: input.hashedKey,
    },
    exp: DateTime.unsafeNow().pipe(DateTime.addDuration(input.duration ?? Duration.hours(2))),
    iss: "api.example.com",
    aud: "cdn.exmaple.com",
  })

  const url = `${cdnUrl}/uploads/${input.hashedKey}?token=${token}`
  yield* storage.set(input.hashedKey, token)

  return url
})
Was this page helpful?