> I think Redis works like a *map<string, string>* and *map<string,
> set<string>>* since 2.4.0 with members.
> I think there is no "sort" concept in Redis, unless I use a ZADD but if I
> use ZADD I have to precise a sort value myself (because there is no
> auto-increment)... That's the reason why I prefer to use SADD.
>
> My questions are :
>
> 1. Can you tell me if I am wrong with my comprehension about sort in
> Redis ?
Your comprehension is correct. Redis sets have no conception of sorting, or
the order of insertion.
> 2. Is it possible to get the last member inserted into a key ? Or should
> I use another key which represent the last inserted element ?
No. Yes. If you wish to ensure that there are no race conditions with
regards to setting that other key, you should use a Lua script to add the
item to the set and then set the other key (a Lua script runs atomically, so
the two operations are guaranteed to run without anything else getting in
the way), or use a MULTI/EXEC block to apply both operations together (as
described in
http://redis.io/topics/transactions).
There's an open question you may not have considered, though -- what happens
if the item you're adding to the set already exists? Do you want to keep a
record of the last item that was *added* to the set (that is, it wasn't
already in the set before you did the SADD) or do you want the value of the
last SADD call, regardless of whether that item was already in the set?
They're rather different things, and the code to achieve them will be quite
different.
- Matt