【发布时间】:2021-04-22 14:17:21
【问题描述】:
使用来自documentation 的简单“电影 API”示例。我在getMovie函数中添加了ttl,这样结果就缓存了10分钟。如何使updateMovie函数中的缓存失效?
const { RESTDataSource } = require('apollo-datasource-rest');
class MoviesAPI extends RESTDataSource {
async getMovie(id) {
return this.get(`movies/${id}`, {}, { cacheOptions: { ttl: 600 } });
}
async updateMovie(id, data) {
const movie = await this.put(`movies/${id}`, data);
// invalidate cache here?!
return movie;
}
}
我知道传递给 ApolloServer 的KeyValueCache 接口提供了一个delete 函数。但是,此对象似乎并未在数据源中公开。它被包裹在HTTPCache 中,它只公开了一个fetch 函数。 KeyValueCache 也包裹在 PrefixingKeyValueCache 中,因此假设 RESTDataSource 的内部实现,如果没有一些讨厌的黑客攻击,似乎几乎不可能在缓存中隐藏某些东西。
【问题讨论】:
标签: caching apollo-server apollo-datasource-rest