crm-api-template-generator/backend/utils/redisClient.js

49 lines
1.4 KiB
JavaScript

const Redis = require("ioredis")
const redis = new Redis(process.env.REDIS_URI)
// Function to set a token with expiration
async function set(key, value, expirationInSeconds) {
await redis.set(key, value, 'EX', expirationInSeconds)
console.log(`Token ${key} set successfully with expiration of ${expirationInSeconds} seconds!`)
}
// Function to get a token
async function get(key) {
const token = await redis.get(key)
if (token === null) {
console.log('Token not found')
} else {
console.log(`Token for ${key}: ${token}`)
return token
}
}
// Function to delete a token
async function del(key) {
const deletedCount = await redis.del(key)
if (deletedCount === 1) {
console.log(`Token ${key} deleted successfully!`)
} else {
console.log('Token not found')
}
}
module.exports = {
set, get, del
}
// // Example usage
// const tokenKey = 'userToken'
// const tokenValue = 'exampleTokenValue'
// const expirationSeconds = 300; // 300 seconds expiration (5 minutes)
// (async () => {
// await setTokenWithExpiration(tokenKey, tokenValue, expirationSeconds)
// await getToken(tokenKey)
// await updateToken(tokenKey, 'newTokenValue')
// await getToken(tokenKey)
// await deleteToken(tokenKey)
// await getToken(tokenKey) // Checking if token is deleted
// redis.quit() // Close Redis connection
// })()