Last active
May 12, 2023 12:09
-
-
Save paulwellnerbou/28559280d8a1b3cf3a5c8ab1637d9087 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 { caching, multiCaching } from 'cache-manager'; | |
import { redisStore } from 'cache-manager-redis-yet'; | |
function createMemoryCache(max, ttl) { | |
return caching('memory', { max, ttl }); | |
} | |
async function createRedisCache(redisHost, ttl) { | |
const redisConfig = { url: `redis://${redisHost}`, db: 0, ttl }; | |
const redisCacheStore = await redisStore(redisConfig); | |
return caching(redisCacheStore); | |
} | |
const createCache = async ({ mode, max, ttl }, redisHost) => { | |
if (mode === 'memory') { | |
return createMemoryCache(max, ttl); | |
} | |
if (mode === 'redis' && redisHost) { | |
return createRedisCache(redisHost, ttl); | |
} | |
if (mode === 'memory+redis' && redisHost) { | |
return Promise.all([ | |
createMemoryCache(max, ttl), | |
createRedisCache(redisHost, ttl), | |
]).then(([memoryCache, redisCache]) => multiCaching([memoryCache, redisCache])); | |
} | |
return null; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment