简体   繁体   English

Redis-排序字典

[英]Redis - Sorted Dictionary

Redis has the data structure sorted sets, which allows you to create a set that is sorted by some score value. Redis具有数据结构排序集,可让您创建按某个得分值排序的集。

There are several problems that I'm trying to solve 我正在尝试解决几个问题

  1. I need to store similar members but with different scores (not possible with sets). 我需要存储相似的成员,但得分不同(套组不可能)。 One solution is to concatenate the score with the original value and store it as the value, but it's kinda ugly. 一种解决方案是将分数与原始值连接起来并将其存储为值,但这有点丑陋。

  2. I need to have only one member per score and I need a way to enforce it. 每个分数只需要一名成员,而且我需要一种方法来强制实施。

  3. I need to be able to update or remove member by score, like in a dictionary. 我需要能够按分数更新或删除成员,例如在字典中。

The best example of what I'm looking for is an order book I need to be able to set amount of a certain price, remove price and retrieve amounts and prices sorted by prices 我要查找的最好的示例是订单簿,我需要能够设置一定价格的数量,删除价格并检索数量以及按价格排序的价格

SET orderBook_buy 1.17 30000
SET orderBook_buy 1.18 40000
SET orderBook_buy 1.19 40000
SET orderBook_buy 1.17 35000 // Override the previous value of 1.17
DEL orderBook_buy 1.18 // 1.18 was sold out

I think it can be done if I combine sorted sets and hash tables 我认为如果将排序集和哈希表结合起来就可以做到

I keep the prices in sorted sets 我把价格分类整理

ZADD orderBook_buy_prices 1.17 1.17
...
ZREM orderBook_buy_prices 1.18

And the amounts in hash tables, by prices 以及哈希表中的金额(按价格)

HSET orderBook_buy 1.17 35000
...
HDEL orderBook_buy 1.17

It could work but i have to do 2 reads and 2 writes every time, and also make sure that the writes are inside a transaction. 它可以工作,但我每次必须进行2次读取和2次写入,并且还要确保该写入在事务内。

Is there a data structure in redid that support sorted dictionaries out of the box (Could be a module)? redid中是否有数据结构支持开箱即用的排序字典(可能是模块)?

Thank you. 谢谢。

It could work but i have to do 2 reads and 2 writes every time, and also make sure that the writes are inside a transaction. 它可以工作,但我每次必须进行2次读取和2次写入,并且还要确保该写入在事务内。

You also want to do the reads in a transaction, unless you don't care about possible read consistency problems. 您还希望在事务中进行读取,除非您不在乎可能的读取一致性问题。

Is there a data structure in redid that support sorted dictionaries out of the box (Could be a module)? redid中是否有数据结构支持开箱即用的排序字典(可能是模块)?

Sorted Sets are just that, but what you're looking for is a single data structure that is a sort of a two way dictionary with ordering (albeit only on one subset of keys/values <- depending the direction you're coming from). 排序集就是这样,但是您要查找的是一个单一数据结构,该结构是一种带排序的双向字典(尽管仅对键/值的一个子集<-取决于您来自的方向) 。

Your approach of "welding" together two existing structures is perfectly valid, with the constraints you've pointed out about two keys and transactionality. 您将两个现有结构“焊接”在一起的方法是完全有效的,并且已经指出了关于两个键和事务性的约束。 You could use a Lua script to wrap the logic and not worry about transactions, but you'd still have it touch 2 keys via two ops. 您可以使用Lua脚本来包装逻辑,而不必担心事务,但是您仍然可以通过两个操作让它触摸2个键。

AFAIK there is no Redis module ATM that implements this data structure (although it should be possible to write one). AFAIK没有实现此数据结构的Redis模块ATM(尽管应该可以写一个)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 字典可以用不同的键排序吗? - Can a Dictionary be sorted by a different key? Redis Cluster如何处理排序集ZSET(及其他)的复制? - How does Redis Cluster handle replication for sorted sets ZSET (and others)? 是否有等效于 Redis 排序集 (zset) 的 Java 数据结构 - Is there a Java data structure equivalent to Redis sorted sets (zset) Redis是一个较大的排序集还是多个较小的排序集,具有更高的内存性能 - Is one large sorted set or many small sorted sets more memory performant in Redis 排序哈希表(map,dictionary)数据结构设计 - Sorted hash table (map, dictionary) data structure design 如何有效地将排序字典的字典展平为 numpy.arrays - How to efficiently flatten a dictionary of Sorted dictionaries to numpy.arrays 如果Redis Sorted Set是用Skip List实现的,为什么ZPOPMIN时间复杂度是O(log n)? - If Redis Sorted Set is implemented with Skip List, why ZPOPMIN time complexity is O(log n)? 需要排序字典,用于查找键小于或大于搜索值的值 - Need sorted dictionary designed to find values with keys less or greater than search value 排序集的目的是什么? - What is the purpose of sorted sets? 排序向量-AddItem - Sorted Vector - AddItem
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM