A Redis caching middleware for tRPC, providing easy-to-use caching capabilities for your tRPC procedures.
- Redis caching for tRPC procedures
- Support for both standard Redis and Upstash Redis
- Type-safe configuration
- User-specific and global caching strategies
- Customizable cache keys
- Built-in logging
- Easy cache invalidation
npm install trpc-redis-cache
pnpm add trpc-redis-cache
yarn add trpc-redis-cache
bun add trpc-redis-cacheimport { createCacheMiddleware } from 'trpc-redis-cache';
// Create a cache middleware with default options
const cacheMiddleware = createCacheMiddleware();
// Use it in your tRPC procedure
const appRouter = createTRPCRouter({
getUser: protectedProcedure
.input(z.object({ id: z.string() }))
.use(cacheMiddleware)
.query(async ({ input }) => {
// Expensive operation that will be cached
return await getUserById(input.id);
}),
});import { createCacheMiddleware, invalidateCache } from 'trpc-redis-cache';
// Global cache middleware (shared among all users)
const globalCacheMiddleware = createCacheMiddleware({
ttl: undefined, // Permanent cache
useUpstash: true,
globalCache: true,
userSpecific: false,
debug: true,
});
// User-specific cache with custom TTL
const userCacheMiddleware = createCacheMiddleware({
ttl: 3600, // 1 hour
useUpstash: true,
userSpecific: true,
debug: process.env.NODE_ENV === 'development',
});
// Invalidate cache for a specific procedure
await invalidateCache(
'getUser',
{ id: '123' },
{
useUpstash: true,
userId: '456',
},
);Will be added in future.
# Install dependencies
pnpm install
# Run tests
pnpm test
# Build the package
pnpm run buildMIT