嘿,有什么方法可以设置 data-loader
(由 facebook 开发人员创建的缓存和批处理库)中的键的生存时间?
在他们的文档中只有 clearAll()
函数,但我想在将其插入缓存后 X 秒后分别清除每个键
最佳答案
DataLoader 的缓存仅在请求期间存在,并且特定于该单个请求。与 GraphQL 一起使用时,每个 DataLoader 实例都会使用请求的上下文重新创建。来自文档:
DataLoader caching does not replace Redis, Memcache, or any other shared application-level cache. DataLoader is first and foremost a data loading mechanism, and its cache only serves the purpose of not repeatedly loading the same data in the context of a single request to your Application. To do this, it maintains a simple in-memory memoization cache (more accurately: .load() is a memoized function).
虽然 DataLoader 实例上有 clear
和 clearAll
方法可用,但这些方法仅适用于您在其中运行多个突变的某些边缘情况。相同的请求和突变可能会导致同一请求中的缓存值过时。
虽然您可以预见地使用超时来实现您所要求的目标,但在设定的时间后不需要使缓存失效或清除缓存 - 一旦您的请求完成,它就会消失。
关于javascript - 设置数据加载器中键的生存时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57159378/