From 4fa16e38c11689f58c874f092d3566dd36aa023c Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Fri, 17 Jan 2025 10:54:06 +0000 Subject: [PATCH] Start to hide cache internals from the client Refs #2186 --- src/CachingHttpClient.ts | 8 ++------ src/HttpCache.ts | 28 +++++++++++++++------------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/CachingHttpClient.ts b/src/CachingHttpClient.ts index b9649f8e8..82fe21158 100644 --- a/src/CachingHttpClient.ts +++ b/src/CachingHttpClient.ts @@ -37,9 +37,7 @@ export const CachingHttpClient: Effect.Effect< yield* pipe( req, httpClient.execute, - Effect.tap(response => - cache.set(key, { staleAt: DateTime.addDuration(timestamp, '10 seconds'), response }), - ), + Effect.tap(response => cache.set(response, DateTime.addDuration(timestamp, '10 seconds'))), Effect.scoped, ) }), @@ -59,9 +57,7 @@ export const CachingHttpClient: Effect.Effect< return yield* pipe( req, httpClient.execute, - Effect.tap(response => - pipe(cache.set(key, { staleAt: DateTime.addDuration(timestamp, '10 seconds'), response }), Effect.ignore), - ), + Effect.tap(response => pipe(cache.set(response, DateTime.addDuration(timestamp, '10 seconds')), Effect.ignore)), Effect.tap(response => Effect.gen(function* () { const cachedValue = yield* cache.get(key) diff --git a/src/HttpCache.ts b/src/HttpCache.ts index d0444a98a..1f8206941 100644 --- a/src/HttpCache.ts +++ b/src/HttpCache.ts @@ -1,16 +1,11 @@ -import { Headers, type HttpClientResponse } from '@effect/platform' -import { Context, type DateTime, Effect, Layer, pipe, Schema } from 'effect' +import { Headers, UrlParams, type HttpClientRequest, type HttpClientResponse } from '@effect/platform' +import { Context, Effect, Layer, pipe, Schema, type DateTime } from 'effect' interface CacheValue { staleAt: DateTime.DateTime response: StoredResponse } -interface CacheInput { - staleAt: DateTime.DateTime - response: HttpClientResponse.HttpClientResponse -} - export type CacheKey = URL type StoredResponse = typeof StoredResponseSchema.Encoded @@ -25,7 +20,7 @@ export class HttpCache extends Context.Tag('HttpCache')< HttpCache, { get: (key: CacheKey) => Effect.Effect - set: (key: CacheKey, value: CacheInput) => Effect.Effect + set: (response: HttpClientResponse.HttpClientResponse, staleAt: DateTime.DateTime) => Effect.Effect delete: (key: CacheKey) => Effect.Effect } >() {} @@ -34,20 +29,27 @@ export const layer = Layer.sync(HttpCache, () => { const cache = new Map() return { get: key => Effect.succeed(cache.get(key.href)), - set: (key, input) => + set: (response, staleAt) => pipe( Effect.gen(function* () { return { - status: input.response.status, - headers: input.response.headers, - body: yield* input.response.text, + status: response.status, + headers: response.headers, + body: yield* response.text, } }), Effect.andThen(Schema.encode(StoredResponseSchema)), Effect.andThen(storedResponse => { - cache.set(key.href, { staleAt: input.staleAt, response: storedResponse }) + cache.set(keyForRequest(response.request).href, { staleAt, response: storedResponse }) }), ), delete: key => Effect.succeed(cache.delete(key.href)), } }) + +const keyForRequest = (request: HttpClientRequest.HttpClientRequest): CacheKey => { + const url = new URL(request.url) + url.search = UrlParams.toString(request.urlParams) + + return url +}