Skip to content

Instantly share code, notes, and snippets.

@paulwellnerbou
Last active May 12, 2023 12:09
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/28559280d8a1b3cf3a5c8ab1637d9087 to your computer and use it in GitHub Desktop.
Save paulwellnerbou/28559280d8a1b3cf3a5c8ab1637d9087 to your computer and use it in GitHub Desktop.
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