groupon / node-cached

A simple caching library for node.js, inspired by the Play cache API
BSD 3-Clause "New" or "Revised" License
94 stars 15 forks source link

Returning expired cache as a fallback #66

Open terrabythia opened 1 year ago

terrabythia commented 1 year ago

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?