slonik-interceptor-query-cache 3.1.0 → 3.3.0
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 +40 -8
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -18,17 +18,33 @@ Which queries are cached is controlled using cache attributes. Cache attributes
|
|
|
18
18
|
|
|
19
19
|
## Cache attributes
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|---|---|---|---|---|
|
|
23
|
-
|`@cache-ttl`|Number (in seconds) to cache the query for.|Yes|`/^d+$/`|N/A|
|
|
24
|
-
|`@cache-key`|Cache key that uniquelly identifies the query.|No|`/^[$A-Za-z0-9\-_:]+$/`|`$bodyHash:$valueHash`|
|
|
25
|
-
|`@cache-discard-empty`|If set to `true`, then `storage.set` is not invoked when query produces no results.|No|`/^(false|true)$/`|`false`|
|
|
21
|
+
#### `@cache-ttl`
|
|
26
22
|
|
|
27
|
-
|
|
23
|
+
|Required|Format|Default|
|
|
24
|
+
|---|---|---|
|
|
25
|
+
|Yes|`/^d+$/`|N/A|
|
|
28
26
|
|
|
29
|
-
|
|
27
|
+
Number (in seconds) to cache the query for.
|
|
30
28
|
|
|
31
|
-
|
|
29
|
+
#### `@cache-key`
|
|
30
|
+
|
|
31
|
+
|Required|Format|Default|
|
|
32
|
+
|---|---|---|
|
|
33
|
+
|No|`/^[$A-Za-z0-9\-_:]+$/`|`$bodyHash:$valueHash`|
|
|
34
|
+
|
|
35
|
+
Cache key that uniquely identifies the query.
|
|
36
|
+
|
|
37
|
+
If present, `$bodyHash` is substituted with the hash of the query (comments and white-spaces are stripped before hashing the query).
|
|
38
|
+
|
|
39
|
+
If present, `$valueHash` is substituted with the hash of the parameter values.
|
|
40
|
+
|
|
41
|
+
#### `@cache-discard-empty`
|
|
42
|
+
|
|
43
|
+
|Required|Format|Default|
|
|
44
|
+
|---|---|---|
|
|
45
|
+
|No|`/^(false\|true)$/`|`false`|
|
|
46
|
+
|
|
47
|
+
If set to `true`, then `storage.set` is not invoked when query produces no results.
|
|
32
48
|
|
|
33
49
|
### Example usage
|
|
34
50
|
|
|
@@ -64,7 +80,11 @@ const pool = await createPool('postgres://', {
|
|
|
64
80
|
}),
|
|
65
81
|
]
|
|
66
82
|
});
|
|
83
|
+
```
|
|
67
84
|
|
|
85
|
+
These are example queries:
|
|
86
|
+
|
|
87
|
+
```ts
|
|
68
88
|
// Caches the query results based on a combination of the query hash and the parameter value hash.
|
|
69
89
|
await connection.any(sql`
|
|
70
90
|
-- @cache-ttl 60
|
|
@@ -76,6 +96,18 @@ await connection.any(sql`
|
|
|
76
96
|
code_alpha_2 = ${countryCode}
|
|
77
97
|
`);
|
|
78
98
|
|
|
99
|
+
// Does not cache the result when query produces no results.
|
|
100
|
+
await connection.any(sql`
|
|
101
|
+
-- @cache-ttl 60
|
|
102
|
+
-- @cache-discard-empty true
|
|
103
|
+
SELECT
|
|
104
|
+
id,
|
|
105
|
+
code_alpha_2
|
|
106
|
+
FROM country
|
|
107
|
+
WHERE
|
|
108
|
+
code_alpha_2 = ${countryCode}
|
|
109
|
+
`);
|
|
110
|
+
|
|
79
111
|
// Caches the query results based only on the parameter value hash.
|
|
80
112
|
await connection.any(sql`
|
|
81
113
|
-- @cache-ttl 60
|
package/package.json
CHANGED