Skip to main content

Connect with NodeJS

Learn how to connect to an Aiven for Caching service using NodeJS with the ioredis library.

Variables​

Replace the following placeholders in the code sample with actual values from your service overview page:

VariableDescription
SERVICE_URIURI for the Aiven for Caching service connection

Prerequisites​

To install the ioredis library, run the following command:

npm install --save ioredis

Code​

Create a file named index.js and insert the code below, substituting the placeholder with your Aiven for Caching URI:

const Redis = require("ioredis");
const redisUri = "REDIS_URI"
const redis = new Redis(redisUri);

redis.set("key", "hello world");

redis.get("key").then(function (result) {
console.log(`The value of key is: ${result}`);
redis.disconnect();
});

This code creates a key named key with the value hello world without an expiration. It then retrieves this key from the caching service and outputs its value.

Execute the script with:

node index.js

Successful execution results in the following output:

The value of key is: hello world