Effect CommunityEC
Effect Community5mo ago
5 replies
Stephen Bluck

First cached RequestResolver hit is not caching for the first request

I am playing with Request resolvers with this code:

interface Identify extends Request.Request<Session.Known, AccessToken.InvalidToken> {
  readonly _tag: "UserIdentify"
  readonly token: Session.Known.Token
}

const Identify = Request.tagged<Identify>("UserIdentify")

const RequestCache = Layer.setRequestCache(
  Request.makeCache({
    capacity: 1,
    timeToLive: Duration.infinity
  })
)

export const withCache = (users: Users): Effect.Effect<Users> =>
  Effect.gen(function* () {
    const resolver = RequestResolver.fromEffect(({ token }: Identify) => users.identify(token))

    return {
      ...users,
      identify: (token: Session.Known.Token) => {
        return Effect.request(Identify({ token }), resolver).pipe(Effect.withRequestCaching(true))
      }
    }
  }).pipe(Effect.provide(RequestCache))


This withCache is built at request runtime. I hit my endpoint once where this identify function gets invoked multiple times and I see that it actually gets executed many times as if there is no cache. The second time I hit it, I get one execution which is basically a cache hit. After a little bit of arbitrary time the same thing happens again where an initial request executes many identify's but the following hits are cached.

Here are some logs I am seeing when doing the same idempotent request over and over:
First request:
INFO (#50): User identified
INFO (#29): User identified

Second request:
INFO (#52): User identified

Third request:
INFO (#52): User identified

A fourth request after some time...
INFO (#50): User identified
INFO (#29): User identified

So as you can see the first request has no caching and neither does the last one but am not sure why or what could cause this? I am sure I have it all set up correctly following the docs.
Was this page helpful?