Skip to content

Commit

Permalink
Implement cache get method for Redis implementation
Browse files Browse the repository at this point in the history
Refs #2186
  • Loading branch information
thewilkybarkid committed Jan 23, 2025
1 parent 7006417 commit 6b80203
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/HttpCache.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Headers, HttpClientResponse, UrlParams, type HttpClientRequest } from '@effect/platform'
import { Context, Effect, Layer, Option, pipe, Schema, type Cause, type DateTime } from 'effect'
import { Cause, Context, Effect, Layer, Option, pipe, Schema, type DateTime } from 'effect'
import * as Redis from './Redis.js'

interface CacheValue {
Expand Down Expand Up @@ -44,7 +44,23 @@ export const layerPersistedToRedis = Layer.effect(
const redis = yield* Redis.HttpCacheRedis

return {
get: () => Option.none(),
get: request =>
pipe(
Effect.tryPromise(() => redis.get(keyForRequest(request))),
Effect.andThen(Option.fromNullable),
Effect.andThen(Schema.decode(CacheValueFromStringSchema)),
Effect.map(({ staleAt, response }) => ({
staleAt,
response: HttpClientResponse.fromWeb(
request,
new Response(response.body, {
status: response.status,
headers: Headers.fromInput(response.headers),
}),
),
})),
Effect.mapError(() => new Cause.NoSuchElementException()),
),
set: (response, staleAt) =>
pipe(
Effect.gen(function* () {
Expand Down

0 comments on commit 6b80203

Please sign in to comment.