@sixthsummit/ship-cache-redis 0.0.0-stage → 0.2.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/LICENSE +202 -0
- package/NOTICE +4 -0
- package/README.md +56 -2
- package/lib/index.d.ts +51 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +112 -0
- package/lib/index.js.map +1 -0
- package/package.json +47 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,57 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @sixthsummit/ship-cache-redis
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Redis `CacheBackend` adapter for declared Ship read caching (ADR-0019).
|
|
4
|
+
|
|
5
|
+
The kernel owns the cache declaration, key derivation and hit/miss policy; this
|
|
6
|
+
package only stores bytes. It is Node-only by design — the kernel stays
|
|
7
|
+
edge-safe because the Redis adapter lives outside it.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { cacheBackendDependency, defineCacheInvalidationModule } from "@sixthsummit/ship-core";
|
|
11
|
+
import { createRedisCacheBackend } from "@sixthsummit/ship-cache-redis";
|
|
12
|
+
import Redis from "ioredis";
|
|
13
|
+
|
|
14
|
+
const cacheToken = cacheBackendDependency("platform.cache");
|
|
15
|
+
const cache = createRedisCacheBackend({
|
|
16
|
+
client: new Redis(process.env.REDIS_URL),
|
|
17
|
+
// Namespace per service and release so a deploy cannot read stale entries.
|
|
18
|
+
keyPrefix: "billing:4.0.0:",
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const module = defineModule({
|
|
22
|
+
id: "billing",
|
|
23
|
+
operations: [summary],
|
|
24
|
+
providers: [], // ship-cache owns the backend binding below
|
|
25
|
+
imports: ["ship-cache"],
|
|
26
|
+
exports: [],
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
// Mounted once: binds and exports `cacheToken`, and purges declared
|
|
30
|
+
// `invalidates` after commit. Introduces the `ship.outbox:manage` permission.
|
|
31
|
+
const invalidation = defineCacheInvalidationModule({
|
|
32
|
+
cache,
|
|
33
|
+
outbox: outboxBackend,
|
|
34
|
+
backend: cacheToken,
|
|
35
|
+
});
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
The client is injected through the small `RedisLike` interface, so this package
|
|
39
|
+
depends on no Redis driver: use `ioredis`, a cluster client, or a test fake.
|
|
40
|
+
|
|
41
|
+
## Behavior
|
|
42
|
+
|
|
43
|
+
- Values are stored as JSON with a millisecond TTL (`SET … PX`).
|
|
44
|
+
- Tagged writes `SADD` the namespaced key into `<keyPrefix><tagPrefix><tag>` and
|
|
45
|
+
give the tag set the entry TTL, so tag bookkeeping never outlives the data.
|
|
46
|
+
- `delByTag` reads the members, deletes them, and drops the set. It is called by
|
|
47
|
+
the framework's cache-invalidation dispatcher after a write commits. An
|
|
48
|
+
actor- or tenant-scoped entry is also indexed under `<tag>@actor:<id>` /
|
|
49
|
+
`<tag>@tenant:<key>`, so a scoped write purges one subject's set and leaves
|
|
50
|
+
every other subject's entries warm; the plain tag still purges them all.
|
|
51
|
+
- `singleFlight` coalesces concurrent misses for one key **within one process**.
|
|
52
|
+
Cross-process stampede control is not part of the declared contract.
|
|
53
|
+
- A stored value this adapter cannot parse is treated as a miss and deleted.
|
|
54
|
+
|
|
55
|
+
`cacheBackendConformance` from `@sixthsummit/ship-testing/cache` runs against
|
|
56
|
+
this adapter in both the unit suite (fake client) and the integration suite
|
|
57
|
+
(real Redis via testcontainers).
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { CacheBackend } from "@sixthsummit/ship-core";
|
|
2
|
+
/**
|
|
3
|
+
* The Redis surface this adapter touches. Any client implementing it works:
|
|
4
|
+
* ioredis in production, a hand-rolled fake in unit tests. Keeping the client
|
|
5
|
+
* injected means this package has no Redis driver dependency of its own.
|
|
6
|
+
*/
|
|
7
|
+
export interface RedisLike {
|
|
8
|
+
set(key: string, value: string, ...arguments_: unknown[]): Promise<unknown>;
|
|
9
|
+
get(key: string): Promise<string | null>;
|
|
10
|
+
del(...keys: string[]): Promise<number>;
|
|
11
|
+
sadd(key: string, ...members: string[]): Promise<number>;
|
|
12
|
+
smembers(key: string): Promise<string[]>;
|
|
13
|
+
pexpire(key: string, milliseconds: number): Promise<number>;
|
|
14
|
+
ping(): Promise<string>;
|
|
15
|
+
}
|
|
16
|
+
export interface RedisCacheBackendOptions {
|
|
17
|
+
readonly client: RedisLike;
|
|
18
|
+
/**
|
|
19
|
+
* Prepended to every key and tag set. Namespace it per service and release
|
|
20
|
+
* (`billing:4.0.0:`) so a deploy can never read a previous build's entries.
|
|
21
|
+
*/
|
|
22
|
+
readonly keyPrefix?: string;
|
|
23
|
+
/** Tag-set namespace, applied after `keyPrefix`. Defaults to `tag:`. */
|
|
24
|
+
readonly tagPrefix?: string;
|
|
25
|
+
/** Surfaced by `health()` when a ping exceeds it. Defaults to 250 ms. */
|
|
26
|
+
readonly slowPingMs?: number;
|
|
27
|
+
}
|
|
28
|
+
export type RedisCacheDiagnosticCode = "SHIP_CACHE_REDIS_OPTIONS_INVALID";
|
|
29
|
+
export declare class RedisCacheError extends TypeError {
|
|
30
|
+
readonly code: RedisCacheDiagnosticCode;
|
|
31
|
+
constructor(code: RedisCacheDiagnosticCode, message: string);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Redis-backed {@link CacheBackend} for declared read caching.
|
|
35
|
+
*
|
|
36
|
+
* Values are JSON: the framework only ever stores a validated operation
|
|
37
|
+
* success result, which is already wire-serializable, and JSON keeps the
|
|
38
|
+
* stored bytes inspectable by operators.
|
|
39
|
+
*
|
|
40
|
+
* Tag tracking mirrors the compatibility `RedisCache`: every tagged write
|
|
41
|
+
* SADDs the namespaced key into `<keyPrefix><tagPrefix><tag>`, and the tag set
|
|
42
|
+
* inherits the entry TTL so bookkeeping never outlives the data it tracks.
|
|
43
|
+
* `delByTag` reads the members, deletes them, and drops the set — this is what
|
|
44
|
+
* the cache-invalidation dispatcher calls after a write commits.
|
|
45
|
+
*
|
|
46
|
+
* Stampede protection is per process: `singleFlight` coalesces concurrent
|
|
47
|
+
* misses for one key inside this instance. Cross-process coalescing is not
|
|
48
|
+
* part of the declared contract.
|
|
49
|
+
*/
|
|
50
|
+
export declare function createRedisCacheBackend(options: RedisCacheBackendOptions): CacheBackend;
|
|
51
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAyC,MAAM,wBAAwB,CAAC;AAGlG;;;;GAIG;AACH,MAAM,WAAW,SAAS;IACxB,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,UAAU,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAC5E,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;IACzC,GAAG,CAAC,GAAG,IAAI,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IACxC,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,OAAO,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IACzD,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;IACzC,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAC5D,IAAI,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;CACzB;AAED,MAAM,WAAW,wBAAwB;IACvC,QAAQ,CAAC,MAAM,EAAE,SAAS,CAAC;IAC3B;;;OAGG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,wEAAwE;IACxE,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,yEAAyE;IACzE,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;CAC9B;AAED,MAAM,MAAM,wBAAwB,GAAG,kCAAkC,CAAC;AAE1E,qBAAa,eAAgB,SAAQ,SAAS;IAC5C,QAAQ,CAAC,IAAI,EAAE,wBAAwB,CAAC;gBAE5B,IAAI,EAAE,wBAAwB,EAAE,OAAO,EAAE,MAAM;CAK5D;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,uBAAuB,CAAC,OAAO,EAAE,wBAAwB,GAAG,YAAY,CA8DvF"}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { createSingleFlight } from "@sixthsummit/ship-core";
|
|
2
|
+
export class RedisCacheError extends TypeError {
|
|
3
|
+
code;
|
|
4
|
+
constructor(code, message) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.name = "RedisCacheError";
|
|
7
|
+
this.code = code;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Redis-backed {@link CacheBackend} for declared read caching.
|
|
12
|
+
*
|
|
13
|
+
* Values are JSON: the framework only ever stores a validated operation
|
|
14
|
+
* success result, which is already wire-serializable, and JSON keeps the
|
|
15
|
+
* stored bytes inspectable by operators.
|
|
16
|
+
*
|
|
17
|
+
* Tag tracking mirrors the compatibility `RedisCache`: every tagged write
|
|
18
|
+
* SADDs the namespaced key into `<keyPrefix><tagPrefix><tag>`, and the tag set
|
|
19
|
+
* inherits the entry TTL so bookkeeping never outlives the data it tracks.
|
|
20
|
+
* `delByTag` reads the members, deletes them, and drops the set — this is what
|
|
21
|
+
* the cache-invalidation dispatcher calls after a write commits.
|
|
22
|
+
*
|
|
23
|
+
* Stampede protection is per process: `singleFlight` coalesces concurrent
|
|
24
|
+
* misses for one key inside this instance. Cross-process coalescing is not
|
|
25
|
+
* part of the declared contract.
|
|
26
|
+
*/
|
|
27
|
+
export function createRedisCacheBackend(options) {
|
|
28
|
+
assertOptions(options);
|
|
29
|
+
const client = options.client;
|
|
30
|
+
const keyPrefix = options.keyPrefix ?? "";
|
|
31
|
+
const tagPrefix = options.tagPrefix ?? "tag:";
|
|
32
|
+
const slowPingMs = options.slowPingMs ?? 250;
|
|
33
|
+
const gate = createSingleFlight();
|
|
34
|
+
const namespaced = (key) => `${keyPrefix}${key}`;
|
|
35
|
+
const tagKey = (tag) => `${keyPrefix}${tagPrefix}${tag}`;
|
|
36
|
+
return Object.freeze({
|
|
37
|
+
kind: "ship.cache-backend",
|
|
38
|
+
get: async (key) => {
|
|
39
|
+
const stored = await client.get(namespaced(key));
|
|
40
|
+
if (stored === null)
|
|
41
|
+
return undefined;
|
|
42
|
+
try {
|
|
43
|
+
return JSON.parse(stored);
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
// A value this adapter did not write, or a truncated one: treat it as
|
|
47
|
+
// a miss and drop it rather than failing the read.
|
|
48
|
+
await client.del(namespaced(key)).catch(() => 0);
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
set: async (key, value, storeOptions) => {
|
|
53
|
+
const target = namespaced(key);
|
|
54
|
+
await client.set(target, JSON.stringify(value), "PX", storeOptions.ttlMs);
|
|
55
|
+
for (const tag of storeOptions.tags ?? []) {
|
|
56
|
+
const set = tagKey(tag);
|
|
57
|
+
await client.sadd(set, target);
|
|
58
|
+
await client.pexpire(set, storeOptions.ttlMs);
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
del: async (key) => {
|
|
62
|
+
await client.del(namespaced(key));
|
|
63
|
+
},
|
|
64
|
+
delByTag: async (tag) => {
|
|
65
|
+
const set = tagKey(tag);
|
|
66
|
+
const members = await client.smembers(set);
|
|
67
|
+
if (members.length > 0)
|
|
68
|
+
await client.del(...members);
|
|
69
|
+
await client.del(set);
|
|
70
|
+
},
|
|
71
|
+
health: async () => {
|
|
72
|
+
const startedAt = Date.now();
|
|
73
|
+
try {
|
|
74
|
+
const reply = await client.ping();
|
|
75
|
+
const latencyMs = Date.now() - startedAt;
|
|
76
|
+
return Object.freeze({
|
|
77
|
+
available: reply === "PONG",
|
|
78
|
+
latencyMs,
|
|
79
|
+
...(latencyMs > slowPingMs ? { detail: `ping exceeded ${slowPingMs}ms` } : {}),
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
return Object.freeze({
|
|
84
|
+
available: false,
|
|
85
|
+
latencyMs: Date.now() - startedAt,
|
|
86
|
+
detail: error instanceof Error ? error.message : "ping failed",
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
singleFlight: gate,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
function assertOptions(options) {
|
|
94
|
+
const client = options?.client;
|
|
95
|
+
const complete = typeof client === "object" &&
|
|
96
|
+
client !== null &&
|
|
97
|
+
["set", "get", "del", "sadd", "smembers", "pexpire", "ping"].every((method) => typeof client[method] === "function");
|
|
98
|
+
if (!complete) {
|
|
99
|
+
throw new RedisCacheError("SHIP_CACHE_REDIS_OPTIONS_INVALID", "redis cache backend requires a client with set, get, del, sadd, smembers, pexpire and ping");
|
|
100
|
+
}
|
|
101
|
+
for (const field of ["keyPrefix", "tagPrefix"]) {
|
|
102
|
+
const value = options[field];
|
|
103
|
+
if (value !== undefined && (typeof value !== "string" || /[\s]/u.test(value))) {
|
|
104
|
+
throw new RedisCacheError("SHIP_CACHE_REDIS_OPTIONS_INVALID", `${field} must be a whitespace-free string`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (options.slowPingMs !== undefined &&
|
|
108
|
+
(!Number.isSafeInteger(options.slowPingMs) || options.slowPingMs <= 0)) {
|
|
109
|
+
throw new RedisCacheError("SHIP_CACHE_REDIS_OPTIONS_INVALID", "slowPingMs must be a positive safe integer");
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAgC5D,MAAM,OAAO,eAAgB,SAAQ,SAAS;IACnC,IAAI,CAA2B;IAExC,YAAY,IAA8B,EAAE,OAAe;QACzD,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;QAC9B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;CACF;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,uBAAuB,CAAC,OAAiC;IACvE,aAAa,CAAC,OAAO,CAAC,CAAC;IACvB,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAC9B,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,EAAE,CAAC;IAC1C,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,MAAM,CAAC;IAC9C,MAAM,UAAU,GAAG,OAAO,CAAC,UAAU,IAAI,GAAG,CAAC;IAC7C,MAAM,IAAI,GAAG,kBAAkB,EAAE,CAAC;IAClC,MAAM,UAAU,GAAG,CAAC,GAAW,EAAU,EAAE,CAAC,GAAG,SAAS,GAAG,GAAG,EAAE,CAAC;IACjE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
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Redis cache backend adapter for declared Ship read caching",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./lib/index.js",
|
|
7
|
+
"types": "./lib/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./lib/index.d.ts",
|
|
11
|
+
"import": "./lib/index.js",
|
|
12
|
+
"default": "./lib/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"sideEffects": false,
|
|
16
|
+
"files": [
|
|
17
|
+
"lib/",
|
|
18
|
+
"README.md",
|
|
19
|
+
"LICENSE",
|
|
20
|
+
"NOTICE"
|
|
21
|
+
],
|
|
22
|
+
"peerDependencies": {
|
|
23
|
+
"@sixthsummit/ship-core": "^1.1.1"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@sixthsummit/ship-core": "1.1.1",
|
|
27
|
+
"@sixthsummit/ship-testing": "^1.2.0",
|
|
28
|
+
"ioredis": "^5.10.1",
|
|
29
|
+
"testcontainers": "^12.1.0"
|
|
30
|
+
},
|
|
31
|
+
"engines": {
|
|
32
|
+
"node": ">=22.22.0"
|
|
33
|
+
},
|
|
34
|
+
"license": "Apache-2.0",
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"registry": "https://registry.npmjs.org",
|
|
37
|
+
"access": "public"
|
|
38
|
+
},
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "git+https://github.com/SixthSummit/Ship.git",
|
|
42
|
+
"directory": "packages/cache-redis"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "node ../../scripts/clean-package.mjs && tsc -p tsconfig.json",
|
|
46
|
+
"type-check": "tsc --noEmit -p tsconfig.check.json",
|
|
47
|
+
"test": "vitest run --root ../.. packages/cache-redis/src",
|
|
48
|
+
"test:coverage": "vitest run --root ../.. packages/cache-redis/src --coverage"
|
|
49
|
+
}
|
|
6
50
|
}
|