首页 > 解决方案 > 如何使用 apollo-server 进行缓存

问题描述

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 explorer 时,我的缓存函数都不会被调用。

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

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

标签: graphqlapolloapollo-server

解决方案


您应该能够通过实现自己的接口来使用 NPM 包“apollo-server-caching”。请参阅实现您自己的缓存,其中提供了一个示例。


推荐阅读