Intermediate

clideploy

Connect to Redis

Edit

Using the r2d2 module, you can connect to a Redis database running anywhere.

Import the `sendCommand()` function from r2d2
import { sendCommand } from "https://deno.land/x/r2d2/mod.ts";
Create a TCP connection with the Redis server
const redisConn = await Deno.connect({ port: 6379 });
Authenticate with the server by sending the command "AUTH <username> <password>"
await sendCommand(redisConn, [
  "AUTH",
  Deno.env.get("REDIS_USERNAME")!,
  Deno.env.get("REDIS_PASSWORD")!,
]);
Set the "hello" key to have value "world" using the command "SET hello world"
await sendCommand(redisConn, ["SET", "hello", "world"]); // "OK"
Get the "hello" key using the command "GET hello"
await sendCommand(redisConn, ["GET", "hello"]); // "world"
Close the connection to the database
redisConn.close();

Run this example locally using the Deno CLI:

deno run --allow-net --allow-env https://raw.githubusercontent.com/denoland/deno-docs/main/by-example/redis.ts