import redis from 'redis';
// Create a Redis client with retry strategy const client = redis.createClient({ host: 'localhost', // Replace with your Redis server host port: 6379, // Replace with your Redis server port retry_strategy: function (options) { // options.error contains the error object returned by the last attempt to connect if (options.error && options.error.code === 'ECONNREFUSED') { // If the connection was refused by the server, log the error and stop retrying console.error('The server refused the connection'); return new Error('The server refused the connection'); } // options.total_retry_time is the total time (in milliseconds) that the client has been trying to reconnect if (options.total_retry_time > 1000 * 60 * 60) { // If the total retry time exceeds 1 hour, log the error and stop retrying console.error('Retry time exhausted'); return new Error('Retry time exhausted'); } // options.attempt is the number of retry attempts so far if (options.attempt > 10) { // If the number of retry attempts exceeds 10, log the error and stop retrying console.error('Too many retry attempts'); return undefined; } // Reconnect after a specific time, which increases with each attempt // options.attempt * 100 gives a delay that increases by 100ms with each attempt // Math.min ensures the delay does not exceed 3000ms (3 seconds) return Math.min(options.attempt * 100, 3000); } }); // Event listener for successful connection client.on('connect', function() { console.log('Redis client connected'); }); // Event listener for errors client.on('error', function (err) { console.error('Something went wrong ' + err); }); export default client;Sidebar content
Wednesday, July 10, 2024
Redis connection in node js
import redis from 'redis';
// Create a Redis client with retry strategy const client = redis.createClient({ host: 'localhost', // Replace with your Redis server host port: 6379, // Replace with your Redis server port retry_strategy: function (options) { if (options.error && options.error.code === 'ECONNREFUSED') { // End reconnecting on a specific error and flush all commands with a individual error console.error('The server refused the connection'); return new Error('The server refused the connection'); } if (options.total_retry_time > 1000 * 60 * 60) { // End reconnecting after a specific timeout and flush all commands with a individual error console.error('Retry time exhausted'); return new Error('Retry time exhausted'); } if (options.attempt > 10) { // End reconnecting with built in error console.error('Too many retry attempts'); return undefined; } // Reconnect after a specific time return Math.min(options.attempt * 100, 3000); } }); client.on('connect', function() { console.log('Redis client connected'); }); client.on('error', function (err) { console.error('Something went wrong ' + err); }); export default client;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}`); });Sunday, May 19, 2024
Friday, February 16, 2024
what is meant by --max-request-journal-entries and --no-request-journal in wiremock configuration
max-request-journal-entries and no-request-journal in wiremock configuration
In WireMock, the request journal is a built-in feature that keeps a record of incoming requests and their corresponding responses. It can be helpful for debugging and analysis purposes.
--max-request-journal-entries is an option that allows you to set a limit on the number of requests that the request journal stores. When this limit is reached, older requests will be removed from the journal to make room for new ones. By setting this option, you can control the memory usage of the request journal.
For example, using --max-request-journal-entries=10000 will limit the request journal to store a maximum of 10,000 requests.
--no-request-journal is an option that disables the request journal entirely. When this option is used, WireMock will not store any requests or responses in the request journal. Disabling the request journal can help reduce memory consumption and improve performance, especially during load testing or in production environments where request logging is not necessary.
In summary:
--max-request-journal-entries: Sets a limit on the number of requests stored in the request journal.
--no-request-journal: Disables the request journal completely.
Thursday, January 18, 2024
Sort an array of 0s, 1s and 2s | Dutch National Flag problem
Sort an array of 0s, 1s and 2s |
Dutch National Flag problem
👀👀👀👀👀
/*
* This program defines a sortArray() method that sorts
* an array of 0s, 1s, and 2s using
* the Dutch National Flag algorithm.
*/
public class Sort012 {
public static void swap(int[] arr,int i,int j) {
int temp;
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
public static void sortArray(int[] arr) {
int low =0;
int mid =0;
int high =arr.length-1;
while(mid<=high) {
switch (arr[mid]) {
case 0:
swap(arr,low,mid);
low++;
mid++;
break;
case 1:
mid++;
break;
case 2:
swap(arr,mid,high);
high--;
break;
default:
break;
}
}
}
public static void main(String... aa) {
int arr[]= {2,0,1,0,2,1,2,0,1,0,2,1};
sortArray(arr);
for(int i:arr) {
System.out.print(i+" ");
}
}
}
Output:
0 0 0 0 1 1 1 1 2 2 2 2
Wednesday, January 17, 2024
Merge two sorted linked lists
Merge two sorted linked lists
public class MergeLinkedLists {
static class ListNode {
int val;
ListNode next;
public ListNode() {}
public ListNode(int val) {
this.val = val;
}
public ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
public static ListNode mergeTwoLists(ListNode list1, ListNode list2) {
ListNode mergedList = new ListNode();
ListNode current = mergedList;
while (list1 != null && list2 != null) {
if (list1.val <= list2.val) {
current.next = list1;
list1 = list1.next;
} else {
current.next = list2;
list2 = list2.next;
}
current = current.next;
}
if (list1 != null) {
current.next = list1;
} else {
current.next = list2;
}
return mergedList.next;
}
public static void main(String[] args) {
// Example usage:
ListNode list1 = new ListNode(1, new ListNode(2, new ListNode(4)));
ListNode list2 = new ListNode(1, new ListNode(3, new ListNode(4)));
ListNode mergedList = mergeTwoLists(list1, list2);
while (mergedList != null) {
System.out.print(mergedList.val + " ");
mergedList = mergedList.next;
}
}
}
Time Complexity: O(M + N), Where M and N are the size of the list1 and list2 respectively.
Auxiliary Space: O(M+N), Function call stack space
Output:
1 1 2 3 4 4
Geeks link:
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
-
Software Development Tools Presentation From the moment you begin developing software, whether as a freelancer for a startup or wo...
-
CDAC Certifications Courses List IT Applications Certificate Course in Business Computing Certificate Course in Global su...
-
Merge two sorted linked lists public class MergeLinkedLists { static class ListNode { int val ; ListNode next ; ...
-
const redis = require('redis'); const { promisify } = require('util'); // Create a Redis client with a connection timeout...
-
Question 1 Which framework is most commonly used for unit testing in Java? JUnit TestNG Mockito Selenium Answer: JUnit Question 2 I...
-
import redis from 'redis'; // Create a Redis client with retry strategy const client = redis.createClient({ host: 'localhos...
-
max-request-journal-entries and no-request-journal in wiremock configuration In WireMock, the request journal is a built-in feature that ke...
-
MG-CEIT Course Feedback Form Step1: Like below three pages for the updated course information ...
-
const express = require('express'); const redis = require('redis'); const { promisify } = require('util'); const ...
-
import redis from 'redis'; // Create a Redis client with retry strategy const client = redis.createClient({ host: 'localhos...