Created
April 16, 2023 12:54
-
-
Save vip3r011/307c7a7f51682f6aa8b49e627d23f17e to your computer and use it in GitHub Desktop.
redis experimental module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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