Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

Invalidating cache with update commands #29

Closed
filiptronicek opened this issue Nov 13, 2021 · 4 comments
Closed

Invalidating cache with update commands #29

filiptronicek opened this issue Nov 13, 2021 · 4 comments
Assignees
Labels
help wanted Extra attention is needed

Comments

@filiptronicek
Copy link

If the database gets updated, the associated cache keys should be invalidated (either by updating or deleting).

@Asjas
Copy link
Owner

Asjas commented Feb 11, 2022

I added Basic Cache Invalidation in version 1.1.0.

I'm basically searching the Redis cache for all keys that matches a model when a Prisma mutation action happens. I then delete all those keys from Redis.

model in this case would be User, Post or similar.

const keys = await redis.keys(`${model}:*`);
const deletedKeys = await redis.del(keys);

So if create or updateMany is called on the User model, all cached keys that refer to the User model will be deleted.

@Asjas Asjas pinned this issue Feb 11, 2022
@Asjas Asjas added the help wanted Extra attention is needed label Feb 11, 2022
@Asjas
Copy link
Owner

Asjas commented Feb 11, 2022

I know most Prisma actions (queries and mutations) usually include an id field in the args or next return value.

I don't know if this is true for all actions or all databases that are supported by Prisma, but that could be something that would allow me to create a more refined cache where only cached queries that match a model AND that match an id field can be deleted.

I'm curious what other people's opinion on this would be?

@Asjas Asjas unpinned this issue Mar 14, 2022
@f-o-w-l
Copy link

f-o-w-l commented Mar 15, 2022

I know most Prisma actions (queries and mutations) usually include an id field in the args or next return value.

I don't know if this is true for all actions or all databases that are supported by Prisma, but that could be something that would allow me to create a more refined cache where only cached queries that match a model AND that match an id field can be deleted.

I'm curious what other people's opinion on this would be?

It makes sense for me to call it a primary key for each model. Models are basically tables, right? If so, then there should only be one primary key per model. Disclaimer, I haven't used Prisma, I'm currently learning it. Referencing #62, I think it would be great to have this kind of API:

prismaClient.$use(
  createPrismaRedisCache({
    models: [
      {
        model: "User",
        primaryKey: "id",
      },
      {
        model: "Post",
        cacheTime: 900,
        primaryKey: "postId",
        excludeCacheMethods: ["findMany", "aggregate"],
      }
    ],
    defaultCacheTime: 300,
    redis,
    defaultExcludeCacheMethods: ["findMany"],
  }),
);

The docs could mention that if a primaryKey isn't specified, then the full model is deleted from cache after any mutation, otherwise the mutation will only delete queries that match the model and primaryKey. I think it makes sense to fully flush the model cache by default instead of checking for the "id" property specifically by default. I like the idea of default fallbacks not just for the cacheTime, I can't think of any downside to allowing excludeCacheMethods to be specified per model. What do you think?

@Asjas
Copy link
Owner

Asjas commented Mar 28, 2022

v2.0.0 has been released.

@Asjas Asjas closed this as completed Mar 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants