连接到 Redis
使用 jsr:@iuioiua/redis 模块,您可以连接到任何地方运行的 Redis 数据库。
从 jsr:@iuioiua/redis 导入 `RedisClient`
import { RedisClient } from "jsr:@iuioiua/redis";
创建与 Redis 服务器的 TCP 连接
using redisConn = await Deno.connect({ port: 6379 });
创建一个 'RedisClient' 实例
const redisClient = new RedisClient(redisConn);
通过发送命令 "AUTH <username> <password>" 与服务器进行身份验证
await redisClient.sendCommand([
"AUTH",
Deno.env.get("REDIS_USERNAME")!,
Deno.env.get("REDIS_PASSWORD")!,
]);
使用命令 "SET hello world" 将 "hello" 键的值设置为 "world"
await redisClient.sendCommand(["SET", "hello", "world"]); // "OK"
使用命令 "GET hello" 获取 "hello" 键
await redisClient.sendCommand(["GET", "hello"]); // "world"
使用 Deno CLI 在本地运行此示例
deno run -N -E https://docs.deno.org.cn/examples/scripts/redis.ts