Skip to main content
Deno 2 is finally here 🎉️
Learn more

r2d2

Docs CI codecov

Lightweight Redis client library for Deno. Design principles:

  • Must be fundamentally simple
  • Use native Deno APIs and Deno’s standard library without custom interfaces
  • Encourage the use of actual Redis commands without intermediate abstractions

Usage

Must be run with --allow-net permission.

Basic commands

import { sendCommand } from "https://deno.land/x/r2d2/mod.ts";

const redisConn = await Deno.connect({ port: 6379 });

// Returns "OK"
await sendCommand(redisConn, ["SET", "hello", "world"]);

// Returns "world"
await sendCommand(redisConn, ["GET", "hello"]);

If you don’t care about the reply:

import { writeCommand } from "https://deno.land/x/r2d2/mod.ts";

const redisConn = await Deno.connect({ port: 6379 });

// Returns nothing
await writeCommand(redisConn, ["SHUTDOWN"]);

Raw data

import {
  sendCommand,
  sendCommandRawReply,
} from "https://deno.land/x/r2d2/mod.ts";

const redisConn = await Deno.connect({ port: 6379 });

const value = new Uint8Array([0, 1, 2, 1, 2, 1, 2, 3]);

// Returns "OK"
await sendCommand(redisConn, ["SET", "binary", value]);

// Returns Uint8Array(8) [0, 1, 2, 1, 2, 1, 2, 3]
await sendCommandRawReply(redisConn, ["GET", "binary"]);

Pipelining

import { pipelineCommands } from "https://deno.land/x/r2d2/mod.ts";

const redisConn = await Deno.connect({ port: 6379 });

// Returns [1, 2, 3, 4]
await pipelineCommands(redisConn, [
  ["INCR", "X"],
  ["INCR", "X"],
  ["INCR", "X"],
  ["INCR", "X"],
]);

Pub/Sub

import { listenReplies, writeCommand } from "https://deno.land/x/r2d2/mod.ts";

const redisConn = await Deno.connect({ port: 6379 });

await writeCommand(redisConn, ["SUBSCRIBE", "mychannel"]);
for await (const reply of listenReplies(redisConn)) {
  // Prints ["subscribe", "mychannel", 1] first iteration
  console.log(reply);
}

Documentation

Check out the documentation here.

Testing

deno task test

Note: Redis must be installed on your local machine. For installation instructions, see here.

Benchmarks

deno task bench

Note: Redis must be installed on your local machine. For installation instructions, see here.

These resources, one way or another, inspired the creation of this module. If you’re one of the authors, thank you.