Attributes
Includes Deno configuration
Repository
Current version released
2 years ago
Dependencies
std
r2d2
Fast, lightweight and simple Redis client library for Deno.
Features
- Supports RESPv2, RESP3, pipelining and pub/sub.
- The fastest Redis client in Deno. See below and try benchmarking yourself!
- 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.
RESPv2
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"]);
RESP3
import { sendCommand } from "https://deno.land/x/r2d2/mod.ts";
const redisConn = await Deno.connect({ port: 6379 });
// Switch to RESP3 protocol
await sendCommand(redisConn, ["HELLO", 3]);
// Returns 2
await sendCommand(redisConn, ["HSET", "hash3", "foo", 1, "bar", 2]);
// Returns { foo: "1", bar: "2" }
await sendCommand(redisConn, ["HGETALL", "hash3"]);
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:
deno fmt
deno lint
deno task redis:start && deno task test
and ensure all tests passdeno 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.