此 RedisCache 实现使用哪种数据类型? [英] Which datatype to use for this RedisCache implementation?

查看:67
本文介绍了此 RedisCache 实现使用哪种数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据库表结构:

I have the below DB table structure:

Id(string)  Type(string)  BeginDate(datetime) CloseDate(dateime) Source(string)
"+ww100"     "L"           23-JAN-20               23-APRIL-20     XYZ
"+ww100"     "L"           23-JAN-20               23-APRIL-20     XYZ
 ---         ---              ---                      ---         --- 

您可能已经观察到,该表没有任何主键,这意味着可能存在重复数据.现在我需要将此表数据存储在 Redis 缓存中并随后检索它.示例:我可能想根据 Id 进行搜索,即使有多个记录,我也想检索它们并进行处理.由于我是 Redis 的新手,您能否建议我在此用例中使用哪种数据类型?由于 Key 不是唯一的,因此我认为将不可能存储为字典类型的数据结构!提前致谢.

As you might have observed, this table does not have any primary key, which means there could be duplicate data. Now I need to store this table data in Redis cache and retrieve it subsequently. Example: I might wanna search based on the Id, even if there are multiple records, I want to retrieve them all and do the processing. Since I am a newbie to Redis, could you please suggest me which datatype to use for this use-case? Since the Key's are not unique, storing as a dictionary type of data structure will not be possible I think! Thanks in advance.

推荐答案

由于您对检索或修改记录中的字段不感兴趣,但大多数时候检索整个记录,您可以将其序列化为您喜欢的格式, 像 JSON 或简单地像:

Since you are not interested in retrieving or modifying a field in a record, but most of the time retrieve the whole record, you can serialize it in your preferred format, like JSON or simply as:

+ww100|L|23-JAN-20|23-APRIL-20|XYZ

| 或您首选的分隔符分隔,确保您的分隔符不会成为数据字段的一部分或相应地转义.

Separating by | or your preferred separator, make sure your separator won't be part of a data field or escape accordingly.

使用排序集

由于两条相同的记录没有什么区别,因此您可以简单地保留一个计数器.

As there is nothing to differentiate from two records that are the same, you can simply keep a counter.

假设您正在存储:

Id(string)  Type(string)  BeginDate(datetime) CloseDate(dateime) Source(string)
"+ww100"     "L"           23-JAN-20               23-APRIL-20     XYZ
"+ww100"     "L"           23-JAN-20               23-APRIL-20     XYZ
"+ww101"     "E"           24-JAN-20               24-APRIL-20     ABC 

您使用 ZADD 插入,使用 INCR代码>选项.如果它是新的,它将插入.如果是重复的,则会增加计数.

You insert with ZADD, using INCR option. If it is new, it will insert. If it is a duplicate, it will increase the count.

> ZADD myData INCR 1 +ww100|L|23-JAN-20|23-APRIL-20|XYZ
"1"
> ZADD myData INCR 1 +ww100|L|23-JAN-20|23-APRIL-20|XYZ
"2"
> ZADD myData INCR 1 +ww101|E|24-JAN-20|24-APRIL-20|ABC
"1"
> ZRANGEBYSCORE myData -inf +inf WITHSCORES
1) "+ww101|E|24-JAN-20|24-APRIL-20|ABC"
2) "1"
3) "+ww100|L|23-JAN-20|23-APRIL-20|XYZ"
4) "2"

注意重复记录如何出现一次但带有计数.

Note how the duplicated record appears once but with the count.

然后您可以使用 ZSCAN 查询给定的 ID 以获得与 ID 匹配的所有记录:

You can then query for a given ID using ZSCAN to get all the records matching an ID:

> ZSCAN myData 0 MATCH "+ww100|*"
1) "0"
2) 1) "+ww100|L|23-JAN-20|23-APRIL-20|XYZ"
   2) "2"

ZSCAN 的缺点是您可能需要多次调用,直到光标回到零,并且您正在遍历服务器端的所有记录.

The downside of ZSCAN is that you may need to call multiple times until you get the cursor back in zero, and you are iterating through all the records server-side.

对每个 ID 使用排序列表

如果您想获得每个 ID 查询的最佳性能,那么每个 ID 使用一个排序集.

If you want to have best performance to query per ID, then use one sorted set per ID.

保留一个包含所有 ID 的集合.

Keep a set with all IDs.

要存储,然后使用 SADD 先添加/确保身份证:

To store, then you use SADD first to add/ensure the ID:

> SADD myDataIDs +ww100
(integer) 1
> ZADD myData:+ww100 INCR 1 +ww100|L|23-JAN-20|23-APRIL-20|XYZ
"1"

这里的缺点是要获取所有记录,需要调用SMEMBERS myDataIDs获取所有ID,然后为每个ID调用ZRANGEBYSCORE.

The downside here is that to get all records, you need to call SMEMBERS myDataIDs to get all IDs, and then call ZRANGEBYSCORE for each ID.

确保在适当的时候使用 pipelining 来节省往返时间.

Make sure to use pipelining to save Round Trip Times when appropriate.

并且你可以使用Lua脚本来优化一些操作.这里以获取所有记录为例:

And you can use Lua scripts to optimize some operations. Here for example to get all records:

local keys = redis.call('SMEMBERS', KEYS[1])
local r = {}
for i, key in ipairs(keys) do
  r[i] = redis.call('ZRANGEBYSCORE', 'myData:'..key, '-inf', '+inf', 'WITHSCORES')
end
return r

这篇关于此 RedisCache 实现使用哪种数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆