Created
November 8, 2022 23:18
-
-
Save rambabusaravanan/cc31e690b56a417bacf7fde76e395674 to your computer and use it in GitHub Desktop.
Toggle caching with "in-memory" / "redis" like h2 database for mysql
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
const { caching } = require('cache-manager'); | |
const { redisStore } = require('cache-manager-redis-yet'); | |
const { redisInsStore } = require('cache-manager-redis-yet'); | |
const { createClient } = require('redis'); | |
const ttl = 5 * 1000; | |
process.env.ENABLE_REDIS = true; | |
async function main() { | |
const redisConfig = { | |
url: 'redis://localhost:6379', | |
ttl: 0, | |
}; | |
const memoryConfig = { | |
max: 100, | |
ttl: 10 * 1000 /*miliseconds*/, | |
} | |
let cache, client; | |
if (process.env.ENABLE_REDIS === 'true') { | |
console.log('caching with redis') | |
// using redis store | |
cache = await caching(redisStore, redisConfig); | |
// using redis client | |
// client = createClient(redisConfig); | |
// await client.connect(); | |
// cache = await caching((c) => redisInsStore(client, c), redisConfig); | |
} else { | |
console.log('caching with memory') | |
cache = await caching('memory', memoryConfig); | |
} | |
// caching with "in-memory" / "redis" using "cache-manager" | |
await cache.set('foo', 'bar', ttl); | |
console.log(await cache.get('foo')); // bar | |
// caching with "redis" using "redis client" | |
// await client.set('fooc', 'barc', { EX: 5 }); | |
// console.log(await client.get('fooc')); // bar | |
// client.disconnect(); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment