gpt4 book ai didi

Shift Redis keys(Shift Redis键)

转载 作者:bug小助手 更新时间:2023-10-25 16:26:00 25 4
gpt4 key购买 nike



In my Redis DB, I've got a list of keys saved in a format of prefix:<index>, so the sample DB looks like this:

在我的Redis数据库中,我有一个以prefix:格式保存的键列表 ,所以示例数据库看起来像这样:


prefix:0 = "lorem"
prefix:1 = "ipsum"
prefix:2 = "dolor"
prefix:3 = "sit"

The problem is when I try to delete prefix:1, because I need the ipsum gone for whatever reason, I'm left with a gap between indexes – the DB being like this:

问题是,当我尝试删除前缀:1时,因为无论出于什么原因,我都需要删除IPSUM,在索引之间留下了一个间隙--数据库是这样的:


prefix:0 = "lorem"
prefix:2 = "dolor"
prefix:3 = "sit"

And I need the DB to be like this:

我需要数据库是这样的:


prefix:0 = "lorem"
prefix:1 = "dolor"
prefix:2 = "sit"

How do I shift the indexes of keys to fill the gap (shifting prefix:2 to prefix:1 and prefix:3 to prefix:2) in Python with the redis-py library? All I can think of is looping through all keys with index greater than deleted index and rename them, but I'd prefer a bulk solution, if there's any.

如何使用redis-py库来移动键的索引以填补Python中的空白(将prefix:2转换为prefix:1,将prefix:3转换为prefix:2)?我所能想到的就是循环遍历索引大于DELETED INDEX的所有键,然后将它们重命名,但我更喜欢批量解决方案,如果有的话。


更多回答
优秀答案推荐

I'm not completely sure of what you are trying to solve here exactly by using redis and shifting keys inside of it. You could just delete prefix:3 rather than deleting prefix:1 and then shifting the remaining keys. I am not sure what I'm missing, please do add more clarity to your question.

我不能完全确定您在这里试图通过使用redis和在其中移动键来解决什么问题。您可以只删除Prefix:3,而不是删除Prefix:1,然后移动剩余的键。我不确定我漏掉了什么,请务必让你的问题更清楚一些。


Operation :  DELETE `prefix:1` and SHIFT <=> DELETE `prefix:3`
Input : `prefix:0` `prefix:1` `prefix:2` `prefix:3`
Output : `prefix:0` `prefix:1` `prefix:2`

更多回答

Just edited my question. Hope I clarified. The thing is, that I need to conserve the values of the keys and just rename them.

刚刚编辑了我的问题。希望我已经说清楚了。问题是,我需要保存键的值并将其重命名。

Will it be okay to use different data types like Redis lists? Will you know the value to delete as in ipsum or the key as in prefix:1 or both?

可以使用不同的数据类型吗,比如Redis列表?您知道要删除的值是IPSUM中的值,还是前缀:1中的键,还是两者都知道?

All of them are going to be the same type - string, and I will in fact know both the key and value in advance.

它们都将是相同的类型--字符串,事实上,我会提前知道键和值。

This sounds like a really inefficient approach to be managing manually - any reason why you wouldn't use a Redis list (needing separate time to live on each entry would be one reason not to). If you really do want to bubble up the key names to fill a gap in your sequence created by deleting one key, check out the RENAME command: redis.io/commands/rename

这听起来像是一种非常低效的手动管理方法--任何不使用Redis列表的原因(需要单独的时间来处理每个条目将是不使用的原因之一)。如果您确实想用键名称来填补通过删除一个键而创建的序列中的空白,请查看rename命令:redis.io/Commands/rename

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com