-
Notifications
You must be signed in to change notification settings - Fork 23.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for hmsetnx #542
Comments
Sumanth and I worked on a patch for this. Please be gentle and provide any feedback since this is the first time we've looked at the code base: |
+1 |
2 similar comments
+1 |
+1 |
Since this request seems to have exponentially increasing interest, I added Example usage: 127.0.0.1:6379> config set module-add /home/matt/repos/krmt/hmsetnx.so
OK
127.0.0.1:6379> hmsetnx bob field1 value1 field2 value2 field3 value3
(integer) 1
127.0.0.1:6379> hmsetnx bob field1 value1 field2 value2 field3 value3
(integer) 0 |
@mattsta thanks for providing this as modules, however there is a specific reason why Redis has no a plugin-system by design, that is, we don't want to debug code which is non stable because of third party plugins, and, users will jump creating stuff which is often not needed and we end with an ecosystem of slightly different instances / APIs without a good reason. So who is using plugins, is on his/her own, I'm not going to consider bug reports / crashes / issues if there is a plugin loaded. For the same reason, please could you make sure (if not already) that krmt reports the bug report with a very visible warning that this instance had krmt modules loaded? Thanks! |
About the |
I've got one right now. I'm using a hash instead of a set to turn this structure:
into
Having The current structure requires me to add the Apologies for the variable soup, hope that makes some amount of sense. It'd be really helpful to have this. |
Since I still don't see this in the latest redis, here's my use case:
The only alternative I have currently is to do multiple HSETNX for each attribute that has a parallel HINCRBY. |
We've recently updated to 3.0 on our systems. I've updated from 3.0 upstream to include the updated hmsetnx patch along with some unit tests Hope this helps |
Running across this as well and HMSETNX would be very helpful. +1! |
I needed this, so I worked out a way to do it with Lua. Of course native would be better in a future version if possible.
|
👍
My personal use-case is a task which sets up stats about itself when queued and deletes them when it has finished running. In some-cases I would like to guarantee task uniqueness - |
+1 |
Adding to the use cases, I have the following use case: Hashmap holds information that's discovered by a crawler that runs every so often. There are fields within the hash I store that will need updating, but a I suppose there's a distinction between |
+1 |
I think HMSETNX is more friendly to a redis cluster than "MSETNX". I have the following use case: for (Sku sku : skuList){
if (redis.setnx("lock_sku_" + sku.id, "") == 0) {
throw new SkuModifyException();
}
} Stock modify request comes from several ways, one is that user upload an excel like:
One day, user upload an excel contains 100,000 records, the setnx is called by 100,000 times, it's inefficiency. “MSETNX” may help? |
I have an iteration which detect unique words and its ctypes and counts.
If i want to do this with redis;
with HMSETNX command these operation could be much more clear.
|
+1 |
1 similar comment
+1 |
+1. It's been 6 years, how is this still not implemented? Seems like such a self-evident feature. |
TBH I ended up just using the redis lua interface and it's a pleasure to use and perfect for this and many other situations like this one. I understand if this functionality has not been prioritized. |
@johanbrandhorst I assume you end up sending 2 separate commands, so things are no longer atomic? I need (or at least, strongly desire) the atomicity that an |
Anything that happens inside a Lua script is done atomically, so you maintain atomicity. |
@johanbrandhorst Game-changer alert! I wasn't aware scripts were atomic. Thanks for the info! |
Would be nice to have this in Redis core, but doesn't look like it's likely to happen. Ref. redis/redis#542 Note that there's another Lua implementation floating around (https://redisgreen.net/library/hmsetnx.html), but that doesn't account for a pre-existing hash key without the given fields.
Would be nice to have this in Redis core, but doesn't look like it's likely to happen. Ref. redis/redis#542 Note that there's another Lua implementation floating around (https://redisgreen.net/library/hmsetnx.html), but that doesn't account for a pre-existing hash key without the given fields.
+1 |
Would be nice to have this in Redis core, but doesn't look like it's likely to happen. Ref. redis/redis#542 Note that there's another Lua implementation floating around (https://redisgreen.net/library/hmsetnx.html), but that doesn't account for a pre-existing hash key without the given fields.
Would be nice to have this in Redis core, but doesn't look like it's likely to happen. Ref. redis/redis#542 Note that there's another Lua implementation floating around (https://redisgreen.net/library/hmsetnx.html), but that doesn't account for a pre-existing hash key without the given fields.
Need this implemented!!! without In my case, most of time the key should not exist, so the query is very likely a waste of effort. |
Would be nice to have this in Redis core, but doesn't look like it's likely to happen. Ref. redis/redis#542 Note that there's another Lua implementation floating around (https://redisgreen.net/library/hmsetnx.html), but that doesn't account for a pre-existing hash key without the given fields.
Would be nice to have this in Redis core, but doesn't look like it's likely to happen. Ref. redis/redis#542 Note that there's another Lua implementation floating around (https://redisgreen.net/library/hmsetnx.html), but that doesn't account for a pre-existing hash key without the given fields.
Would be nice to have this in Redis core, but doesn't look like it's likely to happen. Ref. redis/redis#542 Note that there's another Lua implementation floating around (https://redisgreen.net/library/hmsetnx.html), but that doesn't account for a pre-existing hash key without the given fields.
Would be nice to have this in Redis core, but doesn't look like it's likely to happen. Ref. redis/redis#542 Note that there's another Lua implementation floating around (https://redisgreen.net/library/hmsetnx.html), but that doesn't account for a pre-existing hash key without the given fields.
+1 |
So it looks like there are three cases in this thread:
The command could look like
XX: Only update elements that already exist. Never add elements. It would diverge from ZADD, which doesn't allow flags to be This seems reasonably popular enough we could have someone implement it. @redis/core-team Thoughts on implementing this? |
I'm a bit uncomfortable with the DEL feature, what if the existing key is not a hash, do we want to return a WRONGTYPE error, or override it? Given that it's a popular request (specifically the XX and NX features i guess), i think we should indeed implement it (i see Salvatore thought so too). We need to carefully define the response type, maybe like ZADD, it also needs the CH flag? what seems straight forward to me is just the XX, NX and CH flags. |
Hi @oranagra @madolson I am not sure if XX, CH, or other flags are needed currently. Just for this issue. I think it is easy to solve if HSETNX command supports multi fields and it is compatible with old version, just like HSET command supports multi fields. |
@ShooterIT I agree it would resolve this issue, but I would still prefer to also release this with a more generic HMSET with flags. It's a pretty common pattern, to have a generic add with a specific implementation. Since it will likely involve refactoring into a generic function, I wouldn't decouple them. |
I agree, I rather extend the capabilities of HSET than improve HSETNX (same as SETNX was abandoned in favor of SET NX). But on the other hand the HSET command is taken, and impossible to extend, and improving HSETNX is so straight forward. I'm also not sure I'm a fan of the FIELDS argument. |
I tend to agree w/ @ShooterIT's reasoning, providing we don't need However, if we want to be ahead of the curve (give or take a decade), I can imagine people finding the use cases for the functionality. I would propose, however, that Furthermore, like Oran, the |
@itamarhaber so you mean that This gets more and more complicated, and compared to the really straight forward variadic HSETNX, i'm not sure what's the right move here. |
HMSETNX will be very usefull when I tried to prevent duplicated record,such as unique key from phone number and etc. 12years past , I hope this could be fixed. |
Currently redis supports hmset. It would be really useful to have "nx" functionality built into hmsetnx.
The text was updated successfully, but these errors were encountered: