Skip to content

Instantly share code, notes, and snippets.

@vip3r011
Created April 16, 2023 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vip3r011/307c7a7f51682f6aa8b49e627d23f17e to your computer and use it in GitHub Desktop.
Save vip3r011/307c7a7f51682f6aa8b49e627d23f17e to your computer and use it in GitHub Desktop.
redis experimental module
'use strict';
const { createClient } = require('redis');
const options = {
host: '127.0.0.1',
port: 6379,
db: 0,
enable_offline_queue: false,
socket_keepalive: true,
socket_initialdelay: 5000,
maxRetries: 10,
enableReadyCheck: true,
connectTimeout: 10000,
retryStrategy: function (options) {
if (options.error && options.error.code === 'ECONNREFUSED') {
// End reconnecting on a specific error
return new Error('The server refused the connection');
}
if (options.total_retry_time > 1000 * 60 * 60) {
// End reconnecting after a specific timeout
return new Error('Retry time exhausted');
}
if (options.attempt > 10) {
// End reconnecting with built in error
return undefined;
}
// reconnect after a random time between 0 and 5000 ms
return Math.min(options.attempt * 100, 5000);
}
};
let redisReadClient;
let redisWriteClient;
async function getRedisReadClient() {
if (!redisReadClient) {
redisReadClient = createClient(options);
redisReadClient.on('error', (error) => {
console.error('Redis read client error:', error);
});
await redisReadClient.connect();
}
return redisReadClient;
}
async function getRedisWriteClient() {
if (!redisWriteClient) {
redisWriteClient = createClient(options);
redisWriteClient.on('error', (error) => {
console.error('Redis write client error:', error);
});
await redisWriteClient.connect();
}
return redisWriteClient;
}
async function expire(key, ttl = 300) {
const client = await getRedisWriteClient();
try {
const reply = await client.expire(key, ttl);
if (reply) {
return true;
}
return false;
} catch (err) {
console.error(err);
throw err;
} finally {
}
}
// set a value with expiration
async function setvaluewithexpire(key, value, ttl = 300) {
const client = await getRedisWriteClient();
try {
const reply = await client.set(key, value);
const exp = await client.expire(key, ttl);
if (reply) {
return true;
}
return false;
} catch (err) {
console.error(err);
throw err;
} finally {
}
}
// set a value
async function setvalue(key, value) {
const client = await getRedisWriteClient();
try {
const reply = await client.set(key, value);
if (reply) {
return true;
}
return false;
} catch (err) {
console.error(err);
throw err;
} finally {
}
}
// get a value
async function getvalue(key) {
const client = await getRedisReadClient();
try {
return await client.get(key);
} catch (err) {
console.error(err);
throw err;
} finally {
}
}
async function has(key) {
const client = await getRedisReadClient();
try {
const exist = await client.exists(key);
return exist === 1;
} catch (err) {
console.error(err);
throw err;
} finally {
}
}
async function del(key) {
const client = await getRedisWriteClient();
try {
const del = await client.del(key);
return true;
} catch (err) {
console.error(err);
throw err;
} finally {
}
}
async function ping() {
const client = await getRedisReadClient();
try {
const reply = await client.ping();
return true;
} catch (err) {
console.error(err);
throw err;
} finally {
}
}
module.exports = {
setvalue,
getvalue,
has,
del,
expire,
ping,
setvaluewithexpire
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment