title: Apollo Server - Confusion about cache/datasource options
body: <p>The docs (<a href="https://www.apollographql.com/docs/apollo-server/features/data-sources.html#Using-Memcached-Redis-as-a-cache-storage-backend" rel="noreferrer">https://www.apollographql.com/docs/apollo-server/features/data-sources.html#Using-Memcached-Redis-as-a-cache-storage-backend</a>) show code like this:</p>
  
  <pre><code>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: () =&gt; ({
      moviesAPI: new MoviesAPI(),
    }),
  });
  </code></pre>
  
  <p>I was wondering how that <code>cache</code> key is used, considering it seems like the caching is actually custom implemented in something like <code>MoviesAPI()</code> and then used via <code>context.dataSources.moviesAPI.someFunc()</code>. For example, say I wanted to implement my own cache for a SQL database. It'd look like</p>
  
  <pre><code>  cache: new RedisCache({
      host: 'redis-server',
    }),
    dataSources: () =&gt; ({
      SQL: new SQLCache(),
    }),
  });
  </code></pre>
  
  <p>where <code>SQLCache</code> has my own function that connects to the <code>RedisCache</code> like:</p>
  
  <pre><code>  getCached(id, query, ttl) {
      const cacheKey = `sqlcache:${id}`;
  
      return redisCache.get(cacheKey).then(entry =&gt; {
        if (entry) {
          console.log('CACHE HIT!');
          return Promise.resolve(JSON.parse(entry));
        }
        console.log('CACHE MISS!');
        return query.then(rows =&gt; {
          if (rows) redisCache.set(cacheKey, JSON.stringify(rows), ttl);
          return Promise.resolve(rows);
        });
      });
    }
  </code></pre>
  
  <p>So that means I have <code>RedisCache</code> in both the <code>ApolloServer</code> <code>cache</code> key and <code>dataSource</code> implementation. Clearly, the <code>RedisCache</code> is used in the <code>dataSource</code> implementation, but then what does that <code>ApolloServer</code> <code>cache</code> key do exactly?</p>
  
  <p>Also on the client, examples mostly show use of <code>InMemoryCache</code> instead of Redis cache. Should the client Apollo cache be a different cache from the server cache or should the same cache like <code>RedisCache</code> be in both places?</p>