【问题标题】:How does graphql apollo server uses memcached as a cache system?graphql apollo server 如何使用 memcached 作为缓存系统?
【发布时间】:2021-11-25 01:22:54
【问题描述】:

根据 apollo 文档,我无法使用 telnet 命令从 memcached 获取数据。数据似乎被缓存了,因为当我重新发送相同的请求时,数据源不会调用 API。


const server = new ApolloServer({
  typeDefs,
  resolvers,
  cache: new MemcachedCache(
    '127.0.0.1:11211',
    { retries: 10, retry: 10000 }, // Options
  ),
  dataSources: () => ({
    moviesAPI: new MoviesAPI(),
  }),
});

我尝试在一个虚拟项目中手动实现 memcached,并且能够在 telnet 接口中设置/获取数据。

是graphql查询memcached还是需要在resolver中手动查询?

欢迎任何帮助

【问题讨论】:

    标签: express graphql memcached apollo-server


    【解决方案1】:

    将缓存传递给 ApolloServer 初始化的选项是 optionl。话虽如此,缓存大大降低了数据库获取的速度。您必须在任何 dataSource 类的初始化中传递给定的缓存,如下所示:

    class DogCustomDataSource extends CustomDataSource{
       constructor(models){ 
          ....
          }
       initialize({context,cache}){
       this.context=context
       this.cache=cache
         }
       }
    

    是的,你是对的,随后,你必须在你的数据源的方法中设置和删除对象到缓存中,像这样:

    async get(id,{ttlInSeconds}={}){
      .....
      await this.cache.set("yourKey","yourValue",{ttl:ttlInSeconds})
      //or
      await this.cache.del("yourKey")
      .....
    }
    

    【讨论】:

      猜你喜欢
      • 2011-02-05
      • 2019-03-26
      • 2019-09-01
      • 2020-02-12
      • 2019-02-03
      • 2020-03-08
      • 2020-07-07
      • 2017-12-25
      • 2020-11-17
      相关资源
      最近更新 更多