代码之家  ›  专栏  ›  技术社区  ›  Owen Allen

如何使用Apollo服务器进行缓存

  •  2
  • Owen Allen  · 技术社区  · 7 年前

    阿波罗基本范例 https://www.apollographql.com/docs/apollo-server/features/data-sources.html#Implementing-your-own-cache-backend 他们指出,执行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服务器本机提供的graphql资源管理器时,都不会调用缓存函数。

    我试过了 cacheControl : true cacheControl 像它在里面一样设置 https://medium.com/brikl-engineering/serverless-graphql-cached-in-redis-with-apollo-server-2-0-f491695cac7f . 没有什么。

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

    1 回复  |  直到 6 年前
        1
  •  0
  •   Matthew P    6 年前

    通过实现自己的接口,您应该能够使用NPM包“Apollo服务器缓存”。见 Implementing Your Own Cache 它提供了一个例子。

    推荐文章