rtkquery流,如何限制缓存条目?
因此,我使用此模式 https:// redux-toolkit。 js.org/rtk-query/usage/streaming-updates
它运行良好,但我的问题是,我想将总条目限制为30(Websocket发送了很多,并且很快)。 我需要像FIFO这样的队列来保持我的列表更新,但有限。 这样做的模式是什么,我应该写它?
编辑:
我提出了您建议的解决方案,您认为此实现还可以吗? (实际上效果很好)
const currentCache = getCacheEntry();
if(currentCache.data && currentCache.data.ids.length === MAX_ENTRY) {
const toRemove = currentCache.data.ids[0]
nearTxAdapter.removeOne(draft, toRemove);
}
nearTxAdapter.addOne(draft, data);
So I implemented a websocket using this pattern https://redux-toolkit.js.org/rtk-query/usage/streaming-updates
it works fine but my issue is that I would like to limit the total entries to 30 (websocket is sending a lot, and fast).
I would need a queue like FIFO to keep my list updated but limited.
What would be the pattern to do that , how should I write it ?
EDIT:
I came up with the solution same you suggested, do you think this implementation is ok? (actually it works very well)
const currentCache = getCacheEntry();
if(currentCache.data && currentCache.data.ids.length === MAX_ENTRY) {
const toRemove = currentCache.data.ids[0]
nearTxAdapter.removeOne(draft, toRemove);
}
nearTxAdapter.addOne(draft, data);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每次您
.push()
updateQueryResults
中的数组中的项目,请检查长度现在是否超过30,并且还调用.unshift()
在阵列上?您不需要任何特殊工具就可以做到这一点。
Every time you
.push()
an item into the array inupdateQueryResults
, check if length is now over 30 and also call.unshift()
on the array?You don't need any special tools for doing that.
Phry方向是正确的,这是一个工作实施示例:
phry direction is the right one , here is a working implementation example: