【问题标题】:Graphql server + NodeJS, store data in cache with RedisGraphql 服务器 + NodeJS,使用 Redis 将数据存储在缓存中
【发布时间】:2018-11-07 19:25:15
【问题描述】:

我想知道如何改进将 graphql 连接到 api 的响应时间。我决定使用 Redis。我不知道我该怎么做。

我已经建立了我的 graphql 服务器:

import express from 'express';
import {
    graphqlExpress,
    graphiqlExpress,
} from 'graphql-server-express';
import bodyParser from 'body-parser';

import { schema } from './src/schema';
import cors from 'cors';

import redis from 'redis';

const PORT = 4000;
const server = express();

const client = redis.createClient();

client.on('error', function (err) {
    console.log('error' + err)
});

server.use('*', cors({ origin: 'http://localhost:3000' }));

server.use('/graphql', bodyParser.json(), graphqlExpress({
    schema, context: { client }
}));


server.use('/graphiql', graphiqlExpress({
    endpointURL: '/graphql'
}));



server.listen(PORT, () =>
    console.log(`GraphQL Server is now running on http://localhost:${PORT}`)
);

我这里已经导入了redis。我的 graphql 服务器通过 cors 连接到首页,所以我可以从 api 渲染字符串。在解析器中,我通过 Node.js 连接到 api(如有必要,我将添加它。),我在这里有一些自定义解析器。来自 api 的响应时间(也影响页面上的渲染)太慢了——大约 10~15 秒。

【问题讨论】:

    标签: node.js redis graphql


    【解决方案1】:

    我建议查看Apollo Engine,以更好地了解 GraphQL 的工作原理,并更轻松地使用 Redis 缓存等附加组件。让 Apollo Engine 工作后,您可以使用 apollo-server-redis-cache 创建缓存。

    如果您的请求在没有缓存的情况下需要 10 到 15 秒,我还希望先优化它们,然后再将 Redis 扔到它们面前。

    【讨论】:

    • 问题是,页面响应时间比 Insomnia 低。失眠的响应时间(我已连接到 Test Magento2 Server Rest API)是 29-30 秒。这太高了。另外,我还有另一个 Magento2 服务器 API,但是这个响应时间非常高(失眠 11.2 分钟)。我认为问题出在 api 方面,完全没有优化。
    猜你喜欢
    • 2020-02-26
    • 1970-01-01
    • 2020-04-13
    • 1970-01-01
    • 2015-06-18
    • 1970-01-01
    • 1970-01-01
    • 2016-03-22
    • 2020-08-07
    相关资源
    最近更新 更多