Skip to content

Instantly share code, notes, and snippets.

@paulwellnerbou
Last active May 12, 2023 12:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulwellnerbou/477381069f81eb7d1d7eead3b928d65a to your computer and use it in GitHub Desktop.
Save paulwellnerbou/477381069f81eb7d1d7eead3b928d65a to your computer and use it in GitHub Desktop.
import { NoCacheableError, redisStore } from 'cache-manager-redis-yet';
const cachePromise = createCache(); // this method is creating the actual cache promise object (see other code snippets)
const CacheWrapper = {
get: async (key) => {
if (cachePromise) {
const cache = await cachePromise;
return cache.get(key);
}
return null;
},
set: (key, value) => {
if (cachePromise) {
cachePromise.then(cache => cache.set(key, value));
}
},
wrap: async (key, fn, ttl) => {
if (cachePromise) {
const cache = await cachePromise;
return cache.wrap(key, fn, ttl).catch((e) => {
if (!(e instanceof NoCacheableError)) throw e;
});
}
return new Promise(fn());
},
isCacheEnabled: () => cachePromise !== null,
};
export default CacheWrapper;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment