redis 客户端已关闭

ojsjcaue  于 2022-10-31  发布在  Redis
关注(0)|答案(2)|浏览(253)

我试图学习redis,我获取github repo数据,然后我想用redis缓存它.但是我得到错误为什么尝试使用redis:
如果您有任何问题,请在下面的代码中输入一个错误,然后单击“取消”。
客户端关闭错误:客户端已关闭
这是我密码

import express from "express";
import Redis from "redis";
import fetch from "node-fetch";

const PORT = process.env.PORT || 5000;
const REDIS_PORT = process.env.REDIS_PORT || "6379";

const client = Redis.createClient(REDIS_PORT);

const app = express();

// Set response
function setResponse(username, repos) {
  return `<h2>${username} has ${repos} Github repos</h2>`;
}

// Make request to Github for data
async function getRepos(req, res, next) {
  try {
    console.log("Fetching Data...");

    const { username } = req.params;

    const response = await fetch(`https://api.github.com/users/${username}`);

    const data = await response.json();

    const repos = data.public_repos;

    // Set data to Redis
    // await client.connect();
    client.setEx(username, 3600, repos);

    res.send(setResponse(username, repos));
  } catch (e) {
    console.log(e);
    res.status(500);
  }
}

app.get("/repos/:username", getRepos);

app.listen(5000, () => {
  console.log(`App listening on port ${PORT}`);
});

我如何修复此错误?

sqxo8psd

sqxo8psd1#

如果你看一下节点Redis的README,你会发现下面这段代码:

import { createClient } from 'redis';

const client = createClient();

client.on('error', (err) => console.log('Redis Client Error', err));

await client.connect();

await client.set('key', 'value');
const value = await client.get('key');

如果你想连接到本地运行的Redis和默认端口,client configuration guide会告诉你如何连接。下面是几种常见的方法:

/* the host, port, and password probably from process.env or something */
const host = 'awesome.redis.server'
const port = 6380

/* create a client using host and port */
const client = createClient({ socket : { host, port } })

/* create a client with a Redis URL */
const url = `redis://${host}:${port}`
const client = createClient({ url })
idfiyjo8

idfiyjo82#

我在使用4. 2. 0版本时遇到了同样的问题,为了解决这个错误,需要做两个更改。
1.首先,客户端连接不再自动完成,因此必须手动完成。
1.其次,我建议使用以下格式的client.set。

import express from "express";
import Redis from "redis";
import fetch from "node-fetch";

const PORT = process.env.PORT || 5000;
const REDIS_PORT = process.env.REDIS_PORT || "6379";

const {createClient} = redis;
const client  = createClient();
client.connect();

const app = express();
app.use(express.json())   

// Set response
function setResponse(username, repos) {
  return `<h2>${username} has ${repos} Github repos</h2>`;
}

// Make request to Github for data
async function getRepos(req, res, next) {
  try {
    console.log("Fetching Data...");

    const { username } = req.params;

    const response = await fetch(`https://api.github.com/users/${username}`);

    const data = await response.json();

    const repos = data.public_repos;

    //updated set method for v4
     await client.set(username, repos, {
            EX: 10,
            NX: true
      });
    res.send(setResponse(username, repos));
  } catch (e) {
    console.log(e);
    res.status(500);
  }
}

app.get("/repos/:username", getRepos);

app.listen(5000, () => {
  console.log(`App listening on port ${PORT}`);
});

要获取缓存的数据,请运行const data = await client.get(用户名);

相关问题