This commit is contained in:
iPromKnight
2024-02-07 11:37:59 +00:00
committed by iPromKnight
parent 0f3e20eef3
commit 6e2b776211

View File

@@ -1,4 +1,4 @@
import {Cache, createCache, memoryStore} from 'cache-manager'; import {Cache, createCache, MemoryCache, memoryStore, Store} from 'cache-manager';
import {mongoDbStore} from '@tirke/node-cache-manager-mongodb' import {mongoDbStore} from '@tirke/node-cache-manager-mongodb'
import {configurationService} from './configuration_service'; import {configurationService} from './configuration_service';
import {CacheType} from "../enums/cache_types"; import {CacheType} from "../enums/cache_types";
@@ -23,9 +23,12 @@ export type CacheMethod = () => any;
@injectable() @injectable()
export class CacheService implements ICacheService { export class CacheService implements ICacheService {
private logger: ILoggingService; private logger: ILoggingService;
private readonly memoryCache: MemoryCache;
private readonly remoteCache: Cache<Store> | MemoryCache;
constructor(@inject(IocTypes.ILoggingService) logger: ILoggingService) { constructor(@inject(IocTypes.ILoggingService) logger: ILoggingService) {
this.logger = logger; this.logger = logger;
if (!configurationService.cacheConfig.NO_CACHE) { if (configurationService.cacheConfig.NO_CACHE) {
this.logger.info('Cache is disabled'); this.logger.info('Cache is disabled');
return; return;
} }
@@ -49,7 +52,7 @@ export class CacheService implements ICacheService {
private initiateMemoryCache = () => private initiateMemoryCache = () =>
createCache(memoryStore(), { createCache(memoryStore(), {
ttl: MEMORY_TTL ttl: MEMORY_TTL
}) as Cache; });
private initiateMongoCache = () => { private initiateMongoCache = () => {
const store = mongoDbStore({ const store = mongoDbStore({
@@ -76,7 +79,7 @@ export class CacheService implements ICacheService {
return configurationService.cacheConfig.MONGO_URI ? this.initiateMongoCache() : this.initiateMemoryCache(); return configurationService.cacheConfig.MONGO_URI ? this.initiateMongoCache() : this.initiateMemoryCache();
} }
private getCacheType = (cacheType: CacheType): typeof this.memoryCache | null => { private getCacheType = (cacheType: CacheType): MemoryCache | Cache<Store> => {
switch (cacheType) { switch (cacheType) {
case CacheType.Memory: case CacheType.Memory:
return this.memoryCache; return this.memoryCache;
@@ -87,9 +90,6 @@ export class CacheService implements ICacheService {
} }
} }
private readonly memoryCache: Cache;
private readonly remoteCache: Cache;
private cacheWrap = async ( private cacheWrap = async (
cacheType: CacheType, key: string, method: CacheMethod, options: ICacheOptions): Promise<any> => { cacheType: CacheType, key: string, method: CacheMethod, options: ICacheOptions): Promise<any> => {
const cache = this.getCacheType(cacheType); const cache = this.getCacheType(cacheType);