Connect to Redis database using Deno

Viewed 202

So I'm using Deno for my server and I want to persist some data in a Redis database, how can I do it?

I tried to follow this documentation (https://deno.land/x/redis) but didn't quite understand, I know Redis offers a lot of services, and this is my first time using it, so I'm a little confused

2 Answers

use connect to connect to your Redis server. Then use .set method to set a value, equivalent to redis command:

SET key value

And use .get to retrieve the value, which is equivalent to redis command:

GET key
import { connect } from "https://denopkg.com/keroxp/deno-redis/mod.ts";
const redis = await connect({
  hostname: "127.0.0.1",
  port: 6379
});

const ok = await redis.set("deno", "land");
const value = await redis.get("deno");

console.log(value) // deno

Run the script using --allow-net flag:

deno run --allow-net redis.js
import { connect } from "https://denopkg.com/keroxp/deno-redis/mod.ts";
const redis = await connect({
    hostname: "127.0.0.1",
    port: 6379
});
await redis.set("user1", "Nikhil");
await redis.set("user2", "Mahesh");

const users = await redis.keys("user*");
console.log(users);

Make sure if you get Uncaught PermissionDenied: network access to "127.0.0.1:6379", run again with the --allow-net flag privilege error, you are missing --allow-net flag while running.

deno run --allow-net server.ts

Related