首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用apollo-server进行缓存

如何使用apollo-server进行缓存
EN

Stack Overflow用户
提问于 2018-08-29 07:41:35
回答 2查看 4.2K关注 0票数 5

https://www.apollographql.com/docs/apollo-server/features/data-sources.html#Implementing-your-own-cache-backend的阿波罗基础示例指出,创建redis缓存非常简单,如下所示:

代码语言:javascript
复制
const { RedisCache } = require('apollo-server-cache-redis');

const server = new ApolloServer({
  typeDefs,
  resolvers,
  cache: new RedisCache({
    host: 'redis-server',
    // Options are passed through to the Redis client
  }),
  dataSources: () => ({
    moviesAPI: new MoviesAPI(),
  }),
});

当我查看非redis的示例时,它表明这是一个用于缓存的简单{ get, set }。这意味着理论上我应该能够做到。

代码语言:javascript
复制
cache : {
   get : function() {
     console.log("GET!");
   },
   set : function() {
     console.log("SET!");
   }
}

无论我怎么尝试,当我使用阿波罗服务器原生提供的graphQL浏览器时,我的缓存函数都不会被调用。

我尝试过使用cacheControl : truecacheControl set,就像在https://medium.com/brikl-engineering/serverless-graphql-cached-in-redis-with-apollo-server-2-0-f491695cac7f中一样。没什么。

有没有一个不使用付费Apollo引擎系统的Apollo实现基本缓存的例子?

EN

回答 2

Stack Overflow用户

发布于 2018-09-17 01:22:18

您应该能够通过实现自己的接口来使用NPM包'apollo-server-caching‘。请参阅Implementing Your Own Cache,其中提供了一个示例。

票数 0
EN

Stack Overflow用户

发布于 2019-10-12 18:23:39

您可以查看此package的实现,它缓存完整的响应以实现您自己的缓存。

代码语言:javascript
复制
import { RedisCache } from "apollo-server-redis";
import responseCachePlugin from "apollo-server-plugin-response-cache";


 const server = new ApolloServer({
    ...
    plugins: [responseCachePlugin()],
     cache: new RedisCache({
        connectTimeout: 5000,
        reconnectOnError: function(err) {
          Logger.error("Reconnect on error", err);
          const targetError = "READONLY";
          if (err.message.slice(0, targetError.length) === targetError) {
            // Only reconnect when the error starts with "READONLY"
            return true;
          }
        },
        retryStrategy: function(times) {
          Logger.error("Redis Retry", times);
          if (times >= 3) {
            return undefined;
          }
          return Math.min(times * 50, 2000);
        },
        socket_keepalive: false,
        host: "localhost",
        port: 6379,
        password: "test"
      }),
 });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52067554

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档