- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
有两个页面(或者,关于 vue 术语的组件)都需要相同的数据集,这些数据是通过 http 上的 api 提供的。访问这两个组件的顺序是未定义的(或者,取决于用户行为)并且数据应该只获取一次,因为它不会改变很多。
我知道 state
存储实际数据,mutation
改变 state
和 action
s 做异步请求、多突变协调等肮脏的工作。
问题是:执行上述某些缓存逻辑的最佳做法是什么?
我想出了以下三种方法,但对我来说没有一种是完美的:
缺点:
我需要在访问任何地方的数据之前分派(dispatch)操作,因为我不知道数据是否已经被提取。
// ComponentA
async mouted () {
await this.store.dispatch('fetchData')
this.someData = this.store.state.someData
}
// ComponentB
async mouted () {
await this.store.dispatch('fetchData')
this.someData = this.store.state.someData
}
// vuex action
{
async fetchData ({ state, commit }) {
// handles the cache logic here
if (state.someData) return
commit('setData', await apis.fetchData())
}
}
缓存逻辑散布在整个代码库中——臭~
// ComponentA
async mouted () {
if (this.store.state.someData === undefined) {
// handles the cache logic
await this.store.dispatch('fetchData')
}
this.someData = this.store.state.someData
}
// ComponentB
async mouted () {
if (this.store.state.someData === undefined) {
// handles the cache logic
await this.store.dispatch('fetchData')
}
this.someData = this.store.state.someData
}
// vuex action
{
async fetchData ({ state, commit }) {
commit('setData', await apis.fetchData())
}
}
可能是这三者中最完美的,但是我觉得用action dispatch的返回值作为数据有点奇怪。随着存储的增长,缓存逻辑将分散在所有操作中(会有越来越多的操作重复相同的缓存逻辑)
// ComponentA
async mouted () {
this.someData = await this.store.dispatch('fetchData')
}
// ComponentB
async mouted () {
this.someData = await this.store.dispatch('fetchData')
}
// vuex action
{
async fetchData ({ state, commit }) {
if (!state.someData) commit('setData', await apis.fetchData())
return state.someData
}
}
我更喜欢将缓存逻辑放入我的“vue&vuex-independent”网络层。但是网络层的“缓存”部分可能会变成另一个“vuex”存储。 XD
最佳答案
我觉得我正在重新发明轮子,应该有更好的现成的东西来做到这一点,但这是我的轮子,它对我来说滚动得很好。
我回复是为了回馈,但也因为我希望有人能告诉我更好的方法!
我的方法通过使用“加载” promise 而不是简单地检查特定状态是否已加载来解决避免多次提取的问题。如果您的 api 很慢和/或您的组件可能会在呈现之前多次调用获取操作,这将帮助您将它保持为单个 api 调用。
// vuex action
async fetchCustomers({commit, state}) {
await loadOrWait(loadingPromises, 'customers', async () => {
// this function is called exactly once
const response = await api.get('customers');
commit('SET_CUSTOMERS', {customers : response.data });
});
return state.customers;
},
loaded 只是一个对象,它存储了每次获取的 promise 。我看过someone改用弱映射(如果内存使用是一个问题,那将是一个不错的选择)
const loadingPromises = {
customers: false,
customer: { }
}
loadOrWait
是一个简单的实用程序函数,它要么执行(恰好一次)一个函数(例如,您从 api 中提取)要么它看到提取正在进行中,所以它返回上一个 fetch 调用的 promise 。在任何一种情况下,您都会得到一个 promise ,该 promise 将解析为 API 调用的结果。
async function loadOrWait(loadingPromises, id, fetchFunction) {
if (!loadingPromises[id]) {
loadingPromises[id] = fetchFunction();
}
await loadingPromises[id];
}
也许您想要更细化您的抓取,例如,如果尚未抓取特定客户,则抓取他们。
// vuex action
async fetchCustomer({commit, state}, customerId) {
await loadOrWait(loadingPromises.customer, customerId, async () => {
const response = await api.get(`customers/${customerId}`);
commit('SET_CUSTOMER', { customerId, customer: response.data });
});
return state.customer[customerId];
},
一些其他想法:
loadOrWait
中发挥创意,例如,像这样setTimeout(()=>loadingPromises[id]=false,60*1000)
loadOrWait
保存函数也很简单!当您执行它们时,它会更新状态,如果您是一名优秀的 vue 编码员,您的应用程序将会刷新。const fetchFunctionMap = {};
async function loadOrWait(loadingPromises, id, fetchFunction) {
if (!loadingPromises[id]) {
loadingPromises[id] = fetchFunction();
// save the fetch for later
fetchFunctionMap[id] = fn;
}
await loadingPromises[id];
}
async function reload(loadingPromises, id) {
if (fetchFunctionMap[id]){
loadingPromises[id] = fetchFunctionMap[id]();
}
return await loadingPromises[id];
// strictly speaking there is no need to use async/await in here.
}
//reload the customers every 5 mins
setInterval(()=>reload(loadingPromises,'customers'), 1000 * 60 * 5);
关于vue.js - 使用 vuex 从 api 获取数据的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51121690/
本文分享自华为云社区《大模型LLM之分布式训练》,作者: 码上开花_Lancer。 随着语言模型参数量和所需训练数据量的急速增长,单个机器上有限的资源已无法满足大语言模型训练的要求。需要设计分布式训
本文分享自华为云社区《五大基础算法--动态规划法》,作者: 大金(内蒙的)。 一、基本概念 动态规划法,和分治法极其相似。区别就是,在求解子问题时,会保存该子问题的解,后面的子问题求解时,可以直接拿来
pip install scp pip install pexpect 测试代码: import os import stat import paramiko # 用于调用scp命令 def s
我目前正在实现“ token ”REST 服务。 token 只是一个字符串,由一些参数构建而成,然后经过哈希处理并在一定时间后过期。 我想在我的 REST 服务中有一个可以验证 token 的端点,
打开软删除后,我在客户端上添加一条记录,推送,删除添加的记录推送,然后尝试使用与初始记录相同的主键添加新记录(然后推送),我得到一个异常(exception)。 EntityDomainManager
打开软删除后,我在客户端上添加一条记录,推送,删除添加的记录推送,然后尝试使用与初始记录相同的主键添加新记录(然后推送),我得到一个异常(exception)。 EntityDomainManager
我有一个应用程序,每 x 秒接收一次天气信息。我想将此数据保存到 XML 文件中。 我应该为每个天气通知创建一个新的 XML 文件,还是将每个通知附加到同一个 XML 文件中?我不确定 XML 标准的
我猜我们大多数人都必须在某个时候处理这个问题,所以我想我会问这个问题。 当您的 BLL 中有很多集合并且您发现自己一遍又一遍地编写相同的旧内联(匿名)谓词时,显然有必要进行封装,但实现封装的最佳方
我有一些 c# 代码已经运行了一段时间了..我不得不说,虽然我了解 OO 原则的基础知识,但显然有不止一种方法可以给猫剥皮(尽管我讨厌那个短语!)。 因此,我有一个基本抽象类作为基本数据服务类,如下所
我设计了一个 SQL 数据库系统(使用 Postgre),我有一个问题,即创建一个关系/引用的常见做法是什么,这种关系/引用即使在引用的对象被删除时也能持续存在。 比如有一个UserORM,还有Act
我们的目标是搜索用户输入的字符串并计算在其中找到多少元音。不幸的是我被困在这里,有什么帮助吗? def numVowels(s): vowels= "AEIOUaeiou" if s
我有一个适用于我的“items”int 数组的旋转函数。下面的代码完成了它,除了我不必要地传输值。我正在努力实现“就地”轮换。我的意思是 ptrs 会递增或递减,而不是从数组中获取值。我需要通过这种方
我有一个 json 存储在我的应用程序文档文件夹中,我需要在我的所有 View 中使用它。我正在加载 json 并将其添加到每个 View 中的 NSMutableArray。但现在我了解到,我可以将
我用 C++ 开始了一个项目。这种语言的内存管理对我来说是新的。 我过去常常使用 new () 创建对象,然后传递指针,虽然它可以工作,但调试起来很痛苦,人们看到代码时会用有趣的眼神看着我。我为它没有
已结束。 这个问题是 off-topic .它目前不接受答案。 想要改进这个问题? Update the question所以它是on-topic堆栈溢出。 关闭 10 年前。 Improve thi
保持类松散耦合是编写易于理解、修改和调试的代码的一个重要方面——我明白这一点。然而,作为一个新手,几乎任何时候我都会超越我所苦苦挣扎的最简单的例子。 我或多或少地了解如何将字符串、整数和简单数据类型封
我发现我需要编写大量重复代码,因为我无法从其他 Controller 调用函数。例如,这里新闻提要内容在我的代码中重复,我对一个 Controller 做一些特定的事情,然后需要像这样加载我的新闻提要
假设需要一种数字数据类型,其允许值在指定范围内。更具体地说,假设要定义一个整数类型,其最小值为0,最大值为5000。这种情况在很多情况下都会出现,例如在对数据库数据类型,XSD数据类型进行建模时。 在
假设我想循环整个数组来访问每个元素。使用 for 循环、for...in 循环或 for...of 循环是 JavaScript 开发人员的标准做法吗? 例如: var myArray = ["app
我有一个旧的 SL4/ria 应用程序,我希望用 Breeze 取代它。我有一个关于内存使用和缓存的问题。我的应用程序加载工作列表(一个典型的用户可以访问大约 1,000 个这些工作)。此外,还有很多
我是一名优秀的程序员,十分优秀!