我应该使用什么数据结构来缓存有序实体

时间:2023-02-01 20:02:37

I need to store in cache some items, like chat messages. I also need to slice these items on the key value range. For example (back to chat messages) the most common operation with cache will be getting chat messages from begin date to end date.

我需要在缓存中存储一​​些项目,比如聊天消息。我还需要在键值范围内对这些项进行切片。例如(回到聊天消息),最常见的缓存操作是从开始日期到结束日期获取聊天消息。

What data structure should I be considering? I was thinking about simple array, but it will work for O(n). Is there any data structure that will work faster?

我应该考虑哪种数据结构?我在想简单的数组,但它适用于O(n)。是否有更快的数据结构?

2 个解决方案

#1


1  

You can use a self balancing binary search tree like Red-Black Tree which stores the entries in ordered fashion and will provide the insert,delete,search in O(logn) in both average and worst case.

您可以使用像Red-Black Tree这样的自平衡二叉搜索树,它以有序的方式存储条目,并在平均和最差情况下以O(logn)提供插入,删除和搜索。

So when you need the chat message between a date interval you can search your RB-tree for data range which are already ordered.

因此,当您需要日期间隔之间的聊天消息时,您可以在RB树中搜索已订购的数据范围。

#2


0  

use an associative set, store you data in an array<data>, but use a hash table to store pair<data , arrayIndex>. this way you can search , insert and delete with o(1).

使用关联集,将数据存储在数组 中,但使用哈希表来存储对 。这样你就可以用o(1)进行搜索,插入和删除。 ,arrayindex>

#1


1  

You can use a self balancing binary search tree like Red-Black Tree which stores the entries in ordered fashion and will provide the insert,delete,search in O(logn) in both average and worst case.

您可以使用像Red-Black Tree这样的自平衡二叉搜索树,它以有序的方式存储条目,并在平均和最差情况下以O(logn)提供插入,删除和搜索。

So when you need the chat message between a date interval you can search your RB-tree for data range which are already ordered.

因此,当您需要日期间隔之间的聊天消息时,您可以在RB树中搜索已订购的数据范围。

#2


0  

use an associative set, store you data in an array<data>, but use a hash table to store pair<data , arrayIndex>. this way you can search , insert and delete with o(1).

使用关联集,将数据存储在数组 中,但使用哈希表来存储对 。这样你就可以用o(1)进行搜索,插入和删除。 ,arrayindex>