I've been doing a bunch of works with sets recently (for
http://mps-expenses2.guardian.co.uk which primarily uses Redis for
random item selection with SRANDMEMBER). Currently, sets can be
composed either by multiple calls to SADD or using the extremely
useful SUNIONSTORE/SDIFFSTORE etc commands. The one case that isn't
covered is quickly building up a new set from scratch - that currently
takes multiple SADD commands.
Would it be appropriate for Redis to add a SADDMULTI command which can
take an arbitrary number of values and add them to a set all in one
atomic unit? It would have made a few tasks for the mps-expenses2
project more convenient. As it was, I had to build up temporary sets
and then RENAME them to ensure atomicity.
<si...@simonwillison.net> wrote:
> Would it be appropriate for Redis to add a SADDMULTI command which can
> take an arbitrary number of values and add them to a set all in one
> atomic unit? It would have made a few tasks for the mps-expenses2
> project more convenient. As it was, I had to build up temporary sets
> and then RENAME them to ensure atomicity.
Hello Simon!
yes, it's planned, will probably be called MSADD, and will work in the
following way:
So it will be a bit more general, for example allowing to add the same
two elements to multiple sets at once:
MSADD 3 2 set1 set2 set3 foo bar
Not sure if I can add this for 1.4, probably yes as it should not be
too complex to implement, but at the same time I need a refactoring of
the Sets code as I want to implement specialized sets representations
for sets composed of small (< signed 64bit) integers in a
memory-efficient way.
But at some time this command will surely get inside.
> So it will be a bit more general, for example allowing to add the same
> two elements to multiple sets at once:
> MSADD 3 2 set1 set2 set3 foo bar
That's excellent - much more useful than what I was thinking about (I
can see how it would have helped with my most recent project). I
imagine the client libraries will hide the details of the API -
redis.msadd(['set1', 'set2', 'set3'], ['foo', 'bar']) for example.
<si...@simonwillison.net> wrote:
> imagine the client libraries will hide the details of the API -
> redis.msadd(['set1', 'set2', 'set3'], ['foo', 'bar']) for example.
Yes, that's exactly my idea for the API. Also if the argument is not a
list but a string or symbol it should be considered like a
single-element list, so that this forms will work: