Redis basic commands converted into Nodejs async await functions

Ankit Kumar Rajpoot
3 min readOct 11, 2021

--

get:_

/**
* * this the method for get any key's value.
* @param redisKey
* @returns value
*/
module.exports.get = async (redisKey) => {
return client.get(redisKey, (err, result) => {
return result;
});
}
/**
* * this the method for set any key with the tts value.
* @param redisKey
* @param Value
* @param Ttl in mili seconds
* @returns true
*/
module.exports.set = async (redisKey, value, ttl) => {
if (ttl) {
client.set(redisKey, value, 'EX', ttl);
} else {
client.set(redisKey, value);
}
}

set:_

/**
* * method to set any key with the tts value.
* @param redisKey
* @param Value
* @param Ttl in mili seconds
* @returns true
*/
module.exports.set = async (redisKey, value, ttl) => {
if (ttl) {
return client.set(redisKey, value, 'EX', ttl);
} else {
return client.set(redisKey, value);
}
}

del:_

/**
* * method to delete the key from redis.
* @param redisKey
*/
module.exports.delete = async (redisKey) => {
return client.delete(redisKey);
}

lpush:_

/**
* * method to lpush in the redis list.
* @param redisKey
* @param value
*/
module.exports.lpush = async () => {
return client.lpush(redisKey, value, (err, result) => {
if (err) {
console.log(err);
}
return result;
})
}

rpush:_

/**
* * method to rpush in the redis list.
* @param redisKey
* @param value
*/
module.exports.rpush = async () => {
return client.rpush(redisKey, value, (err, result) => {
if (err) {
console.log(err);
return result;
}
return result
})
}

lrange:_

/**
* * method to lrange in the redis list.
* @param redisKey
* @param start
* @param stop
*/
module.exports.lrange = async (redisKey, start, stop) => {
return client.lrange(redisKey, start, stop, function (err, result) {
if (err) {
console.log(err);
return err;
}
return result;
})
}

lindex:_

/**
* * method to lindex in the redis list.
* @param redisKey
* @param index
*/
module.exports.lindex = async (redisKey, index) => {
return client.lindex(redisKey, index, function (err, result) {
if (err) {
console.log(err);
return err;
}
return result;
})
}

ltrim:_

/**
* * method to ltrim in the redis list.
* @param redisKey
* @param start
* @param stop
*/
module.exports.ltrim = async (redisKey, start, stop) => {
return client.ltrim(redisKey, start, stop, function (err, result) {
if (err) {
console.log(err);
return err;
}
return result;
})
}

lpop:_

/**
* * method to lpop in the redis list.
* @param redisKey
*/
module.exports.lpop = async (redisKey) => {
return client.lpop(redisKey, function (err, result) {
if (err) {
console.log(err);
return err;
}
return result;
})
}

rpop:_

/**
* * method to rpop in the redis list.
* @param redisKey
*/
module.exports.rpop = async (redisKey) => {
return client.rpop(redisKey, function (err, result) {
if (err) {
console.log(err);
return err;
}
return result;
})
}

incr:_

/**
* * method to increament any key value.
* @param redisKey
*/
module.exports.incr = async (redisKey) => {
return client.incr(redisKey)
}

decr:_

/**
* * method to decreament any key value.
* @param redisKey
*/
module.exports.decr = async (redisKey) => {
return client.decr(redisKey)
}

That’s it for this time! I hope you enjoyed this post. As always, I welcome questions, notes, comments and requests for posts on topics you’d like to read. See you next time! Happy Coding !!!!!

--

--

Ankit Kumar Rajpoot

I’m a MERN Developer. ( Redux | AWS | Python ) I enjoy taking on new things, building skills, and sharing what I’ve learned.