rtkquery流,如何限制缓存条目?

发布于 2025-01-21 08:04:28 字数 633 浏览 2 评论 0原文

因此,我使用此模式 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无法回应 2025-01-28 08:04:28

每次您.push() updateQueryResults中的数组中的项目,请检查长度现在是否超过30,并且还调用.unshift()在阵列上?

您不需要任何特殊工具就可以做到这一点。

Every time you .push() an item into the array in updateQueryResults, check if length is now over 30 and also call .unshift() on the array?

You don't need any special tools for doing that.

此刻的回忆 2025-01-28 08:04:28

Phry方向是正确的,这是一个工作实施示例:

lastTransferTxSocket.on('token-tx', (tx) => {
    const data:TokenTxInterface = JSON.parse(tx);
    updateCachedData((draft) => {
        const currentCache = getCacheEntry();
        if(currentCache.data && currentCache.data.ids.length === MAX_ENTRY) {
            const toRemove = currentCache.data.ids[0]
            tokenTxAdapter.removeOne(draft, toRemove);
        }
        tokenTxAdapter.addOne(draft, data);
    })
})

phry direction is the right one , here is a working implementation example:

lastTransferTxSocket.on('token-tx', (tx) => {
    const data:TokenTxInterface = JSON.parse(tx);
    updateCachedData((draft) => {
        const currentCache = getCacheEntry();
        if(currentCache.data && currentCache.data.ids.length === MAX_ENTRY) {
            const toRemove = currentCache.data.ids[0]
            tokenTxAdapter.removeOne(draft, toRemove);
        }
        tokenTxAdapter.addOne(draft, data);
    })
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文