Registry / database / trpc-redis-cache

trpc-redis-cache

JSON →
library0.4.0jsnpmunverified

A Redis caching middleware for tRPC (v0.4.0) that provides type-safe caching for tRPC procedures with support for standard Redis and Upstash Redis. Released with no fixed cadence, this library offers both global and user-specific caching strategies, custom TTL, built-in logging, and cache invalidation. Differentiators include tight integration with tRPC v11, TypeScript-first design, and minimal configuration overhead compared to generic Redis caching solutions.

npm install trpc-redis-cache
INSTALL
IMPORT
SIG · TRPC-REDIS-CACHE
T
trpc-redis-cache
databasejavascriptv0.4.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

createCacheMiddleware
import { createCacheMiddleware } from 'trpc-redis-cache'
const createCacheMiddleware = require('trpc-redis-cache')
ESM-only; CommonJS require will not work. Use import syntax.
invalidateCache
import { invalidateCache } from 'trpc-redis-cache'
import { invalidateCache } from 'trpc-redis-cache/dist'
Direct path imports (e.g., dist) break type resolution; always use package root.
CacheConfig
import type { CacheConfig } from 'trpc-redis-cache'
import { CacheConfig } from 'trpc-redis-cache'
CacheConfig is a TypeScript type; use 'import type' to avoid runtime import errors (requires TypeScript 3.8+).

Shows how to create a cache middleware with a 60-second TTL, apply it to a tRPC query, and invalidate the cache for a specific procedure and user.

import { createCacheMiddleware, invalidateCache } from 'trpc-redis-cache'; import { initTRPC } from '@trpc/server'; import { z } from 'zod'; const t = initTRPC.create(); const router = t.router; const procedure = t.procedure; const cacheMiddleware = createCacheMiddleware({ ttl: 60, useUpstash: false }); const appRouter = router({ getUser: procedure .input(z.object({ id: z.string() })) .use(cacheMiddleware) .query(async ({ input }) => { // Simulate expensive operation return { id: input.id, name: 'Alice' }; }), }); // Expose router... // To invalidate cache: await invalidateCache('getUser', { id: '123' }, { userId: '456' });
Debug
Known issues
breakingPackage requires @trpc/server ^11.7.1; not compatible with tRPC v10 or earlier.
fix
Ensure your project uses tRPC v11.7.1 or later.
affects: >=0.0.0
gotchaWhen useUpstash is true, you must have UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN environment variables set, or pass configuration explicitly. Missing these will cause silent failures at runtime.
fix
Set environment variables or pass options explicitly: createCacheMiddleware({ useUpstash: true, url: '...', token: '...' })
affects: >=0.0.0
gotchaCache invalidation via invalidateCache requires the same Redis configuration as the middleware (e.g., useUpstash). Mismatched configuration will result in invalidation not working.
fix
Always pass identical options to createCacheMiddleware and invalidateCache.
affects: >=0.0.0
deprecatedThe 'debug' option does not exist in current types but is used in README examples; may be removed in future versions. Rely on standard logging or omit.
fix
Remove 'debug' option from configuration; use console.log or a logging library instead.
affects: >=0.4.0
Errors
Common errors & fixes
Error: Cannot find module 'trpc-redis-cache'
Package not installed or not in node_modules.
fix
Run 'npm install trpc-redis-cache' and ensure package.json includes it.
TypeError: createCacheMiddleware is not a function
Using CommonJS require instead of ESM import, or import path incorrect.
fix
Use 'import { createCacheMiddleware } from 'trpc-redis-cache'' and ensure package.json has 'type': 'module'.
ERR_REQUIRE_ESM: require() of ES Module ...
Trying to use require() on an ESM-only package.
fix
Switch to ESM syntax: 'import { createCacheMiddleware } from 'trpc-redis-cache''. If you need CJS, use dynamic import().
Upgrade
Version history
0.4.0latest on npm
Audit
Dependencies
@trpc/serverrequiredpeer dependency required for tRPC integration
redisoptionalrequired for Redis client (if not using Upstash)
Agent activity
25 hits · last 30 days
node
20
Meta
2
OpenAI (training)
1
Resources
trpc-redis-cache — npm install trpc-redis-cache · libregistry