Marconi
Marconi

Reputation: 3651

getting Redis list item index

I have a list and I'm adding elements using RPUSH which returns the updated length of the list. My question is that, is it reliable to use the returned length - 1 as the index of the newly inserted item?

By reliable I mean, If I have multiple connections to Redis doing the same operation is it guaranteed there won't be any overlapping like the returned length actually returning after two RPUSH that occurred simultaneously or is that Redis' list operation atomic by default or do I need transactions?

Upvotes: 2

Views: 1268

Answers (1)

hymloth
hymloth

Reputation: 7035

Considering the atomicity of redis commands, you can be 100% sure that every RPUSH will be followed by the corresponding integer reply, and thus it is safe to assume that the index of your element is (integer_reply - 1). I have answered a somehow related question here.

Upvotes: 2

Related Questions