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!");
}
}无论我怎么尝试,当我使用阿波罗服务器原生提供的graphQL浏览器时,我的缓存函数都不会被调用。
我尝试过使用cacheControl : true和cacheControl set,就像在https://medium.com/brikl-engineering/serverless-graphql-cached-in-redis-with-apollo-server-2-0-f491695cac7f中一样。没什么。
有没有一个不使用付费Apollo引擎系统的Apollo实现基本缓存的例子?
发布于 2018-09-17 01:22:18
您应该能够通过实现自己的接口来使用NPM包'apollo-server-caching‘。请参阅Implementing Your Own Cache,其中提供了一个示例。
发布于 2019-10-12 18:23:39
您可以查看此package的实现,它缓存完整的响应以实现您自己的缓存。
import { RedisCache } from "apollo-server-redis";
import responseCachePlugin from "apollo-server-plugin-response-cache";
const server = new ApolloServer({
...
plugins: [responseCachePlugin()],
cache: new RedisCache({
connectTimeout: 5000,
reconnectOnError: function(err) {
Logger.error("Reconnect on error", err);
const targetError = "READONLY";
if (err.message.slice(0, targetError.length) === targetError) {
// Only reconnect when the error starts with "READONLY"
return true;
}
},
retryStrategy: function(times) {
Logger.error("Redis Retry", times);
if (times >= 3) {
return undefined;
}
return Math.min(times * 50, 2000);
},
socket_keepalive: false,
host: "localhost",
port: 6379,
password: "test"
}),
});https://stackoverflow.com/questions/52067554
复制相似问题