Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I was wondering if I need to add the with-ioredis example #10

Open
luoyjx opened this issue Dec 6, 2022 · 0 comments
Open

I was wondering if I need to add the with-ioredis example #10

luoyjx opened this issue Dec 6, 2022 · 0 comments

Comments

@luoyjx
Copy link

luoyjx commented Dec 6, 2022

Because ioredis is also another popular Redis client library

Here is my example code like the code in with-redis

import { Server } from "https://deno.land/[email protected]/http/server.ts";
import IORedis from 'npm:[email protected]'

// make a connection to the local instance of redis
const client = new IORedis("redis://localhost:6379")

client.on("error", (error) => {
  console.error(error);
});

const server = new Server({
  handler: async (req) => {
    const { pathname } = new URL(req.url);
    // strip the leading slash
    const username = pathname.substring(1);
    const cached_user = await client.get(username);
    if (cached_user) {
      return new Response(cached_user, {
        headers: {
          "content-type": "application/json",
          "is-cached": "true",
        },
      });
    } else {
      const resp = await fetch(`https://api.github.com/users/${username}`);
      const user = await resp.json();
      await client.set(username, JSON.stringify(user));
      return new Response(JSON.stringify(user), {
        headers: {
          "content-type": "application/json",
          "is-cached": "false",
        },
      });
    }
  },

  port: 3000,
});

server.listenAndServe();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant