Skip to content
the poor coder

nodejs redis setex

Poor Man
Published
Reading time
3 min read
On this page

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:

Text

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:

Text

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:

Text

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.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC