Repository navigation
Open
Labels
Description
It would be nice if queries would get out of the box support for caching, it's a pain to make a wrapper since you'd have to make sure the function you create accepts and returns the right types. The syntax could be something like:
const users = await client.user.find({
select: { x: true },
where: { id },
cache: true // defaults to y ms
})
// Custom time
const users = await client.user.find({
select: { x: true },
where: { id },
cache: 60000 // cache for 1 minute / Maybe an options object for custom cache storage keys?
})The cache could default to memory and a custom solution could be added in schema.prisma
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
datasource cache {
provider = "redis"
url = env("CACHE_URL")
}Any thoughts? I just read about the middleware API which maybe could be used under the hood to support this feature, manually caching there would cause duplicate code
Reacted by Till Friebe, Michael Li, Anand Chowdhary, Zakarya Mamouni, Kalai Selvan Kadirvel, Thierry, Zac Denham, Akshay, Tobias, chuckstock and 73 more
Reacted by André Ribeiro, Casper Iversen, ojczeo, Batuhan Göksu, Dan Wetherald, Tyler Bell, Michael Czolko, alexbab90, Leonardo E. Dominguez, Rishabh Rao and 11 more
Reacted by André Ribeiro, ojczeo, Batuhan Göksu, Dan Wetherald, Tyler Bell, alexbab90, Leonardo E. Dominguez, Rishabh Rao, Pavitra Golchha, A-J Roos and 4 more
Reacted by Dan Wetherald, Tyler Bell, alexbab90, Rishabh Rao, A-J Roos, Okan YILDIRIM and Hadi
Activity
divyenduz commentedon Jul 29, 2020
Related discussion #3164 (reply in thread)
AnandChowdhary commentedon Nov 8, 2020
I'd also love to see an easy way to invalidate the cache, if this is implemented. For example:
Then, if you update a user, for example:
This can probably even happen under the hood without explicit keys, because Prisma knows when
user.updateoruser.deleteare called, it can automatically invalidate any cache foruser.find, etc. 🧠daniellwdb commentedon Nov 8, 2020
@AnandChowdhary Seems like a nice addition, I hope this will gain more attention in the future, creating a custom solution using the new middlewares is a option but it adds a lot of repetitive code and abstraction
[-]Request for query caching[/-][+]Query caching[/+]JoelLefkowitz commentedon Aug 3, 2021
In order to cache effectively you need a centralised caching mechanism across all PrismaClient instances. I wrote a library to implement a cache as a static property rather than as middleware. This strips away a lot of the repetitive code as mentioned by @daniellwdb.
https://github.com/JoelLefkowitz/cached-prisma
franco-roura commentedon Oct 1, 2021
Would it be a terrible idea to cache the objects with
JSON.stringify(obj)as a value and their table&id combination as a key?I'm thinking about something like
user:5"{ id: 5, name: \"Mr. Potato\" }"post:123"{ id: 123, title: \"My first post\" }"Then we could invalidate that cached object by deleting the Redis key. What do you guys think?
v0lume commentedon Oct 28, 2021
any updates on this?
casey-chow commentedon Nov 3, 2021
The main issue I've had with this is Date serialization--basically the serializer/deserializer needs some way to distinguish that type automatically.
JoelLefkowitz commentedon Dec 8, 2021
This is the way I've implemented it except I've inverted the control of the cache read/update methods. This way the caching mechanism is configurable. This makes it compatible with Memcached and possible to test without a Redis instance.
https://github.com/JoelLefkowitz/cached-prisma/blob/4ceec8b454456ebde688737be589bbbef42e7f58/src/prisma/prisma.ts#L68
@casey-chow Do you think converting dates to ISO strings before serializing and deserializing would solve the problem? This would include traversing nested objects for date fields. I don't think there are any other types that Prisma handles which aren't capable of being serialized to JSON.
casey-chow commentedon Mar 8, 2022
So we have the following Prisma scalar types, and I've italicized all the datatypes not supported by JSON:
Of these, the only one I actually use is
DateTimebut if the goal is to generalize I'm guessing everything butUnsupportedwould be needed. To this end I just settled on using msgpack because it has a built-in date serializer; the only popular serialization format that I've found that supports all these types is Amazon Ion.5 remaining items
songkeys commentedon Nov 15, 2022
@TasinIshmam Great to hear this!
May I know why it is especially for serverless though?
TasinIshmam commentedon Nov 17, 2022
Hey @songkeys
With the design partner program, we're exploring solutions to problems that are acute in serverless environments. However, the query caching problem is not exclusive to serverless environments. We will also consider partners for the program even if they are not using serverless.
Hope that clarifies things a bit.
TiagoCavalcante commentedon May 13, 2023
Any news on this?
JoelLefkowitz commentedon May 13, 2023
I can't seem to find anything on caching in the roadmap. Maybe it's included in this one: https://prismaio.notion.site/Optimize-Prisma-Client-Query-Performance-68caf089581e4d07ac154dd34ad73639
I've had a couple people fork my repo but I dont think there has been any concrete plans from the Prisma team on what they plan to do.
bsplosion commentedon Oct 30, 2023
I'm pretty confident that the comments from their team were alluding to the Prisma Accelerate system on their Prisma Data Platform.
Unfortunately, while this sort-of solves the issue for a very specific class of users, the rest of us are going to have to still roll our own caching. Actual cache integrations are still not on their roadmap, and Prisma Accelerate seems to have taken priority since they actually produce revenue with it.
Edit: also, Accelerate is unable to handle raw queries at this time which severely inhibits its utility for at least our use case. Many of the biggest, most-cacheable queries are actually done raw since they have advanced reporting and aggregations with results which are usually highly cacheable.
janpio commentedon Oct 31, 2023
What kinds of "cache integrations" would you be looking for @bsplosion?
I am not aware of any having been requested in the past.
And no, the existence of a commercial product like Accelerate with its caching functionality does not mean we will not implement something in ORM directly if it is possible. A cloud-based caching service, which Accelerate is, unfortunately is not possible in an Open Source library as it includes an infrastructure component, though.
bsplosion commentedon Oct 31, 2023
@janpio Sorry, I didn't mean to imply there's no chance that you and the team won't ever do something in the ORM directly, but it certainly does seem like Accelerate took priority from a resource perspective.
Without speaking for everyone here, I'd say something like the Cached Prisma solution would be incredibly helpful:
https://github.com/JoelLefkowitz/cached-prisma
@JoelLefkowitz posted that here back in August 2021 and I think many of our requirements have remained unchanged. Ultimately, we'd want to define a cache engine and strategy at a similar level, with the option to use standard engines like Memcached or Redis (Joel's solution already integrated Memcached).
There are of course a ton of considerations when caching, but frankly I don't think people would mind if those were largely presented as configuration options for the user to handle. The originally-proposed feature on this ticket with a cache invalidation strategy similar to Cached Prisma would probably work well for most as a start on the functionality.
Separately, I've rolled my own caching stack in the past for other projects and just intercepted prepared statements, checking the cache with hashes of the query and parameters. However, that doesn't seem to be an option here since you can't easily "prepare" the statement about to be run in Prisma without using
Prisma.sqlfor everything. Is there some way we could possibly use middleware for this? I can't seem to find a way to demarcate certain types of queries to provide the controls you'd really need from a caching perspective but perhaps I'm missing something.janpio commentedon Oct 31, 2023
Middlewares or Client Extensions indeed would be the right approaches to build something universal yourself. If you have use cases that are not covered by these (and I think you mean raw queries in your use case above), please open a new issue and we can figure out how to make that work - or if we need to build something.
In general the desires and needs expressed in this issue are super diverse. There are many many approaches to caching that make this a pretty vast problem to tackle. You said yourself that "standard engines" already would include Memcached and Redis, and if you ask someone else there would be other storage mechanisms desired as well. When we start talking about the needed functionality and API we probably get even more options.
One of them, a pretty opinionated one, was tackled by colleagues in the commercial side of Prisma with Prisma Accelerate as a hosted infrastructure component.
janpio commentedon Jun 13, 2024
What kind of API or configuration would you expect or wish for? The examples above show an API to cache individual queries with a duration as a named cache, and a way to invalidate individual named caches.
KhaoDoesDev commentedon Jun 13, 2024
This could have the same caching system from Prisma Accelerate.
TML-3398: The cache middleware gains invalidation, opaque entry meta …