Skip to content

Commit

Permalink
fix(docs): update README to reflect changes in cache key generation f…
Browse files Browse the repository at this point in the history
…unctions for clarity

fix(example): correct operation type in auto cache key generation from 'count' to 'findUnique' for accurate usage
fix(cacheUncache.ts): ensure query results do not contain `isCached` key to prevent conflicts
refactor(types.ts): remove unused CacheKeyType definition to clean up type definitions
  • Loading branch information
yxx4c committed Nov 24, 2024
1 parent 78acfa7 commit efa56b0
Showing 5 changed files with 18 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -202,8 +202,8 @@ extendedPrisma.user.update({
uncache: {
uncacheKeys: [
extendedPrisma.getKey({ params: [{ prisma: 'User' }, { id: userId }] }), // Specific key to invalidate
getKeyPattern({ params: [{ prisma: '*' }, { id: userId }]}), // Pattern for wildcard invalidation
getKeyPattern({ params: [{ prisma: 'Post' }, { id: userId }, { glob: '*' }]}), // Use glob for more complex patterns
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
},
7 changes: 5 additions & 2 deletions example/index.ts
Original file line number Diff line number Diff line change
@@ -47,7 +47,7 @@ const config: CacheConfig = {
},
onHit: (key: string) => console.log(`FOUND CACHE: ${key}`),
onMiss: (key: string) => console.log(`NOT FOUND CACHE: ${key}`),
type: 'JSON',
type: 'JSON', // the redis instance must support JSON module if you chose to use JSON type cache
cacheKey: {
case: CacheCase.SNAKE_CASE,
delimiter: '*',
@@ -188,14 +188,17 @@ const main = async () => {

const args = {where: {email: userOne.email}};

// below example uses auto cache key generation function to fetch the results of the auto cache query (above)
// similarly, this can be used to uncache an auto cached query during any mutation (this does not support patterns)
await extendedPrisma.user
.findUnique({
...args,
cache: {
// make sure to use the correct args, model and operation here as it is not being validated
key: extendedPrisma.getAutoKey({
args,
model: 'user',
operation: 'count',
operation: 'findUnique',
}),
},
})
1 change: 1 addition & 0 deletions example/prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -18,4 +18,5 @@ model User {
name String
email String @unique
createdAt DateTime @default(now())
// isCached Boolean @default(true)
}
12 changes: 10 additions & 2 deletions src/cacheUncache.ts
Original file line number Diff line number Diff line change
@@ -146,10 +146,18 @@ export const getCache = async ({
}
} else if (onMiss) onMiss(key);

const result = await query(args);
const result = (await query(args)) as {isCached?: never};

if (result.isCached)
throw new Error(
'Query result must not contain keyword `isCached` as a key!',
);

const cacheContext = {
result,
result: {
...result,
isCached: true,
},
ttl: ttl * 1000 + timestamp,
stale: (ttl + stale) * 1000 + timestamp,
};
1 change: 0 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -245,7 +245,6 @@ export interface CacheDefinitionOptions {
}

export type CacheType = 'JSON' | 'STRING';
export type CacheKeyType = 'INBUILT' | 'CUSTOM';

export type CacheKey = {
/**

0 comments on commit efa56b0

Please sign in to comment.