@tc-libs/app-cache 0.9.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 ADDED
@@ -0,0 +1,11 @@
1
+ # app-cache
2
+
3
+ This library was generated with [Nx](https://nx.dev).
4
+
5
+ ## Building
6
+
7
+ Run `nx build app-cache` to build the library.
8
+
9
+ ## Running unit tests
10
+
11
+ Run `nx test app-cache` to execute the unit tests via [Jest](https://jestjs.io).
package/package.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "@tc-libs/app-cache",
3
+ "version": "0.9.0",
4
+ "dependencies": {
5
+ "tslib": "^2.3.0"
6
+ },
7
+ "type": "commonjs",
8
+ "main": "./src/index.js",
9
+ "typings": "./src/index.d.ts",
10
+ "types": "./src/index.d.ts"
11
+ }
@@ -0,0 +1,6 @@
1
+ import { DynamicModule } from '@nestjs/common';
2
+ import AppCacheModuleAsyncOptions, { AppCacheModuleOptions } from './app-cache.module.options';
3
+ export declare class AppCacheModule {
4
+ static register(enabled: boolean, options: AppCacheModuleOptions, isGlobal?: boolean): DynamicModule;
5
+ static registerAsync(enabled: boolean, options: AppCacheModuleAsyncOptions, isGlobal?: boolean): DynamicModule;
6
+ }
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AppCacheModule = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const cache_manager_1 = require("@nestjs/cache-manager");
6
+ const cache_manager_redis_store_1 = tslib_1.__importDefault(require("cache-manager-redis-store"));
7
+ const app_cache_service_1 = require("./app-cache.service");
8
+ const app_cache_module_options_1 = require("./app-cache.module.options");
9
+ class AppCacheModule {
10
+ static register(enabled, options, isGlobal) {
11
+ const imports = [];
12
+ if (enabled) {
13
+ imports.push(cache_manager_1.CacheModule.register({
14
+ store: cache_manager_redis_store_1.default,
15
+ host: options.server.host,
16
+ port: options.server.port,
17
+ ttl: options.ttl,
18
+ isGlobal: true,
19
+ }));
20
+ }
21
+ else {
22
+ imports.push(cache_manager_1.CacheModule.register({
23
+ isGlobal: true,
24
+ }));
25
+ }
26
+ return {
27
+ module: AppCacheModule,
28
+ providers: [
29
+ { provide: app_cache_module_options_1.APP_CACHE_MODULE_OPTIONS, useValue: options },
30
+ app_cache_service_1.AppCacheService,
31
+ ],
32
+ exports: [app_cache_service_1.AppCacheService],
33
+ controllers: [],
34
+ imports,
35
+ global: isGlobal,
36
+ };
37
+ }
38
+ static registerAsync(enabled, options, isGlobal) {
39
+ const imports = [];
40
+ if (enabled) {
41
+ imports.push(cache_manager_1.CacheModule.registerAsync({
42
+ imports: options.imports,
43
+ useFactory: async (...args) => {
44
+ const appCacheOptions = await options.useFactory(...args);
45
+ return {
46
+ store: cache_manager_redis_store_1.default,
47
+ host: appCacheOptions.server.host,
48
+ port: appCacheOptions.server.port,
49
+ ttl: appCacheOptions.ttl,
50
+ isGlobal: true,
51
+ };
52
+ },
53
+ }));
54
+ }
55
+ else {
56
+ imports.push(cache_manager_1.CacheModule.register({
57
+ isGlobal: true,
58
+ }));
59
+ }
60
+ return {
61
+ module: AppCacheModule,
62
+ imports: [...(options.imports || []), ...imports],
63
+ providers: [
64
+ {
65
+ provide: app_cache_module_options_1.APP_CACHE_MODULE_OPTIONS,
66
+ useFactory: options.useFactory,
67
+ inject: options.inject,
68
+ },
69
+ app_cache_service_1.AppCacheService,
70
+ ],
71
+ exports: [app_cache_service_1.AppCacheService],
72
+ global: isGlobal,
73
+ };
74
+ }
75
+ }
76
+ exports.AppCacheModule = AppCacheModule;
77
+ //# sourceMappingURL=app-cache.module.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"app-cache.module.js","sourceRoot":"","sources":["../../../../packages/app-cache/src/app-cache.module.ts"],"names":[],"mappings":";;;;AAAA,yDAAoD;AAEpD,kGAAmD;AACnD,2DAAsD;AACtD,yEAGoC;AAEpC,MAAa,cAAc;IACzB,MAAM,CAAC,QAAQ,CACb,OAAgB,EAChB,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C,IAAI;wBACjC,GAAG,EAAE,eAAe,CAAC,GAAG;wBACxB,QAAQ,EAAE,IAAI;qBACf,CAAC;gBACJ,CAAC;aACF,CAAC,CACH,CAAC;QACJ,CAAC;aAAM,CAAC;YACN,OAAO,CAAC,IAAI,CACV,2BAAW,CAAC,QAAQ,CAAC;gBACnB,QAAQ,EAAE,IAAI;aACf,CAAC,CACH,CAAC;QACJ,CAAC;QAED,OAAO;YACL,MAAM,EAAE,cAAc;YACtB,OAAO,EAAE,CAAC,GAAG,CAAC,OAAO,CAAC,OAAO,IAAI,EAAE,CAAC,EAAE,GAAG,OAAO,CAAC;YACjD,SAAS,EAAE;gBACT;oBACE,OAAO,EAAE,mDAAwB;oBACjC,UAAU,EAAE,OAAO,CAAC,UAAU;oBAC9B,MAAM,EAAE,OAAO,CAAC,MAAM;iBACvB;gBACD,mCAAe;aAChB;YACD,OAAO,EAAE,CAAC,mCAAe,CAAC;YAC1B,MAAM,EAAE,QAAQ;SACjB,CAAC;IACJ,CAAC;CACF;AA9FD,wCA8FC"}
@@ -0,0 +1,13 @@
1
+ import { ModuleMetadata } from '@nestjs/common';
2
+ import { FactoryProvider } from '@nestjs/common/interfaces/modules/provider.interface';
3
+ export declare const APP_CACHE_MODULE_OPTIONS = "APP_CACHE_MODULE_OPTIONS";
4
+ export interface AppCacheModuleOptions {
5
+ ttl: number;
6
+ server: {
7
+ host: string;
8
+ port: number;
9
+ };
10
+ enabled: boolean;
11
+ }
12
+ type AppCacheModuleAsyncOptions = Pick<ModuleMetadata, 'imports'> & Pick<FactoryProvider<AppCacheModuleOptions>, 'useFactory' | 'inject'>;
13
+ export default AppCacheModuleAsyncOptions;
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.APP_CACHE_MODULE_OPTIONS = void 0;
4
+ exports.APP_CACHE_MODULE_OPTIONS = 'APP_CACHE_MODULE_OPTIONS';
5
+ //# sourceMappingURL=app-cache.module.options.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"app-cache.module.options.js","sourceRoot":"","sources":["../../../../packages/app-cache/src/app-cache.module.options.ts"],"names":[],"mappings":";;;AAGa,QAAA,wBAAwB,GAAG,0BAA0B,CAAC"}
@@ -0,0 +1,20 @@
1
+ import { Logger } from '@nestjs/common';
2
+ import { HashService } from '@tc-libs/helper';
3
+ import { Cache } from 'cache-manager';
4
+ import { AppCacheModuleOptions } from './app-cache.module.options';
5
+ import { ICachingOptions } from './interfaces/app-cache.options.interface';
6
+ export declare class AppCacheService {
7
+ private _options;
8
+ protected _hash: HashService;
9
+ protected _cache: Cache;
10
+ private _cacheTTL;
11
+ private _cacheEnabled;
12
+ protected readonly _logger: Logger;
13
+ constructor(_options: AppCacheModuleOptions, _hash: HashService, _cache: Cache);
14
+ private _createKeyByQuery;
15
+ getCachedDataByQuery<T>(functionName: string, find?: any, options?: any): Promise<T | null | undefined>;
16
+ getCachedDataByKey<T>(key: string): Promise<T | null | undefined>;
17
+ saveToCache(data: any, options?: {
18
+ caching?: ICachingOptions;
19
+ }): void;
20
+ }
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ var AppCacheService_1;
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.AppCacheService = void 0;
5
+ const tslib_1 = require("tslib");
6
+ const common_1 = require("@nestjs/common");
7
+ const helper_1 = require("@tc-libs/helper");
8
+ const cache_manager_1 = require("@nestjs/cache-manager");
9
+ const app_cache_module_options_1 = require("./app-cache.module.options");
10
+ let AppCacheService = AppCacheService_1 = class AppCacheService {
11
+ constructor(_options, _hash, _cache) {
12
+ this._options = _options;
13
+ this._hash = _hash;
14
+ this._cache = _cache;
15
+ this._logger = new common_1.Logger(AppCacheService_1.name);
16
+ this._cacheTTL = this._options.ttl;
17
+ this._cacheEnabled = this._options.enabled;
18
+ }
19
+ _createKeyByQuery(functionName, find, options) {
20
+ if (options.caching.key)
21
+ return;
22
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
23
+ const { caching, returnPlain, session, ...opts } = options;
24
+ const keyObject = {
25
+ ...(find || {}),
26
+ ...opts,
27
+ };
28
+ options.caching.key = `${functionName}-${this._hash.sha1Base64(JSON.stringify(keyObject))}`;
29
+ }
30
+ async getCachedDataByQuery(functionName, find, options) {
31
+ if (!(this._cacheEnabled && options?.caching?.enabled))
32
+ return null;
33
+ this._createKeyByQuery(functionName, find, options);
34
+ const x = await this._cache.get(options.caching.key);
35
+ if (x)
36
+ this._logger.debug(`[CACHE] key [${options.caching.key}] HIT`);
37
+ else
38
+ this._logger.debug(`[CACHE] key [${options.caching.key}] MISS`);
39
+ return x;
40
+ }
41
+ async getCachedDataByKey(key) {
42
+ if (!this._cacheEnabled)
43
+ return null;
44
+ const x = await this._cache.get(key);
45
+ if (x)
46
+ this._logger.debug(`[CACHE] key [${key}] HIT`);
47
+ else
48
+ this._logger.debug(`[CACHE] key [${key}] MISS`);
49
+ return x;
50
+ }
51
+ saveToCache(data, options) {
52
+ if (!options)
53
+ return;
54
+ const { caching } = options;
55
+ if (!this._cacheEnabled || !caching?.enabled)
56
+ return;
57
+ const ttl = caching.ttl || this._cacheTTL;
58
+ this._logger.debug(`[CACHE] key [${caching.key}] SET with ttl [${ttl}]`);
59
+ this._cache.set(caching.key, data, { ttl });
60
+ }
61
+ };
62
+ exports.AppCacheService = AppCacheService;
63
+ exports.AppCacheService = AppCacheService = AppCacheService_1 = tslib_1.__decorate([
64
+ (0, common_1.Injectable)(),
65
+ tslib_1.__param(0, (0, common_1.Inject)(app_cache_module_options_1.APP_CACHE_MODULE_OPTIONS)),
66
+ tslib_1.__param(2, (0, common_1.Inject)(cache_manager_1.CACHE_MANAGER)),
67
+ tslib_1.__metadata("design:paramtypes", [Object, helper_1.HashService, Object])
68
+ ], AppCacheService);
69
+ //# sourceMappingURL=app-cache.service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"app-cache.service.js","sourceRoot":"","sources":["../../../../packages/app-cache/src/app-cache.service.ts"],"names":[],"m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IAD3B,IAAA,mBAAU,GAAE;IAOR,mBAAA,IAAA,eAAM,EAAC,mDAAwB,CAAC,CAAA;IAGhC,mBAAA,IAAA,eAAM,EAAC,6BAAa,CAAC,CAAA;qDADL,oBAAW;GARnB,eAAe,CA0D3B"}
package/src/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './app-cache.module';
2
+ export * from './app-cache.service';
3
+ export * from './interfaces/app-cache.options.interface';
package/src/index.js ADDED
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./app-cache.module"), exports);
5
+ tslib_1.__exportStar(require("./app-cache.service"), exports);
6
+ tslib_1.__exportStar(require("./interfaces/app-cache.options.interface"), exports);
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/app-cache/src/index.ts"],"names":[],"mappings":";;;AAAA,6DAAmC;AACnC,8DAAoC;AACpC,mFAAyD"}
@@ -0,0 +1,5 @@
1
+ export interface ICachingOptions {
2
+ enabled: boolean;
3
+ key: string;
4
+ ttl?: number;
5
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=app-cache.options.interface.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"app-cache.options.interface.js","sourceRoot":"","sources":["../../../../../packages/app-cache/src/interfaces/app-cache.options.interface.ts"],"names":[],"mappings":""}