question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:103
  • Comments:16 (4 by maintainers)

github_iconTop GitHub Comments

23reactions
AnandChowdharycommented, Nov 8, 2020

I’d also love to see an easy way to invalidate the cache, if this is implemented. For example:

const users = await client.user.find({
  select: { x: true },
  where: { id },
  // Set a cache time of 1 minute and a key of "users-cache"
  cache: { time: 60000, key: "users-cache" }
})

Then, if you update a user, for example:

await client.user.update({ where: { id }, data: { /* new user data */ } });
// Invalidate the user cache
await client.cache.invalidate("users-cache");

This can probably even happen under the hood without explicit keys, because Prisma knows when user.update or user.delete are called, it can automatically invalidate any cache for user.find, etc. 🧠

11reactions
laurencefasscommented, Jun 18, 2022

+1 for official cache middleware support!

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is query caching? How do I cache my query?
Cached Queries are a way for you to build applications with charts and tables that load instantly. A common use case for caching...
Read more >
Query Cache - MariaDB Knowledge Base
The query cache stores results of SELECT queries so that if the identical query is received in future, the results can be quickly...
Read more >
Query caching in Power BI Premium - Microsoft Learn
Organizations with Power BI Premium or Power BI Embedded can take advantage of query caching to speed up reports associated with a dataset....
Read more >
How To Optimize MySQL with Query Cache on Ubuntu 18.04
Query cache is a prominent MySQL feature that speeds up data retrieval from a database. It achieves this by storing MySQL SELECT statements ......
Read more >
Query caching | Databricks on AWS
Query results caching: Per cluster caching of query results for all queries through SQL warehouses. To disable query result caching, you can run ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found