The prisma-cache-extension
is a robust package designed to optimize database performance by improving access times,
streamlining cache management, and offering flexible tools for efficient maintenance.
- Redis
npm install prisma-cache-extension
yarn add prisma-cache-extension
import {PrismaClient} from '@prisma/client';
import {Redis} from 'ioredis';
import {
PrismaCacheExtension,
} from 'prisma-cache-extension';
// Create a Redis client
const redis = new Redis({
host: env.REDIS_HOST_NAME, // Specify Redis host name
port: env.REDIS_PORT, // Specify Redis port
});
This configuration is required for enabling caching.
const cache: CacheConfig = {
storage: {
type: 'redis',
options: {
client: redis,
invalidation: {
referencesTTL: 15
}, // Invalidation settings
// Logger for cache events
log: console,
},
},
};
// Create a Prisma client instance
const prisma = new PrismaClient();
return prisma.$extends(
PrismaExtensionRedis(30, {
cache, redis
}));
// Example: Query a user and cache the result when caching is enabled
prisma.user.findUnique({
where,
cache: {
key: JSON.stringify(where)
}
});
// Disable caching for this operation, if not mentioned
extendedPrisma.user.findFirst({
where: {userId: id},
});
// Example: Update a user and invalidate related cache keys
extendedPrisma.user.update({
where: {id},
data: {username},
uncache: {
uncacheKey: "*",
models: ["user", "userprofile"],
hasPattern: true, // Use wildcard pattern for key matching
},
});
ioredis
micromatch
async-cache-dedupe
Effortless Query Caching: Seamlessly cache your Prisma query results in Redis with minimal setup required.
Targeted Cache Invalidation: Easily invalidate specific Prisma queries to maintain accurate and up-to-date data.
Granular Control: Adjust caching and invalidation settings for each query, giving you precise control over caching behavior.
Dynamic Invalidation Strategies: Apply effective cache invalidation strategies to keep your cached data current.
Versatile Maintenance: Leverage general-purpose functions to efficiently manage Redis or Dragonfly databases.
Benefits of prisma-cache-extension
:
- Unified Dependencies: Replace multiple packages with a single
prisma-cache-extension
dependency, simplifying your setup. - Streamlined Maintenance: Enjoy centralized updates and improvements, making maintenance more straightforward.
- Optimized Codebase: Reduce redundancy and enhance performance by consolidating your codebase.
- Community Engagement: Connect with the
prisma-cache-extension
community for support and collaborative development.
Upgrade to prisma-cache-extension
today for a more efficient and integrated Redis caching solution. If this proves
helpful, consider giving it a star! ⭐ Star Me!