Skip to content

Commit

Permalink
Merge pull request #113 from AirWalk-Digital/bug/fix-redis-connection
Browse files Browse the repository at this point in the history
fix: revert redis connection settings
  • Loading branch information
mcuckson authored Jul 28, 2023
2 parents c7fbbcf + e46aad7 commit a1fc771
Showing 1 changed file with 145 additions and 163 deletions.
308 changes: 145 additions & 163 deletions lib/redis/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Redis = require('ioredis');
const Redis = require("ioredis");

let redisInstance;

Expand Down Expand Up @@ -36,189 +36,171 @@ let redisInstance;
// // console.log(await redis.ttl("foo")); // -1

function getRedisConfiguration() {
return {
host: process.env.REDIS_HOST || '172.17.0.1',
password: process.env.REDIS_PASSWORD,
port: process.env.REDIS_PORT,
}
return {
host: process.env.REDIS_HOST || "172.17.0.1",
password: process.env.REDIS_PASSWORD,
port: process.env.REDIS_PORT,
};
}

export async function createRedisInstance(config = getRedisConfiguration()) {
try {
const options = {
enableReadyCheck: true,
scaleReads: "all",
redisOptions: {
host: config.host,
lazyConnect: true,
showFriendlyErrorStack: true,
enableAutoPipelining: true,
maxRetriesPerRequest: 0,
retryStrategy: times => {
if (times > 3) {
throw new Error(`[Redis] Could not connect after ${times} attempts`);
}

return Math.min(times * 200, 1000);
}
},
}

if (config.port) {
options.redisOptions.port = config.port;
}

if (config.password) {
options.redisOptions.password = config.password;
}

let redis;

if (process.env.REDIS_CLUSTER_MODE) {
redis = new Redis.Cluster([
{
host: config.host,
port: config.port
}],
options
try {
const options = {
enableReadyCheck: true,
scaleReads: "all",
redisOptions: {
host: config.host,
lazyConnect: true,
showFriendlyErrorStack: true,
enableAutoPipelining: true,
maxRetriesPerRequest: 0,
retryStrategy: (times) => {
if (times > 3) {
throw new Error(
`[Redis] Could not connect after ${times} attempts`,
);
} else {
// For local development or non-clustered environment
redis = new Redis(options.redisOptions);
}

// Wrap the Redis instance creation in a promise to handle any connection errors.
return new Promise((resolve, reject) => {
redis.on('error', error => {
// console.warn('[Redis] Error connecting', error);
// If an error occurs during the Redis connection, we'll reject the promise.
reject(error);
});

// Now we'll try to connect the Redis instance.
redis.connect(err => {
if (err) {
// console.warn('[Redis] Error connecting', err);
// If an error occurs during the Redis connection, we'll reject the promise.
reject(err);
} else {
// If the connection is successful, resolve the promise with the Redis instance.
resolve(redis);
}
});
});
} catch (e) {
// throw new Error(`[Redis] Could not create a Redis instance`);
}

return Math.min(times * 200, 1000);
},
},
};

if (config.port) {
options.redisOptions.port = config.port;
}
}

if (config.password) {
options.redisOptions.password = config.password;
}

let redis;

if (process.env.REDIS_CLUSTER_MODE) {
redis = new Redis.Cluster(
[
{
host: config.host,
port: config.port,
},
],
options,
);
} else {
// For local development or non-clustered environment
redis = new Redis(options.redisOptions);
}

return redis;
} catch (e) {
// throw new Error(`[Redis] Could not create a Redis instance`);
}
}

// Function to read data from the cache
export async function cacheRead(key) {
try {
if (!redisInstance) {
redisInstance = await createRedisInstance();
}
const value = await redisInstance.get(key);
if (!value) return null;
const parsedValue = JSON.parse(value);
return parsedValue.buffer ? Buffer.from(parsedValue.buffer) : parsedValue;
} catch (error) {
// Handle the error here if Redis is unavailable or there was a connection error.
// For example, you may use a fallback cache mechanism or default values.
console.error('Error during Redis setup:', error);
return null;
try {
if (!redisInstance) {
redisInstance = await createRedisInstance();
}
const value = await redisInstance.get(key);
if (!value) return null;
const parsedValue = JSON.parse(value);
return parsedValue.buffer ? Buffer.from(parsedValue.buffer) : parsedValue;
} catch (error) {
// Handle the error here if Redis is unavailable or there was a connection error.
// For example, you may use a fallback cache mechanism or default values.
console.error("Error during Redis setup:", error);
return null;
}
}


// Function to write data to the cache
export async function cacheWrite(key, value, ttl = null) {
try {
if (!redisInstance) {
redisInstance = await createRedisInstance();
}
const isBuffer = Buffer.isBuffer(value);
const stringifiedValue = isBuffer ? JSON.stringify({ buffer: [...value] }) : JSON.stringify(value);

if (ttl) {
try {
await redisInstance.set(key, stringifiedValue, "EX", ttl);
return true; // or return 'Data set successfully';
} catch (error) {
console.error(`Error setting data: ${error}`);
throw error;
}
} else {
try {
await redisInstance.set(key, stringifiedValue);
return true; // or return 'Data set successfully';
} catch (error) {
console.error(`Error setting data: ${error}`);
throw error;
}
}
} catch (error) {
// Handle the error here if Redis is unavailable or there was a connection error.
// For example, you may use a fallback cache mechanism or default values.
console.error('Error during Redis setup:', error);
return null;
try {
if (!redisInstance) {
redisInstance = await createRedisInstance();
}
const isBuffer = Buffer.isBuffer(value);
const stringifiedValue = isBuffer
? JSON.stringify({ buffer: [...value] })
: JSON.stringify(value);

if (ttl) {
try {
await redisInstance.set(key, stringifiedValue, "EX", ttl);
return true; // or return 'Data set successfully';
} catch (error) {
console.error(`Error setting data: ${error}`);
throw error;
}
} else {
try {
await redisInstance.set(key, stringifiedValue);
return true; // or return 'Data set successfully';
} catch (error) {
console.error(`Error setting data: ${error}`);
throw error;
}
}
} catch (error) {
// Handle the error here if Redis is unavailable or there was a connection error.
// For example, you may use a fallback cache mechanism or default values.
console.error("Error during Redis setup:", error);
return null;
}
}

// Function to read data from the cache
export async function cacheSearch(key) {
try {

if (!redisInstance) {
redisInstance = await createRedisInstance();
}
const value = await redisInstance.keys(key, (err, result) => {
if (err) {
console.error('Error fetching keys:', err);
} else {
return result;
}
});

if (!value) return null;
return value;
} catch (error) {
// Handle the error here if Redis is unavailable or there was a connection error.
// For example, you may use a fallback cache mechanism or default values.
console.error('Error during Redis setup:', error);
return [];
try {
if (!redisInstance) {
redisInstance = await createRedisInstance();
}
const value = await redisInstance.keys(key, (err, result) => {
if (err) {
console.error("Error fetching keys:", err);
} else {
return result;
}
});

if (!value) return null;
return value;
} catch (error) {
// Handle the error here if Redis is unavailable or there was a connection error.
// For example, you may use a fallback cache mechanism or default values.
console.error("Error during Redis setup:", error);
return [];
}
}

export async function hset(key, obj, ttl = null) {

try {
if (!redisInstance) {
redisInstance = await createRedisInstance();
}
if (ttl) {
try {
await redisInstance.hset(key, obj, "EX", ttl);
return true; // or return 'Data set successfully';
} catch (error) {
console.error(`Error setting data: ${error}`);
throw error;
}
} else {
try {
await redisInstance.hset(key, obj);
return true; // or return 'Data set successfully';
} catch (error) {
console.error(`Error setting data: ${error}`);
throw error;
}
}
} catch (error) {
// Handle the error here if Redis is unavailable or there was a connection error.
// For example, you may use a fallback cache mechanism or default values.
console.error('Error during Redis setup:', error);
return false;
try {
if (!redisInstance) {
redisInstance = await createRedisInstance();
}

}
if (ttl) {
try {
await redisInstance.hset(key, obj, "EX", ttl);
return true; // or return 'Data set successfully';
} catch (error) {
console.error(`Error setting data: ${error}`);
throw error;
}
} else {
try {
await redisInstance.hset(key, obj);
return true; // or return 'Data set successfully';
} catch (error) {
console.error(`Error setting data: ${error}`);
throw error;
}
}
} catch (error) {
// Handle the error here if Redis is unavailable or there was a connection error.
// For example, you may use a fallback cache mechanism or default values.
console.error("Error during Redis setup:", error);
return false;
}
}

0 comments on commit a1fc771

Please sign in to comment.