Effect CommunityEC
Effect Community10mo ago
141 replies
Titouan Créac'h

Using a custom runtime with `toWebHandler` and sharing a cache across requests can be a bit trick...

How can I use a custom Runtime when using toWebHandler, and is memoMap can help me here ?

Here is my code (my attempt)

const makeHandler = (authSession: Session | null) =>
  RpcServer.toWebHandler(AuthRpcGroup, {
    layer: Layer.mergeAll(
      AuthRpcLive.pipe(
        Layer.provide(
          Layer.provideMerge(UserSessionService.layer(authSession), MainLive),
        ),
      ),
      RpcSerialization.layerJson,
      HttpServer.layerContext,
    ),
    memoMap: ServerRuntime.memoMap,
  });

export const POST = async (request: NextRequest) => {
  const handler = makeHandler(await getServerSession(authOptions));
  return await handler.handler(request);
};


In MainLive, I have a cache that I want to share between request. Before the new rpc, I was using ServerRuntime.runPromise (server runtime is just a managed runtime) and the cache was shared.

With the new version, I tried to pass ServerRuntime.memoMap as to achieve the same goal, but it looks like the cache is recreated every request
Was this page helpful?