如何使用apollo-server进行缓存

问题描述 投票:3回答:1

https://www.apollographql.com/docs/apollo-server/features/data-sources.html#Implementing-your-own-cache-backend上的apollo基本示例表明,执行redis缓存非常简单:

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 }缓存。这意味着我理论上应该能做到。

cache : {
   get : function() {
     console.log("GET!");
   },
   set : function() {
     console.log("SET!");
   }
}

无论我尝试什么,当我使用apollo-server本身提供的graphQL资源管理器时,我的缓存函数永远不会被调用。

我试过cacheControl : truecacheControl设置就像它在https://medium.com/brikl-engineering/serverless-graphql-cached-in-redis-with-apollo-server-2-0-f491695cac7f。没有。

有没有一个例子说明如何在Apollo中实现基本缓存,而不使用付费的Apollo Engine系统?

graphql apollo apollo-server
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.