node-cached icon indicating copy to clipboard operation
node-cached copied to clipboard

Returning expired cache as a fallback

Open terrabythia opened this issue 3 years ago • 0 comments

Not entirely sure if this isn't already possible, but I couldn't find out how to.

When I use a callback in the getOrElse() function to retrieve new data (and save it), I would like to be able to return the old/expired cache data (if any) whenever there was a problem fetching new data as a fallback. Something like this would be great:

cache.getOrElse(cacheKey, async (oldCache) => {
  try {
    return await fetchNewData();
  }
  catch(error) {
    // return old cache if new data fetch fails
    return oldCache;
  }
});

Is this possible in some other way I'm not aware of?

terrabythia avatar Oct 13 '22 12:10 terrabythia