deno.com

連線到 Redis

在 Github 上編輯

使用 r2d2 模組,您可以連線到任何地方運行的 Redis 資料庫。

從 r2d2 匯入 `sendCommand()` 函式
import { sendCommand } from "https://deno.land/x/r2d2/mod.ts";
與 Redis 伺服器建立 TCP 連線
const redisConn = await Deno.connect({ port: 6379 });
透過傳送指令 "AUTH <username> <password>" 向伺服器驗證身分
await sendCommand(redisConn, [
  "AUTH",
  Deno.env.get("REDIS_USERNAME")!,
  Deno.env.get("REDIS_PASSWORD")!,
]);
使用指令 "SET hello world" 將 "hello" 鍵設定為值 "world"
await sendCommand(redisConn, ["SET", "hello", "world"]); // "OK"
使用指令 "GET hello" 取得 "hello" 鍵
await sendCommand(redisConn, ["GET", "hello"]); // "world"
關閉與資料庫的連線
redisConn.close();

使用 Deno CLI 在本機執行此範例

deno run --allow-net --allow-env https://deno-docs.dev.org.tw/examples/scripts/redis.ts

您找到您需要的資訊了嗎?

隱私權政策