使用磁通模式中继缓存吗? [英] Relay Cache with Flux Pattern?

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

问题描述

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

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的开发人员工具,并且该工具的初始版本应尽快推出.同时,有一些检查缓存状态的选项:

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.

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

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