Last active
May 12, 2023 12:08
-
-
Save paulwellnerbou/477381069f81eb7d1d7eead3b928d65a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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