文档(https://www.apollographql.com/docs/apollo-server/features/data-sources.html#Using-Memcached-Redis-as-a-cache-storage-backend https://www.apollographql.com/docs/apollo-server/features/data-sources.html#Using-Memcached-Redis-as-a-cache-storage-backend)显示这样的代码:
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(),
}),
});
我想知道怎么会这样cache
使用 key ,考虑到缓存实际上是在类似的东西中自定义实现的MoviesAPI()
然后通过使用context.dataSources.moviesAPI.someFunc()
。例如,假设我想为 SQL 数据库实现自己的缓存。它看起来像
cache: new RedisCache({
host: 'redis-server',
}),
dataSources: () => ({
SQL: new SQLCache(),
}),
});
where SQLCache
有我自己的功能连接到RedisCache
like:
getCached(id, query, ttl) {
const cacheKey = `sqlcache:${id}`;
return redisCache.get(cacheKey).then(entry => {
if (entry) {
console.log('CACHE HIT!');
return Promise.resolve(JSON.parse(entry));
}
console.log('CACHE MISS!');
return query.then(rows => {
if (rows) redisCache.set(cacheKey, JSON.stringify(rows), ttl);
return Promise.resolve(rows);
});
});
}
所以这意味着我有RedisCache
在两个ApolloServer
cache
钥匙和dataSource
执行。显然,RedisCache
用于dataSource
实施,但是那又是什么呢?ApolloServer
cache
关键究竟是做什么?
同样在客户端上,示例主要显示了使用InMemoryCache
而不是Redis缓存。客户端 Apollo 缓存是否应该与服务器缓存不同,或者应该是相同的缓存,例如RedisCache
都在两个地方吗?