【发布时间】:2019-04-20 20:34:27
【问题描述】:
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 键是如何使用的,考虑到缓存似乎实际上是在MoviesAPI() 之类的东西中自定义实现的,然后通过context.dataSources.moviesAPI.someFunc() 使用。例如,假设我想为 SQL 数据库实现自己的缓存。它看起来像
cache: new RedisCache({
host: 'redis-server',
}),
dataSources: () => ({
SQL: new SQLCache(),
}),
});
SQLCache 有我自己的函数连接到RedisCache,例如:
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);
});
});
}
这意味着我在ApolloServer cache 键和dataSource 实现中都有RedisCache。显然,RedisCache 用于 dataSource 实现中,但是 ApolloServer cache 键到底是做什么的?
同样在客户端,示例大多显示使用 InMemoryCache 而不是 Redis 缓存。客户端 Apollo 缓存应该是与服务器缓存不同的缓存,还是应该在两个地方都使用像 RedisCache 这样的相同缓存?
【问题讨论】:
标签: apollo apollo-client apollo-server apollostack