nodejs redis setex

Node.js Redis Setex

Redis is an open-source, in-memory data structure store that can be used as a database, cache, and message broker. It supports various data structures such as strings, hashes, lists, sets, and more. Node.js is a popular server-side platform that allows developers to build scalable network applications.

What is SETEX Command in Redis?

In Redis, SETEX command is used to set the value of a key with an expiration time in seconds. This command sets the value of the given key and also sets the expiration time (in seconds) for the key. After the expiration time, the key will automatically be deleted from the Redis database.

How to Use SETEX Command in Node.js?

To use SETEX command in Node.js, we need to install the Redis client package using npm. Here's how:


npm install redis

Once the Redis client package is installed, we can use it to connect to Redis server and execute Redis commands such as SETEX. Here's an example of how to use SETEX command in Node.js:


const redis = require('redis');
const client = redis.createClient();

// Set the value of a key with an expiration time of 60 seconds
client.setex('mykey', 60, 'myvalue', function(err, reply) {
    if (err) {
        console.error(err);
    } else {
        console.log(reply);
    }
});

// Get the value of the key
client.get('mykey', function(err, reply) {
    if (err) {
        console.error(err);
    } else {
        console.log(reply);
    }
});

In the above example, we first create a Redis client using the createClient method provided by the Redis package. Then, we use the setex method of the client object to set the value of 'mykey' as 'myvalue' with an expiration time of 60 seconds. After that, we use the get method of the client object to get the value of the key.

Alternative Way to Use SETEX Command in Node.js

Another way to use SETEX command in Node.js is by using promises instead of callbacks. Here's an example:


const redis = require('redis');
const { promisify } = require('util');
const client = redis.createClient();

// Promisify Redis commands
const setexAsync = promisify(client.setex).bind(client);
const getAsync = promisify(client.get).bind(client);

// Set the value of a key with an expiration time of 60 seconds
setexAsync('mykey', 60, 'myvalue')
    .then(reply => console.log(reply))
    .catch(err => console.error(err));

// Get the value of the key
getAsync('mykey')
    .then(reply => console.log(reply))
    .catch(err => console.error(err));

In the above example, we first create a Redis client using the createClient method provided by the Redis package. Then, we use the promisify method provided by the util package to convert Redis commands such as setex and get into promises. After that, we use the setexAsync and getAsync functions to set and get the value of 'mykey'. The advantage of using promises is that it makes the code more readable and easier to handle errors.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe