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

Commit

Permalink
fix(cache): Log cache not primed erro on only on undefined values fro…
Browse files Browse the repository at this point in the history
…m cache (#405)
  • Loading branch information
immasandwich authored May 9, 2022
1 parent ac3b4d8 commit b17aefa
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/cache/cache-on-interval.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Inject, Injectable, Logger, OnModuleDestroy, OnModuleInit } from '@nestjs/common';
import { DiscoveryService, MetadataScanner, Reflector } from '@nestjs/core';
import Cache from 'file-system-cache';
import { isUndefined } from 'lodash';

import {
CacheOnIntervalOptions,
Expand Down Expand Up @@ -74,20 +75,21 @@ export class CacheOnIntervalService implements OnModuleInit, OnModuleDestroy {
instance[methodName] = async function () {
const cachedValue = await cacheManager.get(cacheKey);

if (cachedValue) {
return cachedValue;
} else {
if (isUndefined(cachedValue)) {
logger.warn(
`@CacheOnInterval has no cache primed for ${instance.constructor.name}#${methodName}. Please wait for a few seconds as the cache is primed.`,
);
}

return cachedValue;
};

let liveData = methodRef.apply(instance);
// Duck typing shennanigans
if (!liveData.then) {
liveData = new Promise(liveData);
}

liveData
.then(d => {
return cacheManager.set(cacheKey, d);
Expand Down

0 comments on commit b17aefa

Please sign in to comment.