Skip to content
Merged
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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,21 @@ const revalidatedPublishedPosts = await client.$cache.revalidation as Post[]
> [!NOTE]
> The total TTL of a cache entry is equal to its `ttl` + `swr`. The `ttl` window comes first, followed by the `swr` window. You can combine the two options to best suit the needs of your application.

## Caching Forever

You can cache results forever by specifying neither `ttl` nor `swr`. Such results will always be considered fresh.

```typescript
client.post.findMany({
cache: {
tags: [`user:${userId}`],
},
})
```

> [!WARNING]
> Your server may eventually run out of memory if you're not careful about invalidating entries that never expire.

## License

MIT
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "workspace",
"type": "module",
"version": "1.0.1",
"version": "1.0.2",
"private": "true",
"license": "MIT",
"scripts": {
Expand Down
11 changes: 9 additions & 2 deletions packages/cache/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
{
"name": "@visualbravo/zenstack-cache",
"version": "1.0.1",
"version": "1.0.2",
"license": "MIT",
"repository": "github:visualbravo/zenstack-cache",
"type": "module",
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.cts",
"keywords": ["zenstack", "cache", "caching", "prisma", "accelerate", "orm"],
"keywords": [
"zenstack",
"cache",
"caching",
"prisma",
"accelerate",
"orm"
],
"exports": {
".": {
"@zenstack-cache/source": "./src/index.ts",
Expand Down
15 changes: 6 additions & 9 deletions packages/cache/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,16 @@ export function defineCachePlugin(pluginOptions: CachePluginOptions) {
status = 'hit'
return entry.result
} else if (entryIsStale(entry)) {
revalidation = proceed(args).then(async result => {
try {
await cache.set(key, {
revalidation = proceed(args).then(result => {
cache
.set(key, {
createdAt: Date.now(),
options,
result,
})
.catch(err => console.error(`Failed to cache query result: ${err}`))

return result
} catch (err) {
console.error(`Failed to cache query result: ${err}`)
return null
}
return result
})

status = 'stale'
Expand All @@ -117,4 +114,4 @@ export function defineCachePlugin(pluginOptions: CachePluginOptions) {
return proceed(args)
},
})
}
}
2 changes: 1 addition & 1 deletion packages/config/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstack-cache/config",
"type": "module",
"version": "1.0.1",
"version": "1.0.2",
"private": true,
"exports": {
"./ts": "./ts.json"
Expand Down