[フレーム]
Docs Pricing
Login Book a meeting Try Redis

ioredis guide (JavaScript)

Connect your Node.js/JavaScript application to a Redis database

ioredis is a Redis client for Node.js/JavaScript. The sections below explain how to install ioredis and connect your application to a Redis database.

Note:
Redis actively maintains and supports ioredis since it is in widespread use, but for new projects, we recommend using our newer Node.js client node-redis. See Migrate from ioredis if you are interested in converting an existing ioredis project to node-redis.

ioredis requires a running Redis server. See here for Redis Open Source installation instructions.

Install

To install ioredis, run:

npm install ioredis

Connect and test

Connect to localhost on port 6379.

import { Redis } from 'ioredis';
const redis = new Redis();
await redis.set('key', 'value');
const value = await redis.get('key');
console.log(value); // >>> value

await redis.hset('user-session:123', {
 name: 'John',
 surname: 'Smith',
 company: 'Redis',
 age: 29
});
const userSession = await redis.hgetall('user-session:123');
console.log(JSON.stringify(userSession, null, 2));
/* >>>
{
 "surname": "Smith",
 "name": "John",
 "company": "Redis",
 "age": "29"
}
 */
redis.disconnect();
ioredis Quick-Start

Store and retrieve a simple string.

import { Redis } from 'ioredis';
const redis = new Redis();
await redis.set('key', 'value');
const value = await redis.get('key');
console.log(value); // >>> value

await redis.hset('user-session:123', {
 name: 'John',
 surname: 'Smith',
 company: 'Redis',
 age: 29
});
const userSession = await redis.hgetall('user-session:123');
console.log(JSON.stringify(userSession, null, 2));
/* >>>
{
 "surname": "Smith",
 "name": "John",
 "company": "Redis",
 "age": "29"
}
 */
redis.disconnect();
ioredis Quick-Start

Store and retrieve a map.

import { Redis } from 'ioredis';
const redis = new Redis();
await redis.set('key', 'value');
const value = await redis.get('key');
console.log(value); // >>> value

await redis.hset('user-session:123', {
 name: 'John',
 surname: 'Smith',
 company: 'Redis',
 age: 29
});
const userSession = await redis.hgetall('user-session:123');
console.log(JSON.stringify(userSession, null, 2));
/* >>>
{
 "surname": "Smith",
 "name": "John",
 "company": "Redis",
 "age": "29"
}
 */
redis.disconnect();
ioredis Quick-Start

When you have finished using a connection, close it with client.quit().

import { Redis } from 'ioredis';
const redis = new Redis();
await redis.set('key', 'value');
const value = await redis.get('key');
console.log(value); // >>> value

await redis.hset('user-session:123', {
 name: 'John',
 surname: 'Smith',
 company: 'Redis',
 age: 29
});
const userSession = await redis.hgetall('user-session:123');
console.log(JSON.stringify(userSession, null, 2));
/* >>>
{
 "surname": "Smith",
 "name": "John",
 "company": "Redis",
 "age": "29"
}
 */
redis.disconnect();
ioredis Quick-Start

More information

The Github repository has useful information, including API docs and a set of code examples.

RATE THIS PAGE
Back to top ↑

On this page

AltStyle によって変換されたページ (->オリジナル) /