Lesson 16 / 18

Redis in a Node App

Talk to Redis from JavaScript with a client library.

Install the client

The official redis package works well for most Node apps.

npm install redis

Connect & use it

The client mirrors Redis commands as async methods, so caching a value looks almost like the CLI.

import { createClient } from 'redis';

const client = createClient({ url: 'redis://localhost:6379' });
await client.connect();

await client.set('greeting', 'hello', { EX: 60 });
const value = await client.get('greeting');
console.log(value); // 'hello'