Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

fix: Catch @Cache & @CacheOnInterval errors #234

Merged
merged 2 commits into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/cache/cache-on-interval.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export class CacheOnIntervalService implements OnModuleInit, OnModuleDestroy {
}

private registerCache(instance: any, methodName: string) {
const logger = this.logger;
const methodRef = instance[methodName];
const cacheKey: CacheOnIntervalOptions['key'] = this.reflector.get(CACHE_ON_INTERVAL_KEY, methodRef);
const cacheTimeout: CacheOnIntervalOptions['timeout'] = this.reflector.get(CACHE_ON_INTERVAL_TIMEOUT, methodRef);
Expand All @@ -74,16 +75,24 @@ export class CacheOnIntervalService implements OnModuleInit, OnModuleDestroy {
if (cachedValue) {
return cachedValue;
} else {
const liveData = await methodRef.apply(instance, args);
await cacheManager.set(cacheKey, liveData, { ttl });
return liveData;
try {
const liveData = await methodRef.apply(instance, args);
await cacheManager.set(cacheKey, liveData, { ttl });
return liveData;
} catch (e) {
logger.error(`@CacheOnInterval error for ${instance.constructor.name}#${methodName}`, e);
}
}
};

// Save the interval
const interval = setInterval(async () => {
const liveData = await methodRef.apply(instance);
await cacheManager.set(cacheKey, liveData, { ttl });
try {
const liveData = await methodRef.apply(instance);
await cacheManager.set(cacheKey, liveData, { ttl });
} catch (e) {
logger.error(`@CacheOnInterval error for ${instance.constructor.name}#${methodName}`, e);
}
}, cacheTimeout);
this.intervals.push(interval);
}
Expand Down
17 changes: 12 additions & 5 deletions src/cache/cache.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CACHE_MANAGER, Inject, Injectable, OnModuleInit } from '@nestjs/common';
import { CACHE_MANAGER, Inject, Injectable, Logger, OnModuleInit } from '@nestjs/common';
import { DiscoveryService, MetadataScanner, Reflector } from '@nestjs/core';
import { Cache } from 'cache-manager';
import { isNil } from 'lodash';
Expand All @@ -7,6 +7,8 @@ import { CacheOptions, CACHE_KEY, CACHE_TTL } from './cache.decorator';

@Injectable()
export class CacheService implements OnModuleInit {
private logger = new Logger(CacheService.name);

constructor(
@Inject(CACHE_MANAGER) private readonly cacheManager: Cache,
@Inject(DiscoveryService) private readonly discoveryService: DiscoveryService,
Expand Down Expand Up @@ -39,6 +41,7 @@ export class CacheService implements OnModuleInit {
}

private registerCache(instance: any, methodName: string) {
const logger = this.logger;
const methodRef = instance[methodName];
const rawCacheKey: CacheOptions['key'] = this.reflector.get(CACHE_KEY, methodRef);
const rawCacheTtl: CacheOptions['ttl'] = this.reflector.get(CACHE_TTL, methodRef);
Expand All @@ -59,10 +62,14 @@ export class CacheService implements OnModuleInit {
if (cachedValue) {
return cachedValue;
} else {
const cacheTtl = extractTtl(rawCacheTtl, args);
const liveData = await methodRef.apply(this, args);
await cacheManager.set(cacheKey, liveData, { ttl: cacheTtl });
return liveData;
try {
const cacheTtl = extractTtl(rawCacheTtl, args);
const liveData = await methodRef.apply(this, args);
await cacheManager.set(cacheKey, liveData, { ttl: cacheTtl });
return liveData;
} catch (e) {
logger.error(`@Cache error for ${instance.constructor.name}#${methodName}`, e);
}
}
};
}
Expand Down