The problem/use-case that the feature addresses
I'm using a long list of strings why may change between project releases. I want to have a quick and transparent translation function which converts a number of keys to "updated" keys or return the input key if not found in the translation hash.
Description of the feature
Just like HMGET this function return found fields, but returns the input field instead of Null values. For example:
redis> HSET translation foo "foe"
(integer) 1
redis> HMTR translation foo bar baz
1) "foe"
2) "bar"
3) "baz"
Alternatives you've considered
I'm using a Python script to communicate with Redis and I could use HGET for every single key, checking if it's empty and if so, replace it with the key name. However this requires a loop in Python which communicates and (de)serializes a bunch of strings every time. I image a "translation" function within Redis is much faster.
Additional information
I'm happy to implement this feature if it seems of general value to other people as well.
Comment From: madolson
This seems much more like code that should be implemented in the client side to me, using the algorithm that you mentioned. I'm not convinced the performance benefit will be all that significant, doing a loop over some strings seems pretty negligible. Happy to leave this open to see if other people see value, or see a different implementation that is more generic, but for now I'm not inclined to add this feature.
Comment From: aparcar
@madolson Sure I understand it's a bit of a corner case, I might just implement it and see if a benchmark shows actually any advantage.
Current Python implementation works fine
tr = set()
for p in req["packages"]:
p_tr = r.hget("mapping-abi", p)
if p_tr:
tr.add(p_tr.decode())
else:
tr.add(p)
req["packages"] = tr
If you don't mind leave this open for another few days and see if anyone comments.
Comment From: madolson
@aparcar Of course, I'll leave it open.
Comment From: leikang123
该功能解决的问题/用例
我正在使用一长串字符串,为什么可能会在项目版本之间发生变化。我想要一个快速且透明的_翻译_功能,它将多个键转换为“更新”键,或者如果在_翻译_哈希中找不到,则返回输入键。
功能描述
就像_HMGET_这个函数返回找到的字段,但返回输入字段而不是 Null 值。例如:
redis> HSET translation foo "foe" (integer) 1 redis> HMTR translation foo bar baz 1) "foe" 2) "bar" 3) "baz"您考虑过的替代方案
我正在使用 Python 脚本与 Redis 通信,我可以使用
HGET每个键,检查它是否为空,如果是,则将其替换为键名。然而,这需要 Python 中的一个循环,它每次都通信和(反)序列化一堆字符串。我想象 Redis 中的“翻译”功能要快得多。附加信息
如果它对其他人也具有普遍价值,我很高兴实现此功能。
Your idea of this function is really good, but the value used in actual business projects is relatively small. If you are a little careless, you will encounter performance problems, involving algorithms, but it is best to implement a new data structure at the bottom. To achieve this function, it is better to ensure stability.