Autoincrement in Redis -


I am starting using Redis, and I have participated in the following problem.

I have a bunch of objects, let's say message in my system every time a new user adds, I do the following:

  1. INCR is a global variable, say g_message_id , and g_message_id returns of INCR value (current code ).

  2. LPUSH New message in a list (including id and actual message).

    There is no new message to receive, using the values ​​of other clients g_message_id to check

    the problem is that a client INCR to g_message_id , but no time LPUSH message before attempting another client To read it, assuming that there is a new message.

    In other words, I'm looking for a way to equalize the addition of rows in SQL, and to work with an auto-indexed index.

    Notes :

    I can not use the list index, because I have to delete parts of the list often, it will be invalidated is.

    Current Solutions :

    The Best Solution I '

    But what do I plan to do? I'm trying to use watch and transaction . There is a common usage in Radis - that I am surprised that there is no current answer for this, so I am worried that I am doing something wrong.

    If I am read, then come in correctly, both of you as an id sequence and Using g_message_id to indicate new messages (indicators). One option is to split it into two variables: assign a message identifier and give the second one as a flag to indicate to provide a new message.

    The client then compares the current / previous value can g_new_message_flag to see if new messages when you are:

      & gt; INCR g_message_id (integer) 123 # code with id = 123 code & gt; Multi OK & gt; INCR g_new_message_flag QUEUED & gt; LPUSH g_msg_queue "{\" id \ ": 123, \" msg \ ": \" hey \ "}" trash " EXEC   

    Possible options, if your client can support it : You want to look at commands, e.g. Cients can publish notifications of new messages to receive notifications and subscribe to one or more message channels.

    Update : If you g_msg_queue Can keep. There are available messages to know, pop up all available, and zero the list, use the transaction to read an option list:

      # to handle the message queue " 123 "," 456 "," 789 ".. # Detects a client that has new messages, then runs it: & gt; Watch G_MSG_Q OK & gt; LRANGE g_msg_queue 0 100000 CAD & gt; DEL g_msg_queue QUEUED & gt; EXEC 1) "789" 2) "456" 3) "123" 2) (integer) 1   

    Update 2 : Looking at the new information , Here's what I will do:

    1. Use your author's customer RPUSH to add new messages to the list
    2. Read only the list of readers Remember the indicator of the previous message received from.
    3. to bring every reader client when new messages " LRANGE list index limit g_new_message_flag See. Assume that a reader customer has seen 5 messages in total, it will run " LRANGE g_msg_queue 5 15 " to get the next 10 messages. Let's say that 3 has returned, so this index remembers 8 . You can create as many borders as you like, and walk through the list in small batches.
    4. The deceiver customer should set a watch in the list and it should be deleted in one transaction, any client has to cancel reading from it alternately.
    5. When a reader client tries LRANGE and receives 0 messages, it can assume that the list has been shortened and its index is 0 .

Comments