@mondart/nestjs-common-module 2.2.3 → 2.2.5

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.
@@ -1,10 +1,11 @@
1
1
  import { ModuleMetadata, Type } from '@nestjs/common';
2
2
  export declare const CACHING_MODULE_OPTIONS = "CACHING_MODULE_OPTIONS";
3
3
  export interface CachingModuleOptions {
4
- ttl: number;
5
4
  host: string;
6
5
  port: number;
7
6
  password: string;
7
+ namespace?: string;
8
+ ttl?: number;
8
9
  }
9
10
  export interface CachingModuleFactory {
10
11
  createModuleOptions: () => Promise<CachingModuleOptions> | CachingModuleOptions;
@@ -12,6 +12,7 @@ const common_1 = require("@nestjs/common");
12
12
  const caching_service_1 = require("./caching.service");
13
13
  const cache_manager_1 = require("@nestjs/cache-manager");
14
14
  const caching_interface_1 = require("./caching.interface");
15
+ const redis_1 = require("@keyv/redis");
15
16
  let CachingModule = CachingModule_1 = class CachingModule {
16
17
  static register(options) {
17
18
  return {
@@ -20,10 +21,17 @@ let CachingModule = CachingModule_1 = class CachingModule {
20
21
  cache_manager_1.CacheModule.register({
21
22
  isGlobal: true,
22
23
  useFactory: async (cachingOptions) => ({
23
- host: cachingOptions.host,
24
- port: cachingOptions.port,
25
- password: cachingOptions.password,
26
- ttl: cachingOptions.ttl,
24
+ stores: [
25
+ new redis_1.Keyv({
26
+ store: new redis_1.default({
27
+ url: `redis://${cachingOptions.host}:${cachingOptions.port}`,
28
+ password: cachingOptions.password,
29
+ }),
30
+ }, {
31
+ namespace: cachingOptions.namespace,
32
+ ttl: cachingOptions.ttl,
33
+ }),
34
+ ],
27
35
  }),
28
36
  }),
29
37
  ],
@@ -47,11 +55,17 @@ let CachingModule = CachingModule_1 = class CachingModule {
47
55
  cache_manager_1.CacheModule.registerAsync({
48
56
  imports: [asyncOptionsProvider],
49
57
  useFactory: async (cachingOptions) => ({
50
- host: cachingOptions.host,
51
- port: cachingOptions.port,
52
- password: cachingOptions.password,
53
- ttl: cachingOptions.ttl,
54
- max: 10000,
58
+ stores: [
59
+ new redis_1.Keyv({
60
+ store: new redis_1.default({
61
+ url: `redis://${cachingOptions.host}:${cachingOptions.port}`,
62
+ password: cachingOptions.password,
63
+ }),
64
+ }, {
65
+ namespace: cachingOptions.namespace,
66
+ ttl: cachingOptions.ttl,
67
+ }),
68
+ ],
55
69
  }),
56
70
  inject: [caching_interface_1.CACHING_MODULE_OPTIONS],
57
71
  }),
@@ -1,10 +1,12 @@
1
1
  import { Cache } from 'cache-manager';
2
+ import { Keyv } from '@keyv/redis';
2
3
  export declare class CachingService {
3
4
  private cacheManager;
5
+ private readonly logger;
4
6
  constructor(cacheManager: Cache);
5
- getStores(): import("keyv").Keyv<any>[];
7
+ getStore(): Keyv<any>;
6
8
  get<T>(key: string): Promise<T | undefined>;
7
- getMany<T>(keys: string[]): Promise<T[] | undefined>;
9
+ getMany<T>(keys?: string[]): Promise<T[] | undefined>;
8
10
  set<T>(key: string, value: T, ttlSeconds?: number): Promise<string>;
9
11
  setMany<T>(list: {
10
12
  key: string;
@@ -11,16 +11,30 @@ var __metadata = (this && this.__metadata) || function (k, v) {
11
11
  var __param = (this && this.__param) || function (paramIndex, decorator) {
12
12
  return function (target, key) { decorator(target, key, paramIndex); }
13
13
  };
14
+ var CachingService_1;
14
15
  Object.defineProperty(exports, "__esModule", { value: true });
15
16
  exports.CachingService = void 0;
16
17
  const common_1 = require("@nestjs/common");
17
18
  const cache_manager_1 = require("@nestjs/cache-manager");
18
- let CachingService = class CachingService {
19
+ const redis_1 = require("@keyv/redis");
20
+ let CachingService = CachingService_1 = class CachingService {
19
21
  constructor(cacheManager) {
20
22
  this.cacheManager = cacheManager;
23
+ this.logger = new common_1.Logger(CachingService_1.name, {
24
+ timestamp: true,
25
+ });
26
+ if (this.cacheManager.stores?.length === 0) {
27
+ throw new Error('Cache manager is not initialized');
28
+ }
29
+ if (!(this.cacheManager.stores?.[0] instanceof redis_1.Keyv)) {
30
+ throw new Error('Cache manager is not a Keyv instance');
31
+ }
32
+ this.cacheManager.stores[0].opts.store.on('error', (error) => {
33
+ this.logger.error('Redis error:', error);
34
+ });
21
35
  }
22
- getStores() {
23
- return this.cacheManager.stores;
36
+ getStore() {
37
+ return this.cacheManager.stores?.[0];
24
38
  }
25
39
  async get(key) {
26
40
  const result = await this.cacheManager.get(key);
@@ -64,7 +78,7 @@ let CachingService = class CachingService {
64
78
  }
65
79
  };
66
80
  exports.CachingService = CachingService;
67
- exports.CachingService = CachingService = __decorate([
81
+ exports.CachingService = CachingService = CachingService_1 = __decorate([
68
82
  (0, common_1.Injectable)(),
69
83
  __param(0, (0, common_1.Inject)(cache_manager_1.CACHE_MANAGER)),
70
84
  __metadata("design:paramtypes", [Object])