Home > Software engineering >  Set bits using node-redis with a custom offset
Set bits using node-redis with a custom offset

Time:12-03

I am having a bit of an issue figuring out the API. How can I go about using the API to send a request to Redis to execute something such as "bitfield somebf SET i4 #0 5"? i.e., set the first 4 bits at offset 0 to a 5 (0101). Currently, I execute setbit() 4 times setting the bits manually, which is rather inconvenient.

CodePudding user response:

The snippet of code that you are looking for is:

await client.bitField('key', [{
  operation: 'SET',
  encoding: 'i4',
  offset: '#0',
  value: 5
}])

The various commands and their arguments for Node Redis are not terribly well documented. My personal life hack for when I need to figure one of the more complex ones out—which happens quite a bit—is to take a look at the source code for the tests.

  • Related