【发布时间】:2022-01-26 00:26:00
【问题描述】:
我现在很绝望,我正在寻求任何帮助。 我正在尝试使用 GraphQL 和 Redis 在我的项目中设置缓存机制。 这就是我配置 GraphQLModule 的方式:
GraphQLModule.forRoot({
cache: new BaseRedisCache({
client: new Redis({
host: 'localhost',
port: 6379,
password: 'Zaq1xsw@',
}),
cacheControl: {
defaultMaxAge: 10000
},
}),
plugins: [
responseCachePlugin()
],
autoSchemaFile: path.resolve(__dirname, `../generated/schema.graphql`),
installSubscriptionHandlers: true,
}),
这就是我创建查询和突变的方式:
@Resolver()
export class AuthResolver {
constructor(
private readonly prismaService: PrismaService,
private readonly authService: AuthService,
){}
@Query(returns => String)
async testowe(@Args(`input`) input: String, @Info() info: any) {
info.cacheControl.setCacheHint({ maxAge: 5000, scope: 'PUBLIC' });
return 'test';
}}
当我使用 GraphQL Playground 并尝试此查询时,我得到的响应和标题如下所示:
HTTP/1.1 200 OK
X-Powered-By: Express
Access-Control-Allow-Origin: *
Content-Type: application/json; charset=utf-8
cache-control: max-age=5000, public
Content-Length: 28
ETag: W/"1c-2Df/lONPXcLzs1yVERHhOmONyns"
Date: Tue, 28 Dec 2021 21:35:11 GMT
Connection: keep-alive
Keep-Alive: timeout=5
正如您所见,有一个“缓存控制”部分。 我的问题是我看不到存储在 Redis 中的任何键或值。我使用 redis-cli 工具连接到 Redis 服务器,我尝试了“KEYS ‘*’”命令。 Redis 中没有存储任何内容。
此外,我对更复杂的查询也有疑问 - 我什至没有得到带有“缓存控制”部分的标题。
你知道我在这里做错了什么吗?我应该能够通过这种方法在 Redis 中查看存储的值吗? 提前感谢您的任何建议。
【问题讨论】:
标签: node.js graphql nestjs apollo