Thursday, June 13, 2024

Clear cache || FlushDB || Clear Redis Cache ||Node js ||aws

Clear cache || FlushDB || Clear Redis Cache ||Node js ||aws 



const express = require('express');

const redis = require('redis'); const app = express(); const port = 3000; // Replace with your actual hostname and port const client = redis.createClient({ host: 'your-elasticache-hostname', port: your-elasticache-port }); client.on('error', (err) => { console.log("Error " + err); }); app.get('/flushall', (req, res) => { client.flushdb((err, succeeded) => { if (err) { res.status(500).send({ error: 'Failed to flush Redis cache' }); } else { res.send({ message: 'Redis cache successfully flushed' }); } }); }); app.listen(port, () => { console.log(`App running on port ${port}`); });

===============================
Method 2: with lamda 

const Redis = require('ioredis'); exports.handler = async (event, context) => { const redis = new Redis({ host: 'YOUR_REDIS_HOST', // replace with your host port: YOUR_REDIS_PORT, // replace with your port password: 'YOUR_REDIS_PASSWORD', // replace with your password (if any) db: 0, }); try { // Ping Redis to check connection const ping = await redis.ping(); console.log('Ping:', ping); // Clear all keys await redis.flushdb(); console.log('Cache cleared'); return { statusCode: 200, body: 'Cache cleared' }; } catch (error) { console.log('Error:', error); return { statusCode: 500, body: 'Error clearing cache' }; } finally { // Disconnect from Redis await redis.quit(); } };

=============================================
Methos 3: with redis dependency: const redis = require('redis'); exports.handler = async (event, context) => { return new Promise((resolve, reject) => { const client = redis.createClient({ host: 'YOUR_REDIS_HOST', // replace with your host port: YOUR_REDIS_PORT, // replace with your port password: 'YOUR_REDIS_PASSWORD', // replace with your password (if any) db: 0, }); client.on('connect', function() { console.log('Connected to Redis'); client.flushdb(function (err, succeeded) { if (err) { console.error('Error:', err); reject({ statusCode: 500, body: 'Error clearing cache' }); } else { console.log('Cache cleared'); resolve({ statusCode: 200, body: 'Cache cleared' }); } // Disconnect from Redis client.quit(); }); }); client.on('error', function (err) { console.error('Error:', err); reject({ statusCode: 500, body: 'Error clearing cache' }); }); }); };

No comments:

Post a Comment

Create a Digital Clock using HTML and JavaScript

Create a Digital Clock using HTML and JavaScript  <! DOCTYPE html> < html > < head > ...

Followers

Search This Blog

Popular Posts