ioredis-toolkit 0.0.9 → 0.5.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.
Files changed (267) hide show
  1. package/CHANGELOG.md +67 -0
  2. package/LICENSE +1 -1
  3. package/README.md +68 -1058
  4. package/dist/cache/cache.d.ts +30 -0
  5. package/dist/cache/cache.d.ts.map +1 -0
  6. package/dist/cache/cache.js +59 -0
  7. package/dist/cache/cache.js.map +1 -0
  8. package/dist/cache/config.d.ts +12 -0
  9. package/dist/cache/config.d.ts.map +1 -0
  10. package/dist/cache/config.js +13 -0
  11. package/dist/cache/config.js.map +1 -0
  12. package/dist/cache/types.d.ts +32 -0
  13. package/dist/cache/types.d.ts.map +1 -0
  14. package/dist/cache/types.js +5 -0
  15. package/dist/cache/types.js.map +1 -0
  16. package/dist/index.d.ts +43 -51
  17. package/dist/index.d.ts.map +1 -0
  18. package/dist/index.js +32 -44
  19. package/dist/index.js.map +1 -0
  20. package/dist/lock/config.d.ts +12 -0
  21. package/dist/lock/config.d.ts.map +1 -0
  22. package/dist/lock/config.js +8 -0
  23. package/dist/lock/config.js.map +1 -0
  24. package/dist/lock/lock.d.ts +20 -0
  25. package/dist/lock/lock.d.ts.map +1 -0
  26. package/dist/lock/lock.js +44 -0
  27. package/dist/lock/lock.js.map +1 -0
  28. package/dist/lock/types.d.ts +19 -0
  29. package/dist/lock/types.d.ts.map +1 -0
  30. package/dist/lock/types.js +2 -0
  31. package/dist/lock/types.js.map +1 -0
  32. package/dist/modules-config.d.ts +3 -0
  33. package/dist/modules-config.d.ts.map +1 -0
  34. package/dist/modules-config.js +2 -0
  35. package/dist/modules-config.js.map +1 -0
  36. package/dist/pubsub/config.d.ts +11 -0
  37. package/dist/pubsub/config.d.ts.map +1 -0
  38. package/dist/pubsub/config.js +6 -0
  39. package/dist/pubsub/config.js.map +1 -0
  40. package/dist/pubsub/pubsub.d.ts +20 -0
  41. package/dist/pubsub/pubsub.d.ts.map +1 -0
  42. package/dist/pubsub/pubsub.js +54 -0
  43. package/dist/pubsub/pubsub.js.map +1 -0
  44. package/dist/pubsub/types.d.ts +24 -0
  45. package/dist/pubsub/types.d.ts.map +1 -0
  46. package/dist/pubsub/types.js +2 -0
  47. package/dist/pubsub/types.js.map +1 -0
  48. package/dist/rate-limit/config.d.ts +12 -0
  49. package/dist/rate-limit/config.d.ts.map +1 -0
  50. package/dist/rate-limit/config.js +6 -0
  51. package/dist/rate-limit/config.js.map +1 -0
  52. package/dist/rate-limit/rate-limiter.d.ts +18 -0
  53. package/dist/rate-limit/rate-limiter.d.ts.map +1 -0
  54. package/dist/rate-limit/rate-limiter.js +37 -0
  55. package/dist/rate-limit/rate-limiter.js.map +1 -0
  56. package/dist/rate-limit/types.d.ts +27 -0
  57. package/dist/rate-limit/types.d.ts.map +1 -0
  58. package/dist/rate-limit/types.js +2 -0
  59. package/dist/rate-limit/types.js.map +1 -0
  60. package/dist/redis/client-facade.d.ts +77 -0
  61. package/dist/redis/client-facade.d.ts.map +1 -0
  62. package/dist/redis/client-facade.js +102 -0
  63. package/dist/redis/client-facade.js.map +1 -0
  64. package/dist/redis/client.d.ts +10 -0
  65. package/dist/redis/client.d.ts.map +1 -0
  66. package/dist/redis/client.js +29 -0
  67. package/dist/redis/client.js.map +1 -0
  68. package/dist/redis/cluster.d.ts +7 -0
  69. package/dist/redis/cluster.d.ts.map +1 -0
  70. package/dist/redis/cluster.js +47 -0
  71. package/dist/redis/cluster.js.map +1 -0
  72. package/dist/redis/config.d.ts +39 -0
  73. package/dist/redis/config.d.ts.map +1 -0
  74. package/dist/redis/config.js +52 -0
  75. package/dist/redis/config.js.map +1 -0
  76. package/dist/redis/errors.d.ts +5 -0
  77. package/dist/redis/errors.d.ts.map +1 -0
  78. package/dist/redis/errors.js +5 -0
  79. package/dist/redis/errors.js.map +1 -0
  80. package/dist/redis/types.d.ts +135 -0
  81. package/dist/redis/types.d.ts.map +1 -0
  82. package/dist/redis/types.js +2 -0
  83. package/dist/redis/types.js.map +1 -0
  84. package/dist/redis/wrapper.d.ts +88 -0
  85. package/dist/redis/wrapper.d.ts.map +1 -0
  86. package/dist/redis/wrapper.js +206 -0
  87. package/dist/redis/wrapper.js.map +1 -0
  88. package/dist/session/config.d.ts +47 -0
  89. package/dist/session/config.d.ts.map +1 -0
  90. package/dist/session/config.js +101 -0
  91. package/dist/session/config.js.map +1 -0
  92. package/dist/session/cookie.d.ts +16 -0
  93. package/dist/session/cookie.d.ts.map +1 -0
  94. package/dist/session/cookie.js +28 -0
  95. package/dist/session/cookie.js.map +1 -0
  96. package/dist/session/errors.d.ts +56 -0
  97. package/dist/session/errors.d.ts.map +1 -0
  98. package/dist/session/errors.js +58 -0
  99. package/dist/session/errors.js.map +1 -0
  100. package/dist/session/factory.d.ts +21 -0
  101. package/dist/session/factory.d.ts.map +1 -0
  102. package/dist/session/factory.js +30 -0
  103. package/dist/session/factory.js.map +1 -0
  104. package/dist/session/health.d.ts +12 -0
  105. package/dist/session/health.d.ts.map +1 -0
  106. package/dist/session/health.js +23 -0
  107. package/dist/session/health.js.map +1 -0
  108. package/dist/session/keys.d.ts +23 -0
  109. package/dist/session/keys.d.ts.map +1 -0
  110. package/dist/session/keys.js +27 -0
  111. package/dist/session/keys.js.map +1 -0
  112. package/dist/session/manager.d.ts +34 -0
  113. package/dist/session/manager.d.ts.map +1 -0
  114. package/dist/session/manager.js +31 -0
  115. package/dist/session/manager.js.map +1 -0
  116. package/dist/session/metrics.d.ts +11 -0
  117. package/dist/session/metrics.d.ts.map +1 -0
  118. package/dist/session/metrics.js +10 -0
  119. package/dist/session/metrics.js.map +1 -0
  120. package/dist/session/repository.d.ts +49 -0
  121. package/dist/session/repository.d.ts.map +1 -0
  122. package/dist/session/repository.js +203 -0
  123. package/dist/session/repository.js.map +1 -0
  124. package/dist/session/revocation.d.ts +22 -0
  125. package/dist/session/revocation.d.ts.map +1 -0
  126. package/dist/session/revocation.js +41 -0
  127. package/dist/session/revocation.js.map +1 -0
  128. package/dist/session/script-sources.d.ts +11 -0
  129. package/dist/session/script-sources.d.ts.map +1 -0
  130. package/dist/session/script-sources.js +140 -0
  131. package/dist/session/script-sources.js.map +1 -0
  132. package/dist/session/scripts.d.ts +15 -0
  133. package/dist/session/scripts.d.ts.map +1 -0
  134. package/dist/session/scripts.js +41 -0
  135. package/dist/session/scripts.js.map +1 -0
  136. package/dist/session/serializer.d.ts +12 -0
  137. package/dist/session/serializer.d.ts.map +1 -0
  138. package/dist/session/serializer.js +77 -0
  139. package/dist/session/serializer.js.map +1 -0
  140. package/dist/session/service.d.ts +48 -0
  141. package/dist/session/service.d.ts.map +1 -0
  142. package/dist/session/service.js +235 -0
  143. package/dist/session/service.js.map +1 -0
  144. package/dist/session/token.d.ts +16 -0
  145. package/dist/session/token.d.ts.map +1 -0
  146. package/dist/session/token.js +32 -0
  147. package/dist/session/token.js.map +1 -0
  148. package/dist/session/types.d.ts +134 -0
  149. package/dist/session/types.d.ts.map +1 -0
  150. package/dist/session/types.js +2 -0
  151. package/dist/session/types.js.map +1 -0
  152. package/dist/streams/config.d.ts +12 -0
  153. package/dist/streams/config.d.ts.map +1 -0
  154. package/dist/streams/config.js +6 -0
  155. package/dist/streams/config.js.map +1 -0
  156. package/dist/streams/streams.d.ts +24 -0
  157. package/dist/streams/streams.d.ts.map +1 -0
  158. package/dist/streams/streams.js +55 -0
  159. package/dist/streams/streams.js.map +1 -0
  160. package/dist/streams/types.d.ts +32 -0
  161. package/dist/streams/types.d.ts.map +1 -0
  162. package/dist/streams/types.js +2 -0
  163. package/dist/streams/types.js.map +1 -0
  164. package/docs/ACCEPTANCE-REPORT.md +70 -0
  165. package/docs/ARCHITECTURE.md +61 -0
  166. package/docs/CAPACITY.md +33 -0
  167. package/docs/DEPLOYMENT.md +22 -0
  168. package/docs/README-API.md +15 -0
  169. package/docs/STATE-MACHINE.md +38 -0
  170. package/docs/TESTING.md +37 -0
  171. package/docs/THREAT-MODEL.md +23 -0
  172. package/docs/TYPE-SAFETY.md +34 -0
  173. package/docs/modules/cache/README.md +7 -0
  174. package/docs/modules/cache/usage.md +156 -0
  175. package/docs/modules/lock/README.md +7 -0
  176. package/docs/modules/lock/usage.md +105 -0
  177. package/docs/modules/pubsub/README.md +7 -0
  178. package/docs/modules/pubsub/usage.md +106 -0
  179. package/docs/modules/rate-limit/README.md +7 -0
  180. package/docs/modules/rate-limit/usage.md +100 -0
  181. package/docs/modules/sessions/README.md +7 -0
  182. package/docs/modules/sessions/usage.md +262 -0
  183. package/docs/modules/streams/README.md +7 -0
  184. package/docs/modules/streams/usage.md +141 -0
  185. package/package.json +50 -60
  186. package/src/scripts/cleanup-index.lua +4 -0
  187. package/src/scripts/conditional-update.lua +21 -0
  188. package/src/scripts/consume-session.lua +21 -0
  189. package/src/scripts/create-session.lua +28 -0
  190. package/src/scripts/delete.lua +2 -0
  191. package/src/scripts/destroy-user.lua +13 -0
  192. package/src/scripts/enforce-limit.lua +17 -0
  193. package/src/scripts/revoke-session.lua +13 -0
  194. package/src/scripts/rotate.lua +24 -0
  195. package/src/scripts/touch-session.lua +28 -0
  196. package/src/scripts/update-session.lua +18 -0
  197. package/dist/cache.d.ts +0 -796
  198. package/dist/cache.js +0 -1120
  199. package/dist/client.d.ts +0 -284
  200. package/dist/client.js +0 -1114
  201. package/dist/cluster-slot.d.ts +0 -4
  202. package/dist/cluster-slot.js +0 -31
  203. package/dist/cluster.d.ts +0 -79
  204. package/dist/cluster.js +0 -156
  205. package/dist/errors.d.ts +0 -30
  206. package/dist/errors.js +0 -63
  207. package/dist/health.d.ts +0 -180
  208. package/dist/health.js +0 -239
  209. package/dist/lock.d.ts +0 -248
  210. package/dist/lock.js +0 -397
  211. package/dist/logger.d.ts +0 -12
  212. package/dist/logger.js +0 -40
  213. package/dist/pubsub.d.ts +0 -423
  214. package/dist/pubsub.js +0 -537
  215. package/dist/ratelimiter.d.ts +0 -441
  216. package/dist/ratelimiter.js +0 -539
  217. package/dist/session/index.d.ts +0 -23
  218. package/dist/session/index.js +0 -16
  219. package/dist/session/revocation-store.d.ts +0 -176
  220. package/dist/session/revocation-store.js +0 -318
  221. package/dist/session/scripts/cleanup-index.lua +0 -21
  222. package/dist/session/scripts/conditional-update-encrypted.lua +0 -60
  223. package/dist/session/scripts/conditional-update.lua +0 -63
  224. package/dist/session/scripts/create.lua +0 -83
  225. package/dist/session/scripts/delete-by-user.lua +0 -29
  226. package/dist/session/scripts/delete.lua +0 -15
  227. package/dist/session/scripts/enforce-limit.lua +0 -38
  228. package/dist/session/scripts/revoke.lua +0 -61
  229. package/dist/session/scripts/rotate-encrypted.lua +0 -110
  230. package/dist/session/scripts/rotate.lua +0 -122
  231. package/dist/session/scripts/touch-encrypted.lua +0 -89
  232. package/dist/session/scripts/touch.lua +0 -72
  233. package/dist/session/scripts/validate.lua +0 -90
  234. package/dist/session/session-circuit-breaker.d.ts +0 -42
  235. package/dist/session/session-circuit-breaker.js +0 -129
  236. package/dist/session/session-config.d.ts +0 -335
  237. package/dist/session/session-config.js +0 -162
  238. package/dist/session/session-cookie.d.ts +0 -72
  239. package/dist/session/session-cookie.js +0 -101
  240. package/dist/session/session-encryption.d.ts +0 -87
  241. package/dist/session/session-encryption.js +0 -139
  242. package/dist/session/session-errors.d.ts +0 -85
  243. package/dist/session/session-errors.js +0 -145
  244. package/dist/session/session-health.d.ts +0 -38
  245. package/dist/session/session-health.js +0 -60
  246. package/dist/session/session-keys.d.ts +0 -51
  247. package/dist/session/session-keys.js +0 -113
  248. package/dist/session/session-manager.d.ts +0 -73
  249. package/dist/session/session-manager.js +0 -94
  250. package/dist/session/session-metrics.d.ts +0 -33
  251. package/dist/session/session-metrics.js +0 -112
  252. package/dist/session/session-repository.d.ts +0 -161
  253. package/dist/session/session-repository.js +0 -683
  254. package/dist/session/session-scripts.d.ts +0 -36
  255. package/dist/session/session-scripts.js +0 -130
  256. package/dist/session/session-serializer.d.ts +0 -42
  257. package/dist/session/session-serializer.js +0 -248
  258. package/dist/session/session-service.d.ts +0 -104
  259. package/dist/session/session-service.js +0 -611
  260. package/dist/session/session-token.d.ts +0 -38
  261. package/dist/session/session-token.js +0 -86
  262. package/dist/session/session-types.d.ts +0 -253
  263. package/dist/session/session-types.js +0 -16
  264. package/dist/types.d.ts +0 -924
  265. package/dist/types.js +0 -151
  266. package/dist/utils/deepmerge.d.ts +0 -9
  267. package/dist/utils/deepmerge.js +0 -61
package/dist/cache.d.ts DELETED
@@ -1,796 +0,0 @@
1
- import { RedisClientWrapper } from './client.js';
2
- import { CacheOptions, CacheInputConfig } from './types.js';
3
- import { LoggerLike } from './logger.js';
4
- /**
5
- * Cache layer on top of {@link RedisClientWrapper} with JSON serialization,
6
- * optional gzip compression and namespace support.
7
- *
8
- * Works in all three modes (standalone, sentinel, cluster): multi-key operations
9
- * are slot-aware and pattern scans cover every cluster node.
10
- *
11
- * @example
12
- * ```ts
13
- * const cache = new Cache(client, { defaultTTL: 3600, compressionThreshold: 1024 });
14
- * await cache.set('user:1', { name: 'alice' });
15
- * const user = await cache.get('user:1');
16
- * ```
17
- */
18
- export declare class Cache {
19
- private client;
20
- private logger;
21
- private defaultTTL;
22
- private compressionThreshold;
23
- private namespace;
24
- /**
25
- * Creates a cache bound to a Redis client.
26
- *
27
- * **Parameters:**
28
- * - `client` - The underlying {@link RedisClientWrapper}. All cache operations
29
- * delegate to this client.
30
- * - `config` - Configuration object with the following fields:
31
- * - `defaultTTL` (number, optional, default: `3600`) - Default TTL in seconds
32
- * applied when no per-call TTL is specified.
33
- * - `compressionThreshold` (number, optional, default: `1024`) - Byte threshold
34
- * above which values are gzip-compressed transparently.
35
- * - `namespace` (string, optional, default: `cache`) - Namespace prefix for all keys.
36
- * - `logger` - Optional pino-compatible logger. Supports `trace/debug/info/warn/error/fatal`
37
- * levels and `child()` for namespace logging. Defaults to `console`.
38
- *
39
- * **Type Parameters:**
40
- * - `T` - The type of values stored/retrieved from the cache.
41
- *
42
- * **Example:**
43
- * ```ts
44
- * const cache = new Cache(client, { defaultTTL: 600, compressionThreshold: 2048, namespace: "myapp" });
45
- * ```
46
- */
47
- constructor(client: RedisClientWrapper, config: CacheInputConfig, logger?: LoggerLike);
48
- private serialize;
49
- private deserialize;
50
- private getKey;
51
- /**
52
- * Reads a cached value.
53
- *
54
- * Objects are parsed from JSON and compressed values are transparently
55
- * decompressed. Strings that are not JSON are returned as-is.
56
- *
57
- * @param key - Cache key.
58
- * @param namespace - Optional namespace prefix (`namespace:key`).
59
- * @returns The stored value, or `null` when missing.
60
- *
61
- * @example
62
- * ```ts
63
- * const user = await cache.get<User>('user:1');
64
- * const token = await cache.get('token', 'auth');
65
- * ```
66
- */
67
- /**
68
- * Reads a cached value.
69
- *
70
- * **Behavior:**
71
- * - Objects are parsed from JSON when stored as JSON.
72
- * - Values larger than `compressionThreshold` bytes are transparently
73
- * decompressed (gzip) when reading.
74
- * - Strings that are not JSON are returned as-is.
75
- * - If the key does not exist, returns `null`.
76
- *
77
- * **Type Parameters:**
78
- * - `T` - The expected return type. When the stored value is a JSON object/array,
79
- * it will be parsed and returned as `T`. When it's a primitive (string/number/bool),
80
- * it is returned as-is and typed as `T`.
81
- *
82
- * **Returns:**
83
- * - The stored value, parsed as `T` when possible, or `null` when the key is missing.
84
- *
85
- * **Example:**
86
- * ```ts
87
- * // Store an object
88
- * await cache.set('user:1', { name: 'alice', age: 30 });
89
- *
90
- * // Read it back with type coercion
91
- * const user: { name: string; age: number } | null = await cache.get<UserProfile>('user:1');
92
- * // user === { name: 'alice', age: 30 }
93
- *
94
- * // Read a string value
95
- * const token = await cache.get('token'); // 'abc' | null
96
- * ```
97
- *
98
- * **Parameters:**
99
- * - `key` - Cache key.
100
- * - `namespace` - Optional namespace prefix (`namespace:key`). When provided,
101
- * the key is internally transformed to `${namespace}:${key}`.
102
- *
103
- * @returns The stored value, or `null` when missing.
104
- */
105
- get<T = any>(key: string, namespace?: string): Promise<T | null>;
106
- /**
107
- * Stores a value in the cache.
108
- *
109
- * @param key - Cache key.
110
- * @param value - Any serializable value (string, number, boolean, Buffer, object).
111
- * @param options - `ttl` in seconds (defaults to `defaultTTL`), `namespace`,
112
- * and `compress` (default `true`). Values larger than `compressionThreshold`
113
- * bytes are gzip-compressed.
114
- * @returns `true` when stored successfully.
115
- *
116
- * @example
117
- * ```ts
118
- * await cache.set('user:1', user, { ttl: 300 });
119
- * await cache.set('token', 'abc', { namespace: 'auth', compress: false });
120
- * ```
121
- */
122
- /**
123
- * Stores a value in the cache.
124
- *
125
- * **Behavior:**
126
- * - Values are JSON-serialized when they are objects, arrays, or booleans/strings/numbers
127
- * are stored as-is.
128
- * - Values larger than `compressionThreshold` bytes are gzip-compressed transparently.
129
- * The compressed form is stored with metadata (`_compressed: true`, `_data: base64`) so
130
- * it is transparently decompressed on read.
131
- * - Set `compress: false` to disable compression for a single write, regardless of size.
132
- * - Per-call TTL overrides the cache's `defaultTTL`.
133
- * - Per-call `namespace` overrides the cache's configured namespace for that operation.
134
- *
135
- * **Type Parameters:**
136
- * - `T` - The type of the value being stored. Can be any serializable JavaScript value.
137
- *
138
- * **Returns:**
139
- * - `true` when the value was stored successfully (`result === 'OK'`).
140
- *
141
- * **Example:**
142
- * ```ts
143
- * // Store an object with a custom TTL
144
- * await cache.set('user:1', { name: 'alice' }, { ttl: 300 });
145
- *
146
- * // Store with compression disabled
147
- * await cache.set('token', 'abc123', { compress: false });
148
- *
149
- * // Store with a namespace
150
- * await cache.set('token', 'abc', { namespace: 'auth' });
151
- * ```
152
- *
153
- * **Parameters:**
154
- * - `key` - Cache key.
155
- * - `value` - Any serializable value (string, number, boolean, Buffer, or object).
156
- * - `options` - Optional configuration:
157
- * - `ttl` (number, optional) - TTL in seconds. Falls back to `defaultTTL`.
158
- * - `namespace` (string, optional) - Namespace prefix. Falls back to cache config.
159
- * - `compress` (boolean, optional) - Force compression or disable it. Defaults to `true`.
160
- *
161
- * @returns `true` when stored successfully.
162
- */
163
- set<T>(key: string, value: T, options?: CacheOptions): Promise<boolean>;
164
- /**
165
- * Stores a value only if the key does not exist yet (`SETNX`).
166
- *
167
- * @param key - Cache key.
168
- * @param value - The value to store.
169
- * @param options - `ttl` in seconds and `namespace`.
170
- * @returns `true` only when the value was actually stored.
171
- *
172
- * @example
173
- * ```ts
174
- * const claimed = await cache.setNX('job:1', 'worker-1', { ttl: 60 });
175
- * ```
176
- */
177
- /**
178
- * Stores a value only if the key does not exist yet (`SETNX`).
179
- *
180
- * **Behavior:**
181
- * - The value is stored atomically using Redis `SET key value EX ttl NX`.
182
- * - Returns `true` only when the key did not exist and the value was set.
183
- * - Per-call TTL overrides the cache's `defaultTTL`.
184
- * - Per-call `namespace` is applied to the key.
185
- *
186
- * **Type Parameters:**
187
- * - `T` - The type of the value being stored. Will be JSON-stringified if not a string.
188
- *
189
- * **Returns:**
190
- * - `true` only when the value was actually stored (Redis SETNX returned `1`).
191
- *
192
- * **Example:**
193
- * ```ts
194
- * const claimed = await cache.setNX('job:1', 'worker-1', { ttl: 60 });
195
- * // claimed === true (job was claimed by this worker)
196
- * ```
197
- *
198
- * **Parameters:**
199
- * - `key` - Cache key.
200
- * - `value` - The value to store. String stored as-is; objects are JSON-stringified.
201
- * - `options` - Optional configuration:
202
- * - `ttl` (number, optional) - TTL in seconds. Falls back to `defaultTTL`.
203
- * - `namespace` (string, optional) - Namespace prefix.
204
- *
205
- * @returns `true` only when the value was actually stored.
206
- */
207
- setNX<T>(key: string, value: T, options?: CacheOptions): Promise<boolean>;
208
- /**
209
- * Stores a value only if the key does not exist yet, atomically with the TTL
210
- * (`SET ... EX NX`).
211
- *
212
- * @param key - Cache key.
213
- * @param value - The value to store.
214
- * @param options - `ttl` in seconds and `namespace`.
215
- * @returns `true` only when the value was actually stored.
216
- *
217
- * @example
218
- * ```ts
219
- * const locked = await cache.setEXNX('lock:order:42', 'txn-id', { ttl: 30 });
220
- * ```
221
- */
222
- /**
223
- * Stores a value only if the key does not exist yet, atomically with the TTL
224
- * (`SET ... EX NX`).
225
- *
226
- * **Behavior:**
227
- * - The value is stored atomically using Redis `SET key value EX ttl NX`.
228
- * - This is the atomic equivalent of calling `SET key value EX ttl` followed by
229
- * `SET key value NX` - but done in a single Redis call.
230
- * - Returns `true` only when the key did not exist and the value was set with TTL.
231
- * - Per-call TTL overrides the cache's `defaultTTL`.
232
- * - Per-call `namespace` is applied to the key.
233
- *
234
- * **Type Parameters:**
235
- * - `T` - The type of the value being stored. Will be JSON-stringified if not a string.
236
- *
237
- * **Returns:**
238
- * - `true` only when the value was actually stored (Redis SET returned `OK`).
239
- *
240
- * **Example:**
241
- * ```ts
242
- * const locked = await cache.setEXNX('lock:order:42', 'txn-id', { ttl: 30 });
243
- * // locked === true (lock was acquired with 30s TTL)
244
- * ```
245
- *
246
- * **Parameters:**
247
- * - `key` - Cache key.
248
- * - `value` - The value to store. String stored as-is; objects are JSON-stringified.
249
- * - `options` - Optional configuration:
250
- * - `ttl` (number, optional) - TTL in seconds. Falls back to `defaultTTL`.
251
- * - `namespace` (string, optional) - Namespace prefix.
252
- *
253
- * @returns `true` only when the value was actually stored.
254
- */
255
- setEXNX<T>(key: string, value: T, options?: CacheOptions): Promise<boolean>;
256
- /**
257
- * Reads multiple cache keys in one call.
258
- *
259
- * Cluster-safe: keys are grouped by hash slot under the hood.
260
- *
261
- * @param keys - Cache keys to read.
262
- * @param namespace - Optional namespace prefix applied to every key.
263
- * @returns Values in input order; `null` for missing keys.
264
- *
265
- * @example
266
- * ```ts
267
- * const [a, b] = await cache.mget(['user:1', 'user:2']);
268
- * ```
269
- */
270
- /**
271
- * Reads multiple cache keys in one call.
272
- *
273
- * **Behavior:**
274
- * - Cluster-safe: keys are grouped by hash slot under the hood, avoiding CROSS-SLOT errors.
275
- * - Values are deserialized from JSON when stored as JSON. Strings/numbers/buffers
276
- * are returned as-is.
277
- * - Missing keys return `null` in the corresponding position.
278
- *
279
- * **Type Parameters:**
280
- * - `T` - The expected type of each returned value. When the stored value is JSON,
281
- * it will be parsed and coerced to `T`.
282
- *
283
- * **Returns:**
284
- * - An array of values in the same order as the input `keys`. Each element is `T | null`.
285
- * `null` indicates the key did not exist.
286
- *
287
- * **Example:**
288
- * ```ts
289
- * const [a, b] = await cache.mget(['user:1', 'user:2']);
290
- * // a === { name: 'alice' }, b === { name: 'bob' }
291
- * ```
292
- *
293
- * **Parameters:**
294
- * - `keys` - Cache keys to read. Will have the namespace prefix applied automatically
295
- * if a namespace is configured.
296
- * - `namespace` - Optional namespace prefix applied to every key. When provided,
297
- * each key is internally transformed to `${namespace}:${key}`.
298
- *
299
- * @returns Values in input order; `null` for missing keys.
300
- */
301
- mget<T = any>(keys: string[], namespace?: string): Promise<(T | null)[]>;
302
- /**
303
- * Stores multiple key/value entries in one call.
304
- *
305
- * Cluster-safe: entries are grouped by hash slot, one pipeline per slot.
306
- *
307
- * @param entries - Object mapping cache keys to values.
308
- * @param options - `ttl` in seconds (defaults to `defaultTTL`) and `namespace`.
309
- * @returns `true` when every entry was stored.
310
- *
311
- * @example
312
- * ```ts
313
- * await cache.mset({ 'user:1': alice, 'user:2': bob }, { ttl: 300 });
314
- * ```
315
- */
316
- /**
317
- * Stores multiple key/value entries in one call.
318
- *
319
- * **Behavior:**
320
- * - Cluster-safe: entries are grouped by hash slot, one pipeline per slot.
321
- * This avoids CROSS-SLOT errors that would occur if keys spanned multiple slots in a
322
- * single pipeline.
323
- * - Values are JSON-serialized when they are objects; strings/buffers are stored as-is.
324
- * - Per-call TTL overrides the cache's `defaultTTL`.
325
- * - Per-call `namespace` is applied to all keys.
326
- *
327
- * **Type Parameters:**
328
- * - `T` - The type of values being stored. Objects are JSON-stringified; strings/buffers
329
- * are stored as-is.
330
- *
331
- * **Returns:**
332
- * - `true` when every entry was stored successfully.
333
- * - `false` if any entry failed to store.
334
- *
335
- * **Example:**
336
- * ```ts
337
- * await cache.mset({ 'user:1': alice, 'user:2': bob }, { ttl: 300 });
338
- * // Both entries stored with a 5-minute TTL
339
- * ```
340
- *
341
- * **Parameters:**
342
- * - `entries` - Object mapping cache keys to values.
343
- * - `options` - Optional configuration:
344
- * - `ttl` (number, optional) - TTL in seconds. Falls back to `defaultTTL`.
345
- * - `namespace` (string, optional) - Namespace prefix. Applied to all keys.
346
- *
347
- * @returns `true` when every entry was stored.
348
- */
349
- mset<T>(entries: Record<string, T>, options?: CacheOptions): Promise<boolean>;
350
- /**
351
- * Deletes a cache key.
352
- *
353
- * @param key - Cache key.
354
- * @param namespace - Optional namespace prefix.
355
- * @returns `true` if the key existed and was deleted.
356
- *
357
- * @example
358
- * ```ts
359
- * const removed = await cache.delete('user:1');
360
- * ```
361
- */
362
- /**
363
- * Deletes a cache key.
364
- *
365
- * **Behavior:**
366
- * - Deletes the full key (including any namespace prefix).
367
- * - Returns `true` only when the key existed and was deleted (Redis DEL returned `1`).
368
- *
369
- * **Returns:**
370
- * - `true` if the key existed and was deleted.
371
- *
372
- * **Example:**
373
- * ```ts
374
- * const removed = await cache.delete('user:1');
375
- * // removed === true
376
- * ```
377
- *
378
- * **Parameters:**
379
- * - `key` - Cache key.
380
- * - `namespace` - Optional namespace prefix.
381
- *
382
- * @returns `true` if the key existed and was deleted.
383
- */
384
- delete(key: string, namespace?: string): Promise<boolean>;
385
- /**
386
- * Checks whether a cache key exists.
387
- *
388
- * @param key - Cache key.
389
- * @param namespace - Optional namespace prefix.
390
- * @returns `true` if the key exists.
391
- *
392
- * @example
393
- * ```ts
394
- * const cached = await cache.exists('user:1');
395
- * ```
396
- */
397
- /**
398
- * Checks whether a cache key exists.
399
- *
400
- * **Returns:**
401
- * - `true` if the key exists in Redis.
402
- * - `false` if the key does not exist.
403
- *
404
- * **Example:**
405
- * ```ts
406
- * const cached = await cache.exists('user:1');
407
- * // cached === true when 'user:1' has been set
408
- * ```
409
- *
410
- * **Parameters:**
411
- * - `key` - Cache key.
412
- * - `namespace` - Optional namespace prefix.
413
- *
414
- * @returns `true` if the key exists.
415
- */
416
- exists(key: string, namespace?: string): Promise<boolean>;
417
- /**
418
- * Sets the TTL of an existing cache key.
419
- *
420
- * @param key - Cache key.
421
- * @param ttl - TTL in seconds.
422
- * @param namespace - Optional namespace prefix.
423
- * @returns `true` if the TTL was applied.
424
- *
425
- * @example
426
- * ```ts
427
- * const extended = await cache.expire('session:42', 3600);
428
- * ```
429
- */
430
- /**
431
- * Sets the TTL of an existing cache key.
432
- *
433
- * **Returns:**
434
- * - `true` if the TTL was applied (Redis EXPIRE returned `1`).
435
- * - `false` if the key did not exist.
436
- *
437
- * **Example:**
438
- * ```ts
439
- * const extended = await cache.expire('session:42', 3600);
440
- * // extended === true (TTL was set to 1 hour)
441
- * ```
442
- *
443
- * **Parameters:**
444
- * - `key` - Cache key.
445
- * - `ttl` - TTL in seconds.
446
- * - `namespace` - Optional namespace prefix.
447
- *
448
- * @returns `true` if the TTL was applied.
449
- */
450
- expire(key: string, ttl: number, namespace?: string): Promise<boolean>;
451
- /**
452
- * Returns the remaining TTL of a cache key in seconds.
453
- *
454
- * @param key - Cache key.
455
- * @param namespace - Optional namespace prefix.
456
- * @returns Remaining TTL in seconds (`-2` if missing, `-1` if no TTL).
457
- *
458
- * @example
459
- * ```ts
460
- * const secondsLeft = await cache.ttl('session:42');
461
- * ```
462
- */
463
- /**
464
- * Returns the remaining TTL of a cache key in seconds.
465
- *
466
- * **Returns:**
467
- * - The remaining TTL in seconds.
468
- * - `-2` if the key does not exist.
469
- * - `-1` if the key exists but has no TTL set.
470
- *
471
- * **Example:**
472
- * ```ts
473
- * const secondsLeft = await cache.ttl('session:42');
474
- * // secondsLeft === 2500 (approximately 42 minutes remaining)
475
- * ```
476
- *
477
- * **Parameters:**
478
- * - `key` - Cache key.
479
- * - `namespace` - Optional namespace prefix.
480
- *
481
- * @returns Remaining TTL in seconds (`-2` if missing, `-1` if no TTL).
482
- */
483
- ttl(key: string, namespace?: string): Promise<number>;
484
- /**
485
- * Atomically increments a cache counter.
486
- *
487
- * @param key - Counter key.
488
- * @param by - Amount to increment by (default `1`; ignored by Redis, kept for API parity).
489
- * @param namespace - Optional namespace prefix.
490
- * @returns The new counter value.
491
- *
492
- * @example
493
- * ```ts
494
- * const visits = await cache.increment('stats:visits');
495
- * ```
496
- */
497
- /**
498
- * Atomically increments a cache counter.
499
- *
500
- * **Behavior:**
501
- * - Uses Redis `INCR` command on the full key (including namespace if set).
502
- * - The counter starts at `0` if the key does not exist, then increments to `1`.
503
- * - The `by` parameter is passed to Redis but note: Redis `INCR` always increments
504
- * by `1`. The `by` parameter is kept for API parity with other cache implementations
505
- * but has no effect on the actual Redis command result.
506
- *
507
- * **Returns:**
508
- * - The new counter value (the value after incrementing).
509
- *
510
- * **Example:**
511
- * ```ts
512
- * const visits = await cache.increment('stats:visits');
513
- * // visits === 1 (first increment)
514
- * const more = await cache.increment('stats:visits', 5); // by parameter ignored
515
- * // more === 2
516
- * ```
517
- *
518
- * **Parameters:**
519
- * - `key` - Counter key.
520
- * - `by` - Amount to increment by (default `1`). Note: Redis `INCR` always increments
521
- * by `1`; this parameter is kept for API parity.
522
- * - `namespace` - Optional namespace prefix.
523
- *
524
- * @returns The new counter value.
525
- */
526
- increment(key: string, by?: number, namespace?: string): Promise<number>;
527
- /**
528
- * Atomically decrements a cache counter.
529
- *
530
- * @param key - Counter key.
531
- * @param by - Amount to decrement by (default `1`; ignored by Redis, kept for API parity).
532
- * @param namespace - Optional namespace prefix.
533
- * @returns The new counter value.
534
- *
535
- * @example
536
- * ```ts
537
- * const stock = await cache.decrement('inventory:sku-1');
538
- * ```
539
- */
540
- /**
541
- * Atomically decrements a cache counter.
542
- *
543
- * **Behavior:**
544
- * - Uses Redis `DECR` command on the full key (including namespace if set).
545
- * - The `by` parameter is passed to Redis but note: Redis `DECR` always decrements
546
- * by `1`. The `by` parameter is kept for API parity with other cache implementations
547
- * but has no effect on the actual Redis command result.
548
- *
549
- * **Returns:**
550
- * - The new counter value (the value after decrementing).
551
- *
552
- * **Example:**
553
- * ```ts
554
- * const stock = await cache.decrement('inventory:sku-1');
555
- * // stock === 99 (started at 100, decremented by 1)
556
- * ```
557
- *
558
- * **Parameters:**
559
- * - `key` - Counter key.
560
- * - `by` - Amount to decrement by (default `1`). Note: Redis `DECR` always decrements
561
- * by `1`; this parameter is kept for API parity.
562
- * - `namespace` - Optional namespace prefix.
563
- *
564
- * @returns The new counter value.
565
- */
566
- decrement(key: string, by?: number, namespace?: string): Promise<number>;
567
- /**
568
- * Reads a field from a hash-style cache key.
569
- *
570
- * @param key - Cache key.
571
- * @param field - Hash field.
572
- * @param namespace - Optional namespace prefix.
573
- * @returns The field value (JSON-parsed when possible), or `null`.
574
- *
575
- * @example
576
- * ```ts
577
- * const name = await cache.hget('user:1', 'name');
578
- * ```
579
- */
580
- /**
581
- * Reads a field from a hash-style cache key.
582
- *
583
- * **Behavior:**
584
- * - The field value is retrieved from the Redis hash.
585
- * - When the stored value is a JSON string, it is parsed and returned as the typed result.
586
- * - When the stored value is not JSON, it is returned as a raw string, typed as `T`.
587
- *
588
- * **Returns:**
589
- * - The field value, parsed as `T` when possible, or `null` when the key or field does not exist.
590
- *
591
- * **Example:**
592
- * ```ts
593
- * const name = await cache.hget('user:1', 'name');
594
- * // name === 'alice' | null
595
- * ```
596
- *
597
- * **Parameters:**
598
- * - `key` - Cache key (hash key in Redis).
599
- * - `field` - Hash field to read.
600
- * - `namespace` - Optional namespace prefix. Applied to the key.
601
- *
602
- * @returns The field value, or `null`.
603
- */
604
- hget<T = any>(key: string, field: string, namespace?: string): Promise<T | null>;
605
- /**
606
- * Writes a field into a hash-style cache key.
607
- *
608
- * @param key - Cache key.
609
- * @param field - Hash field.
610
- * @param value - Any serializable value (JSON-stringified unless it is a string).
611
- * @param namespace - Optional namespace prefix.
612
- * @returns `true` if a new field was created.
613
- *
614
- * @example
615
- * ```ts
616
- * await cache.hset('user:1', 'age', 30);
617
- * ```
618
- */
619
- /**
620
- * Writes a field into a hash-style cache key.
621
- *
622
- * **Behavior:**
623
- * - The value is JSON-stringified when it is not a string (objects, arrays, etc.).
624
- * Strings are stored as-is.
625
- * - Returns `true` only when a new field was created (Redis HSET returned `1`).
626
- * If the field already exists, its value is overwritten and `true` is still returned.
627
- *
628
- * **Returns:**
629
- * - `true` if a new field was created.
630
- *
631
- * **Example:**
632
- * ```ts
633
- * await cache.hset('user:1', 'age', 30);
634
- * // Field 'age' set to '30' (stringified) in hash 'user:1'
635
- * ```
636
- *
637
- * **Parameters:**
638
- * - `key` - Cache key (hash key in Redis).
639
- * - `field` - Hash field to write.
640
- * - `value` - Any serializable value. Strings stored as-is; objects are JSON-stringified.
641
- * - `namespace` - Optional namespace prefix. Applied to the key.
642
- *
643
- * @returns `true` if a new field was created.
644
- */
645
- hset(key: string, field: string, value: any, namespace?: string): Promise<boolean>;
646
- /**
647
- * Returns every field of a hash-style cache key.
648
- *
649
- * @param key - Cache key.
650
- * @param namespace - Optional namespace prefix.
651
- * @returns Object mapping fields to values (JSON-parsed when possible).
652
- *
653
- * @example
654
- * ```ts
655
- * const profile = await cache.hgetall('user:1');
656
- * ```
657
- */
658
- /**
659
- * Returns every field of a hash-style cache key.
660
- *
661
- * **Behavior:**
662
- * - Retrieves all fields and values from the Redis hash.
663
- * - Each value is parsed from JSON when possible. Non-JSON values are returned as raw strings.
664
- * - Returns an empty object `{}` when the key does not exist or has no fields.
665
- *
666
- * **Returns:**
667
- * - An object mapping field names to their values, with values parsed as `T` when possible.
668
- *
669
- * **Example:**
670
- * ```ts
671
- * const profile = await cache.hgetall('user:1');
672
- * // profile === { age: 30, name: 'alice' }
673
- * ```
674
- *
675
- * **Parameters:**
676
- * - `key` - Cache key (hash key in Redis).
677
- * - `namespace` - Optional namespace prefix. Applied to the key.
678
- *
679
- * @returns Object mapping fields to values (JSON-parsed when possible).
680
- */
681
- hgetall<T = any>(key: string, namespace?: string): Promise<Record<string, T>>;
682
- /**
683
- * Deletes every cache key matching a glob pattern.
684
- *
685
- * Cluster-safe: scans every node before deleting.
686
- *
687
- * @param pattern - Glob pattern, e.g. `'user:*'`.
688
- * @param namespace - Optional namespace prefix (`namespace:pattern`).
689
- * @returns The number of deleted keys.
690
- *
691
- * @example
692
- * ```ts
693
- * const removed = await cache.deletePattern('temp:*');
694
- * ```
695
- */
696
- /**
697
- * Deletes every cache key matching a glob pattern.
698
- *
699
- * **Behavior:**
700
- * - Cluster-safe: scans every node before deleting.
701
- * - Uses `SCAN` iteratively to avoid blocking the Redis server on large datasets.
702
- * - Each matching key is individually deleted via `DEL`.
703
- * - The `batchSize` and `scanCount` options control the scan batching.
704
- *
705
- * **Returns:**
706
- * - The number of deleted keys.
707
- *
708
- * **Example:**
709
- * ```ts
710
- * const removed = await cache.deletePattern('temp:*');
711
- * // removed === number of keys matching 'temp:*' that were deleted
712
- * ```
713
- *
714
- * **Parameters:**
715
- * - `pattern` - Glob pattern, e.g. `'temp:*'`.
716
- * - `namespace` - Optional namespace prefix. The pattern is transformed to
717
- * `${namespace}:${pattern}` before scanning.
718
- *
719
- * @returns The number of deleted keys.
720
- */
721
- deletePattern(pattern: string, namespace?: string): Promise<number>;
722
- /**
723
- * Lists every cache key matching a glob pattern.
724
- *
725
- * Cluster-safe: scans every node.
726
- *
727
- * @param pattern - Glob pattern, e.g. `'session:*'`.
728
- * @param namespace - Optional namespace prefix (`namespace:pattern`).
729
- * @returns Matching keys.
730
- *
731
- * @example
732
- * ```ts
733
- * const sessions = await cache.keys('session:*');
734
- * ```
735
- */
736
- /**
737
- * Lists every cache key matching a glob pattern.
738
- *
739
- * **Behavior:**
740
- * - Cluster-safe: scans every node.
741
- * - Uses `SCAN` iteratively to avoid blocking the Redis server on large datasets.
742
- * - Returns all keys matching the glob pattern across all nodes (in cluster mode).
743
- *
744
- * **Returns:**
745
- * - An array of matching keys (relative format, without namespace prefix unless
746
- * one was provided in the options).
747
- *
748
- * **Example:**
749
- * ```ts
750
- * const sessions = await cache.keys('session:*');
751
- * // sessions === ['session:1', 'session:2', ...]
752
- * ```
753
- *
754
- * **Parameters:**
755
- * - `pattern` - Glob pattern, e.g. `'session:*'`.
756
- * - `namespace` - Optional namespace prefix. The pattern is transformed to
757
- * `${namespace}:${pattern}` before scanning.
758
- *
759
- * @returns Matching keys.
760
- */
761
- keys(pattern: string, namespace?: string): Promise<string[]>;
762
- /**
763
- * Deletes every key inside a namespace.
764
- *
765
- * @param namespace - Namespace to wipe (`namespace:*`).
766
- * @returns The number of deleted keys.
767
- *
768
- * @example
769
- * ```ts
770
- * const cleared = await cache.clearNamespace('sessions');
771
- * ```
772
- */
773
- /**
774
- * Deletes every key inside a namespace.
775
- *
776
- * **Behavior:**
777
- * - Deletes all keys matching the pattern `*` within the specified namespace.
778
- * - Internally calls {@link deletePattern} with the pattern `*` and the given namespace.
779
- *
780
- * **Returns:**
781
- * - The number of deleted keys.
782
- *
783
- * **Example:**
784
- * ```ts
785
- * const cleared = await cache.clearNamespace('sessions');
786
- * // cleared === number of keys deleted in the 'sessions' namespace
787
- * ```
788
- *
789
- * **Parameters:**
790
- * - `namespace` - Namespace to wipe (e.g. `'sessions'`). Every key of the form
791
- * `namespace:*` will be deleted.
792
- *
793
- * @returns The number of deleted keys.
794
- */
795
- clearNamespace(namespace: string): Promise<number>;
796
- }