ioredis-toolkit 0.0.7 → 0.0.9

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/dist/cache.d.ts CHANGED
@@ -20,6 +20,7 @@ export declare class Cache {
20
20
  private logger;
21
21
  private defaultTTL;
22
22
  private compressionThreshold;
23
+ private namespace;
23
24
  /**
24
25
  * Creates a cache bound to a Redis client.
25
26
  *
@@ -31,6 +32,7 @@ export declare class Cache {
31
32
  * applied when no per-call TTL is specified.
32
33
  * - `compressionThreshold` (number, optional, default: `1024`) - Byte threshold
33
34
  * above which values are gzip-compressed transparently.
35
+ * - `namespace` (string, optional, default: `cache`) - Namespace prefix for all keys.
34
36
  * - `logger` - Optional pino-compatible logger. Supports `trace/debug/info/warn/error/fatal`
35
37
  * levels and `child()` for namespace logging. Defaults to `console`.
36
38
  *
@@ -39,7 +41,7 @@ export declare class Cache {
39
41
  *
40
42
  * **Example:**
41
43
  * ```ts
42
- * const cache = new Cache(client, { defaultTTL: 600, compressionThreshold: 2048 });
44
+ * const cache = new Cache(client, { defaultTTL: 600, compressionThreshold: 2048, namespace: "myapp" });
43
45
  * ```
44
46
  */
45
47
  constructor(client: RedisClientWrapper, config: CacheInputConfig, logger?: LoggerLike);
package/dist/cache.js CHANGED
@@ -23,6 +23,7 @@ export class Cache {
23
23
  // private config: RedisConfig;
24
24
  defaultTTL;
25
25
  compressionThreshold;
26
+ namespace;
26
27
  /**
27
28
  * Creates a cache bound to a Redis client.
28
29
  *
@@ -34,6 +35,7 @@ export class Cache {
34
35
  * applied when no per-call TTL is specified.
35
36
  * - `compressionThreshold` (number, optional, default: `1024`) - Byte threshold
36
37
  * above which values are gzip-compressed transparently.
38
+ * - `namespace` (string, optional, default: `cache`) - Namespace prefix for all keys.
37
39
  * - `logger` - Optional pino-compatible logger. Supports `trace/debug/info/warn/error/fatal`
38
40
  * levels and `child()` for namespace logging. Defaults to `console`.
39
41
  *
@@ -42,7 +44,7 @@ export class Cache {
42
44
  *
43
45
  * **Example:**
44
46
  * ```ts
45
- * const cache = new Cache(client, { defaultTTL: 600, compressionThreshold: 2048 });
47
+ * const cache = new Cache(client, { defaultTTL: 600, compressionThreshold: 2048, namespace: "myapp" });
46
48
  * ```
47
49
  */
48
50
  constructor(client, config, logger = defaultLogger) {
@@ -51,6 +53,7 @@ export class Cache {
51
53
  // this.config = config;
52
54
  this.defaultTTL = config.defaultTTL || 3600;
53
55
  this.compressionThreshold = config.compressionThreshold || 1024;
56
+ this.namespace = config.namespace?.trim() || "cache";
54
57
  }
55
58
  async serialize(value) {
56
59
  // Convert to Buffer
@@ -105,7 +108,10 @@ export class Cache {
105
108
  return str;
106
109
  }
107
110
  getKey(key, namespace) {
108
- return namespace ? `${namespace}:${key}` : key;
111
+ if (namespace?.trim()) {
112
+ return `${this.namespace}:${namespace.trim()}:${key}`;
113
+ }
114
+ return `${this.namespace}:${key}`;
109
115
  }
110
116
  /**
111
117
  * Reads a cached value.
@@ -1006,7 +1012,13 @@ export class Cache {
1006
1012
  * @returns The number of deleted keys.
1007
1013
  */
1008
1014
  async deletePattern(pattern, namespace) {
1009
- const fullPattern = namespace ? `${namespace}:${pattern}` : pattern;
1015
+ let fullPattern;
1016
+ if (namespace?.trim()) {
1017
+ fullPattern = `${this.namespace}:${namespace.trim()}:${pattern}`;
1018
+ }
1019
+ else {
1020
+ fullPattern = `${this.namespace}:${pattern}`;
1021
+ }
1010
1022
  let deleted = 0;
1011
1023
  for await (const key of this.client.scanIterator(fullPattern)) {
1012
1024
  const result = await this.client.del(key);
@@ -1055,7 +1067,13 @@ export class Cache {
1055
1067
  * @returns Matching keys.
1056
1068
  */
1057
1069
  async keys(pattern, namespace) {
1058
- const fullPattern = namespace ? `${namespace}:${pattern}` : pattern;
1070
+ let fullPattern;
1071
+ if (namespace?.trim()) {
1072
+ fullPattern = `${this.namespace}:${namespace.trim()}:${pattern}`;
1073
+ }
1074
+ else {
1075
+ fullPattern = `${this.namespace}:${pattern}`;
1076
+ }
1059
1077
  const keys = [];
1060
1078
  for await (const key of this.client.scanIterator(fullPattern)) {
1061
1079
  keys.push(key);
package/dist/client.js CHANGED
@@ -295,6 +295,7 @@ export class RedisClientWrapper {
295
295
  revocationStore: options.revocationStore ?? this.revocationStore,
296
296
  };
297
297
  this._session = createSessionManager(params);
298
+ void this._session.init();
298
299
  this.config.sessionOptions = params;
299
300
  }
300
301
  return this._session;
package/dist/types.d.ts CHANGED
@@ -50,6 +50,7 @@ export type CacheOptions = {
50
50
  export declare const CacheOptionsSchema: z.ZodObject<{
51
51
  defaultTTL: z.ZodDefault<z.ZodNumber>;
52
52
  compressionThreshold: z.ZodDefault<z.ZodNumber>;
53
+ namespace: z.ZodDefault<z.ZodString>;
53
54
  }, z.core.$strip>;
54
55
  export type CacheInputConfig = z.input<typeof CacheOptionsSchema>;
55
56
  export type CacheStats = {
@@ -135,6 +136,7 @@ export declare const BaseRedisConfigSchema: z.ZodObject<{
135
136
  cacheOptions: z.ZodOptional<z.ZodObject<{
136
137
  defaultTTL: z.ZodDefault<z.ZodNumber>;
137
138
  compressionThreshold: z.ZodDefault<z.ZodNumber>;
139
+ namespace: z.ZodDefault<z.ZodString>;
138
140
  }, z.core.$strip>>;
139
141
  slowCommandThreshold: z.ZodDefault<z.ZodNumber>;
140
142
  rateLimit: z.ZodOptional<z.ZodObject<{
@@ -179,6 +181,7 @@ export declare const StandaloneRedisConfigSchema: z.ZodObject<{
179
181
  cacheOptions: z.ZodOptional<z.ZodObject<{
180
182
  defaultTTL: z.ZodDefault<z.ZodNumber>;
181
183
  compressionThreshold: z.ZodDefault<z.ZodNumber>;
184
+ namespace: z.ZodDefault<z.ZodString>;
182
185
  }, z.core.$strip>>;
183
186
  slowCommandThreshold: z.ZodDefault<z.ZodNumber>;
184
187
  rateLimit: z.ZodOptional<z.ZodObject<{
@@ -224,6 +227,7 @@ export declare const SentinelRedisConfigSchema: z.ZodObject<{
224
227
  cacheOptions: z.ZodOptional<z.ZodObject<{
225
228
  defaultTTL: z.ZodDefault<z.ZodNumber>;
226
229
  compressionThreshold: z.ZodDefault<z.ZodNumber>;
230
+ namespace: z.ZodDefault<z.ZodString>;
227
231
  }, z.core.$strip>>;
228
232
  slowCommandThreshold: z.ZodDefault<z.ZodNumber>;
229
233
  rateLimit: z.ZodOptional<z.ZodObject<{
@@ -271,6 +275,7 @@ export declare const ClusterRedisConfigSchema: z.ZodObject<{
271
275
  cacheOptions: z.ZodOptional<z.ZodObject<{
272
276
  defaultTTL: z.ZodDefault<z.ZodNumber>;
273
277
  compressionThreshold: z.ZodDefault<z.ZodNumber>;
278
+ namespace: z.ZodDefault<z.ZodString>;
274
279
  }, z.core.$strip>>;
275
280
  slowCommandThreshold: z.ZodDefault<z.ZodNumber>;
276
281
  rateLimit: z.ZodOptional<z.ZodObject<{
@@ -322,6 +327,7 @@ export declare const RedisConfigInputSchema: z.ZodUnion<readonly [z.ZodObject<{
322
327
  cacheOptions: z.ZodOptional<z.ZodObject<{
323
328
  defaultTTL: z.ZodDefault<z.ZodNumber>;
324
329
  compressionThreshold: z.ZodDefault<z.ZodNumber>;
330
+ namespace: z.ZodDefault<z.ZodString>;
325
331
  }, z.core.$strip>>;
326
332
  slowCommandThreshold: z.ZodDefault<z.ZodNumber>;
327
333
  rateLimit: z.ZodOptional<z.ZodObject<{
@@ -362,6 +368,7 @@ export declare const RedisConfigInputSchema: z.ZodUnion<readonly [z.ZodObject<{
362
368
  cacheOptions: z.ZodOptional<z.ZodObject<{
363
369
  defaultTTL: z.ZodDefault<z.ZodNumber>;
364
370
  compressionThreshold: z.ZodDefault<z.ZodNumber>;
371
+ namespace: z.ZodDefault<z.ZodString>;
365
372
  }, z.core.$strip>>;
366
373
  slowCommandThreshold: z.ZodDefault<z.ZodNumber>;
367
374
  rateLimit: z.ZodOptional<z.ZodObject<{
@@ -403,6 +410,7 @@ export declare const RedisConfigInputSchema: z.ZodUnion<readonly [z.ZodObject<{
403
410
  cacheOptions: z.ZodOptional<z.ZodObject<{
404
411
  defaultTTL: z.ZodDefault<z.ZodNumber>;
405
412
  compressionThreshold: z.ZodDefault<z.ZodNumber>;
413
+ namespace: z.ZodDefault<z.ZodString>;
406
414
  }, z.core.$strip>>;
407
415
  slowCommandThreshold: z.ZodDefault<z.ZodNumber>;
408
416
  rateLimit: z.ZodOptional<z.ZodObject<{
@@ -462,6 +470,7 @@ export declare const RedisConfigSchema: z.ZodPipe<z.ZodUnion<readonly [z.ZodObje
462
470
  cacheOptions: z.ZodOptional<z.ZodObject<{
463
471
  defaultTTL: z.ZodDefault<z.ZodNumber>;
464
472
  compressionThreshold: z.ZodDefault<z.ZodNumber>;
473
+ namespace: z.ZodDefault<z.ZodString>;
465
474
  }, z.core.$strip>>;
466
475
  slowCommandThreshold: z.ZodDefault<z.ZodNumber>;
467
476
  rateLimit: z.ZodOptional<z.ZodObject<{
@@ -502,6 +511,7 @@ export declare const RedisConfigSchema: z.ZodPipe<z.ZodUnion<readonly [z.ZodObje
502
511
  cacheOptions: z.ZodOptional<z.ZodObject<{
503
512
  defaultTTL: z.ZodDefault<z.ZodNumber>;
504
513
  compressionThreshold: z.ZodDefault<z.ZodNumber>;
514
+ namespace: z.ZodDefault<z.ZodString>;
505
515
  }, z.core.$strip>>;
506
516
  slowCommandThreshold: z.ZodDefault<z.ZodNumber>;
507
517
  rateLimit: z.ZodOptional<z.ZodObject<{
@@ -543,6 +553,7 @@ export declare const RedisConfigSchema: z.ZodPipe<z.ZodUnion<readonly [z.ZodObje
543
553
  cacheOptions: z.ZodOptional<z.ZodObject<{
544
554
  defaultTTL: z.ZodDefault<z.ZodNumber>;
545
555
  compressionThreshold: z.ZodDefault<z.ZodNumber>;
556
+ namespace: z.ZodDefault<z.ZodString>;
546
557
  }, z.core.$strip>>;
547
558
  slowCommandThreshold: z.ZodDefault<z.ZodNumber>;
548
559
  rateLimit: z.ZodOptional<z.ZodObject<{
@@ -592,6 +603,7 @@ export declare const RedisConfigSchema: z.ZodPipe<z.ZodUnion<readonly [z.ZodObje
592
603
  cacheOptions?: {
593
604
  defaultTTL: number;
594
605
  compressionThreshold: number;
606
+ namespace: string;
595
607
  } | undefined;
596
608
  rateLimit?: {
597
609
  limit: number;
@@ -630,6 +642,7 @@ export declare const RedisConfigSchema: z.ZodPipe<z.ZodUnion<readonly [z.ZodObje
630
642
  cacheOptions?: {
631
643
  defaultTTL: number;
632
644
  compressionThreshold: number;
645
+ namespace: string;
633
646
  } | undefined;
634
647
  rateLimit?: {
635
648
  limit: number;
@@ -666,6 +679,7 @@ export declare const RedisConfigSchema: z.ZodPipe<z.ZodUnion<readonly [z.ZodObje
666
679
  cacheOptions?: {
667
680
  defaultTTL: number;
668
681
  compressionThreshold: number;
682
+ namespace: string;
669
683
  } | undefined;
670
684
  rateLimit?: {
671
685
  limit: number;
@@ -702,6 +716,7 @@ export declare const RedisConfigSchema: z.ZodPipe<z.ZodUnion<readonly [z.ZodObje
702
716
  cacheOptions?: {
703
717
  defaultTTL: number;
704
718
  compressionThreshold: number;
719
+ namespace: string;
705
720
  } | undefined;
706
721
  rateLimit?: {
707
722
  limit: number;
@@ -743,6 +758,7 @@ export declare const RedisConfigSchema: z.ZodPipe<z.ZodUnion<readonly [z.ZodObje
743
758
  cacheOptions?: {
744
759
  defaultTTL: number;
745
760
  compressionThreshold: number;
761
+ namespace: string;
746
762
  } | undefined;
747
763
  rateLimit?: {
748
764
  limit: number;
@@ -781,6 +797,7 @@ export declare const RedisConfigSchema: z.ZodPipe<z.ZodUnion<readonly [z.ZodObje
781
797
  cacheOptions?: {
782
798
  defaultTTL: number;
783
799
  compressionThreshold: number;
800
+ namespace: string;
784
801
  } | undefined;
785
802
  rateLimit?: {
786
803
  limit: number;
package/dist/types.js CHANGED
@@ -7,6 +7,7 @@ export const DistributedLockOptionsSchema = z.object({
7
7
  export const CacheOptionsSchema = z.object({
8
8
  defaultTTL: z.number().int().min(0).default(3_600),
9
9
  compressionThreshold: z.number().int().min(1).default(1_024),
10
+ namespace: z.string().min(1).default("cache"),
10
11
  });
11
12
  // ============================================================================
12
13
  // Rate Limiting
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ioredis-toolkit",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
4
4
  "description": "Production-grade, type-safe Redis infrastructure for standalone, Sentinel, and Cluster deployments",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",