Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rename a hash field efficiently in Redis

Tags:

redis

I can HGET mykey oldfield and store value in my application and do HSET newfield value, how can I avoid copy my value just like RENAMENX key newkey while using hash? Thanks.

like image 938
Zoozy Avatar asked Oct 17 '25 07:10

Zoozy


1 Answers

There is no specific command to do it, but you can use a server-side Lua script to execute the following operations atomically:

  • hget key field
  • hsetnx key newfield value
  • if successful, hdel key field

You will pay for only one roundtrip, and you can handle errors (when the new field does already exist).

like image 133
Didier Spezia Avatar answered Oct 21 '25 10:10

Didier Spezia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!