Skip to content

Commit

Permalink
Start to hide cache internals from the client
Browse files Browse the repository at this point in the history
Refs #2186
  • Loading branch information
thewilkybarkid committed Jan 17, 2025
1 parent 23939fc commit 4fa16e3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
8 changes: 2 additions & 6 deletions src/CachingHttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}),
Expand All @@ -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)
Expand Down
28 changes: 15 additions & 13 deletions src/HttpCache.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -25,7 +20,7 @@ export class HttpCache extends Context.Tag('HttpCache')<
HttpCache,
{
get: (key: CacheKey) => Effect.Effect<CacheValue | undefined>
set: (key: CacheKey, value: CacheInput) => Effect.Effect<void, Error>
set: (response: HttpClientResponse.HttpClientResponse, staleAt: DateTime.DateTime) => Effect.Effect<void, Error>
delete: (key: CacheKey) => Effect.Effect<void>
}
>() {}
Expand All @@ -34,20 +29,27 @@ export const layer = Layer.sync(HttpCache, () => {
const cache = new Map<string, CacheValue>()
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
}

0 comments on commit 4fa16e3

Please sign in to comment.