export const guildProcedure = protectedProcedure .input(z.object({ guildId: z.string() })) .use(async ({ ctx, next, input }) => { const hasPermissions = await someExpensiveOperationToCheckPermissions(input.guildId) if(!hasPermissions) { throw new TRPCError({ code: 'FORBIDDEN', message: 'You do not have permission to access this guild' }); } return next({ ctx: { session: { ...ctx.session, user: ctx.session.user }, }, }); });
export const guildProcedure = protectedProcedure .input(z.object({ guildId: z.string() })) .use(async ({ ctx, next, input }) => { const hasPermissions = await someExpensiveOperationToCheckPermissions(input.guildId) if(!hasPermissions) { throw new TRPCError({ code: 'FORBIDDEN', message: 'You do not have permission to access this guild' }); } return next({ ctx: { session: { ...ctx.session, user: ctx.session.user }, }, }); });
Is there some way of telling TRPC to cache the response from the procedure for all batched requests so wont do the same permission check, even though it has already been done?
As it currently stands, this expensive procedure will run 5 times, one for each api request that is prefetched.
Yes, I could cache this in redis (which I do) but if there is no cache to quickly see if they have permissions each query will have to run the expensive function and it could make the page take up to 30 seconds to load.
I'm thinking I could use some sort of redis lock, but it still seems a little inefficient having 5 queries that run at he same time (same trpc batch) all getting the same data from redis?