Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,24 @@ extendedPrisma.user.update({
});
```

### Direct Cache Invalidation

Cache invalidation can also be performed directly using the `uncache` method, allowing you to uncache without needing to perform a database operation.

**Example of Direct Cache Invalidation:**

```javascript
// Invalidate cache when updating a user's information
extendedPrisma.uncache({
uncacheKeys: [
extendedPrisma.getKey({ params: [{ prisma: 'User' }, { id: userId }] }), // Specific key to invalidate
extendedPrisma.getKeyPattern({ params: [{ prisma: '*' }, { id: userId }]}), // Pattern for wildcard invalidation
extendedPrisma.getKeyPattern({ params: [{ prisma: 'Post' }, { id: userId }, { glob: '*' }]}), // Use glob for more complex patterns
],
hasPattern: true, // Use pattern matching for invalidation
});
```

**Explanation of Cache Invalidation:**

- **`uncacheKeys`**: Specifies the keys or patterns to be invalidated.
Expand Down
33 changes: 19 additions & 14 deletions src/cacheUncache.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import micromatch from 'micromatch';
import {coalesceAsync} from 'promise-coalesce';
import {type Redis} from 'iovalkey'

import type {Operation} from '@prisma/client/runtime/library';

Expand Down Expand Up @@ -212,27 +213,31 @@ export const customCacheAction = async ({
});
};

export const uncacheAction = async (redis: Redis, {uncacheKeys, hasPattern}: UncacheOptions) => {
if (hasPattern) {
const patternKeys = micromatch(uncacheKeys, ['*\\**', '*\\?*']);
const plainKeys = micromatch(uncacheKeys, ['*', '!*\\**', '!*\\?*']);

const unlinkPromises = [
...unlinkPatterns({
redis,
patterns: patternKeys,
}),
...(plainKeys.length ? [redis.unlink(plainKeys)] : []),
];

await Promise.all(unlinkPromises);
} else await redis.unlink(uncacheKeys);
}

export const customUncacheAction = async ({
redis,
options: {args, query},
config,
}: ActionParams) => {
const {uncacheKeys, hasPattern} = args.uncache as unknown as UncacheOptions;

if (hasPattern) {
const patternKeys = micromatch(uncacheKeys, ['*\\**', '*\\?*']);
const plainKeys = micromatch(uncacheKeys, ['*', '!*\\**', '!*\\?*']);

const unlinkPromises = [
...unlinkPatterns({
redis,
patterns: patternKeys,
}),
...(plainKeys.length ? [redis.unlink(plainKeys)] : []),
];

await Promise.all(unlinkPromises);
} else await redis.unlink(uncacheKeys);
await uncacheAction(redis, {uncacheKeys, hasPattern});

return {result: await query({...args, uncache: undefined})};
};
Expand Down
9 changes: 5 additions & 4 deletions src/prismaExtensionRedis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import {
isAutoCacheEnabled,
isCustomCacheEnabled,
isCustomUncacheEnabled,
uncacheAction
} from './cacheUncache';
import {getAutoKeyGen, getKeyGen, getKeyPatternGen} from './cacheKey';

import type {ExtendedModel, PrismaExtensionRedisOptions} from './types';
import type {ExtendedModel, PrismaExtensionRedisOptions, UncacheOptions} from './types';

export const PrismaExtensionRedis = (options: PrismaExtensionRedisOptions) => {
const {
Expand All @@ -27,6 +28,7 @@ export const PrismaExtensionRedis = (options: PrismaExtensionRedisOptions) => {
const getKey = getKeyGen(delimiter, cacheCase, prefix);
const getAutoKey = getAutoKeyGen(getKey);
const getKeyPattern = getKeyPatternGen(delimiter, cacheCase, prefix);
const uncache = (args: UncacheOptions) => uncacheAction(redis, args)

return Prisma.defineExtension({
name: 'prisma-extension-redis',
Expand All @@ -35,6 +37,7 @@ export const PrismaExtensionRedis = (options: PrismaExtensionRedisOptions) => {
getKey,
getKeyPattern,
getAutoKey,
uncache
},
model: {
$allModels: {} as ExtendedModel,
Expand Down Expand Up @@ -68,9 +71,7 @@ export const PrismaExtensionRedis = (options: PrismaExtensionRedisOptions) => {
config,
});

return {
result: await query({...args, cache: undefined}),
};
return await query({...args, cache: undefined})
},
},
},
Expand Down