Skip to main content

r2d2

Docs CI codecov

Fast, lightweight and simple Redis client library for Deno.

Features

  • The fastest Redis client in Deno. See below and try benchmarking yourself!
  • Supports RESPv2, RESP3, pipelining and pub/sub.
  • Written to be easily understood and debugged.
  • Encourages the use of actual Redis commands without intermediate abstractions.

Usage

Must be run with --allow-net permission. Check out the full documentation here.

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"]);

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);
}

Timeout

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

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

// Rejects if the command takes longer than 100 ms
await deadline(sendCommand(redisConn, ["SLOWLOG", "GET"]), 100);

Contributing

Before submitting a pull request, please run:

  1. deno fmt
  2. deno lint
  3. deno task redis:start && deno task test and ensure all tests pass
  4. deno task redis:start && deno task bench and ensure performance hasn’t degraded

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