Skip to content

ariburaco/trpc-redis-cache

Repository files navigation

trpc-redis-cache

A Redis caching middleware for tRPC, providing easy-to-use caching capabilities for your tRPC procedures.

Features

  • 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

Installation

npm install trpc-redis-cache

pnpm add trpc-redis-cache

yarn add trpc-redis-cache

bun add trpc-redis-cache

Basic Usage

import { 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);
    }),
});

Advanced Usage

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',
  },
);

Examples

Will be added in future.

Development

# Install dependencies
pnpm install

# Run tests
pnpm test

# Build the package
pnpm run build

License

MIT

About

No description, website, or topics provided.

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •