具有 Flux 模式的中继缓存? [英] Relay Cache with Flux Pattern?

查看:13
本文介绍了具有 Flux 模式的中继缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很想将 Relay 缓存合并到我的 Flux 存储中,这样我就可以进行时间旅行"并深入了解应用程序.

I'd really like to incorporate the Relay cache within my Flux store so I can do "time-travel" and gain deep insight into the application.

看起来中继存储和操作都是不可序列化的类,这很糟糕.但看起来我应该能够将缓存与网络请求分开,并将缓存保存在 Flux 存储中.

It looks like the Relay store and actions are all classes which aren't serializable which is bummer. But it looks like I ought to be able to separate the cache from the network requests and save the cache in a Flux store.

这听起来很有趣还是我说错了树?

Does that sound interesting or am I barking up the wrong tree?

推荐答案

Relay 当然可以与 Flux 一起使用,我们已经与许多成功地一起使用它们的开发人员进行了交谈.一般的模式是让 Relay 拥有服务器数据的缓存并管理与服务器的通信,并使用 Flux 来存储 &更新仅限客户端的数据.

Relay can certainly be used alongside Flux, and we've spoken to many developers who are using them together successfully. The general pattern is to let Relay own the cache of server data and manage communication with the server, and use Flux for storing & updating client-only data.

如果 Flux 存储需要访问服务器数据,他们可以使用 Relay.Store API 从服务器获取数据并从缓存中读取:

If the Flux stores need access to server data, they can use the Relay.Store APIs to fetch data from the server and read it from the cache:

// build a query 
var query = Relay.createQuery(Relay.QL`query { ... }`, {var: 'value'});
// fetch any missing data for this query
Relay.Store.primeCache({query}, readyState => {
  if (readyState.done) {
    // read data once the cache is populated
    var data = Relay.Store.readQuery(query)[0];
  }
});

检查中继缓存

Relay 不直接支持时间旅行调试.但是,我们正在积极开发 Relay 的开发者工具,它的初始版本应该很快就会推出.同时,有几个选项可以检查缓存的状态:

Inspecting the Relay Cache

Relay doesn't directly support time-travel debugging. However, we are actively working on developer tools for Relay and the initial version of this should be available soon. In the meantime there are a few options for inspecting the state of the cache:

  • 拦截并记录中继存储的所有低级更新.这可以通过使用 RelayStoreData.getDefaultInstance().injectCacheManager(...) 注入缓存管理器来完成(注意 API 名称可能会改变,但缓存管理器 API 本身是稳定的).CacheManager 接口是在此处定义 -请注意,这将允许您记录写入 Relay 缓存的所有值,以构建您想要的任何数据可视化.写入缓存管理器的所有字段值都是 JSON 可序列化的.请注意,缓存管理器无法将数据写回存储中,因此这主要允许您了解缓存的当前状态.
  • 通过注入 自定义网络层.这将指示您的应用程序正在请求什么数据以及它从服务器接收什么数据.
  • Intercept and record all low-level updates to the Relay store. This can be done by injecting a cache manager with RelayStoreData.getDefaultInstance().injectCacheManager(...) (note that the API names may change, but the cache manager API itself is stable). The CacheManager interface is defined here - note that this will allow you to record all the values written to Relay's cache to build up any visualization you'd like of the data. All of the field values written to the cache manager are JSON-serializable. Note that the cache manager cannot write data back into the store, so this would primarily allow you to have visibility into the current state of the cache.
  • Intercept all query and mutation requests at the network layer by injecting a custom network layer. This will give an indication of what data your application is requesting and what it's receiving back from the server.

这篇关于具有 Flux 模式的中继缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆