@visualbravo/zenstack-cache 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -0
- package/dist/plugin.cjs +16 -14
- package/dist/plugin.mjs +16 -14
- package/package.json +12 -9
package/README.md
CHANGED
|
@@ -134,6 +134,21 @@ const revalidatedPublishedPosts = await client.$cache.revalidation as Post[]
|
|
|
134
134
|
> [!NOTE]
|
|
135
135
|
> 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.
|
|
136
136
|
|
|
137
|
+
## Caching Forever
|
|
138
|
+
|
|
139
|
+
You can cache results forever by specifying neither `ttl` nor `swr`. Such results will always be considered fresh.
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
client.post.findMany({
|
|
143
|
+
cache: {
|
|
144
|
+
tags: [`user:${userId}`],
|
|
145
|
+
},
|
|
146
|
+
})
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
> [!WARNING]
|
|
150
|
+
> Your server may eventually run out of memory if you're not careful about invalidating entries that never expire.
|
|
151
|
+
|
|
137
152
|
## License
|
|
138
153
|
|
|
139
154
|
MIT
|
package/dist/plugin.cjs
CHANGED
|
@@ -32,9 +32,16 @@ function defineCachePlugin(pluginOptions) {
|
|
|
32
32
|
return revalidation;
|
|
33
33
|
}
|
|
34
34
|
} },
|
|
35
|
-
onQuery: async ({ args, model, operation, proceed }) => {
|
|
35
|
+
onQuery: async ({ args, model, operation, proceed, client }) => {
|
|
36
36
|
if (args && "cache" in args) {
|
|
37
|
-
|
|
37
|
+
let json;
|
|
38
|
+
if (client.$auth) json = (0, stable_hash.stableHash)({
|
|
39
|
+
args,
|
|
40
|
+
model,
|
|
41
|
+
operation,
|
|
42
|
+
userId: Object.keys(client.$auth).filter((key$1) => client.$schema.models[client.$schema.authType].idFields.includes(key$1)).join("_")
|
|
43
|
+
});
|
|
44
|
+
else json = (0, stable_hash.stableHash)({
|
|
38
45
|
args,
|
|
39
46
|
model,
|
|
40
47
|
operation
|
|
@@ -49,18 +56,13 @@ function defineCachePlugin(pluginOptions) {
|
|
|
49
56
|
status = "hit";
|
|
50
57
|
return entry.result;
|
|
51
58
|
} else if (require_utils.entryIsStale(entry)) {
|
|
52
|
-
revalidation = proceed(args).then(
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
return result$1;
|
|
60
|
-
} catch (err) {
|
|
61
|
-
console.error(`Failed to cache query result: ${err}`);
|
|
62
|
-
return null;
|
|
63
|
-
}
|
|
59
|
+
revalidation = proceed(args).then((result$1) => {
|
|
60
|
+
cache.set(key, {
|
|
61
|
+
createdAt: Date.now(),
|
|
62
|
+
options,
|
|
63
|
+
result: result$1
|
|
64
|
+
}).catch((err) => console.error(`Failed to cache query result: ${err}`));
|
|
65
|
+
return result$1;
|
|
64
66
|
});
|
|
65
67
|
status = "stale";
|
|
66
68
|
return entry.result;
|
package/dist/plugin.mjs
CHANGED
|
@@ -30,9 +30,16 @@ function defineCachePlugin(pluginOptions) {
|
|
|
30
30
|
return revalidation;
|
|
31
31
|
}
|
|
32
32
|
} },
|
|
33
|
-
onQuery: async ({ args, model, operation, proceed }) => {
|
|
33
|
+
onQuery: async ({ args, model, operation, proceed, client }) => {
|
|
34
34
|
if (args && "cache" in args) {
|
|
35
|
-
|
|
35
|
+
let json;
|
|
36
|
+
if (client.$auth) json = stableHash({
|
|
37
|
+
args,
|
|
38
|
+
model,
|
|
39
|
+
operation,
|
|
40
|
+
userId: Object.keys(client.$auth).filter((key$1) => client.$schema.models[client.$schema.authType].idFields.includes(key$1)).join("_")
|
|
41
|
+
});
|
|
42
|
+
else json = stableHash({
|
|
36
43
|
args,
|
|
37
44
|
model,
|
|
38
45
|
operation
|
|
@@ -47,18 +54,13 @@ function defineCachePlugin(pluginOptions) {
|
|
|
47
54
|
status = "hit";
|
|
48
55
|
return entry.result;
|
|
49
56
|
} else if (entryIsStale(entry)) {
|
|
50
|
-
revalidation = proceed(args).then(
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
return result$1;
|
|
58
|
-
} catch (err) {
|
|
59
|
-
console.error(`Failed to cache query result: ${err}`);
|
|
60
|
-
return null;
|
|
61
|
-
}
|
|
57
|
+
revalidation = proceed(args).then((result$1) => {
|
|
58
|
+
cache.set(key, {
|
|
59
|
+
createdAt: Date.now(),
|
|
60
|
+
options,
|
|
61
|
+
result: result$1
|
|
62
|
+
}).catch((err) => console.error(`Failed to cache query result: ${err}`));
|
|
63
|
+
return result$1;
|
|
62
64
|
});
|
|
63
65
|
status = "stale";
|
|
64
66
|
return entry.result;
|
package/package.json
CHANGED
|
@@ -1,20 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@visualbravo/zenstack-cache",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
|
+
"keywords": [
|
|
5
|
+
"accelerate",
|
|
6
|
+
"cache",
|
|
7
|
+
"caching",
|
|
8
|
+
"orm",
|
|
9
|
+
"prisma",
|
|
10
|
+
"redis",
|
|
11
|
+
"self-hosted",
|
|
12
|
+
"typescript",
|
|
13
|
+
"zenstack"
|
|
14
|
+
],
|
|
4
15
|
"license": "MIT",
|
|
5
16
|
"repository": "github:visualbravo/zenstack-cache",
|
|
6
17
|
"type": "module",
|
|
7
18
|
"main": "./dist/index.cjs",
|
|
8
19
|
"module": "./dist/index.mjs",
|
|
9
20
|
"types": "./dist/index.d.cts",
|
|
10
|
-
"keywords": [
|
|
11
|
-
"zenstack",
|
|
12
|
-
"cache",
|
|
13
|
-
"caching",
|
|
14
|
-
"prisma",
|
|
15
|
-
"accelerate",
|
|
16
|
-
"orm"
|
|
17
|
-
],
|
|
18
21
|
"exports": {
|
|
19
22
|
".": {
|
|
20
23
|
"@zenstack-cache/source": "./src/index.ts",
|