Redis 3.2.2 command entry

Redis 3.2 string type entry

3.2.2 Command

1. Get a list of key names conform to the rules of

SET key value
GET key

  GET SET and Redis are two of the most simple commands, functions, and programming languages ​​they achieve in reading and writing similar to a variable.

2. incremental number

INCR key

  When the string is stored in integer form, Redis provides a practical command INCR, its role is to make the current key value is incremented, and returns the value after incrementing.

  Some readers may recall that two aid GET and SET commands to achieve their own incr function, pseudo-code is as follows:

def incr($key)
    $value = GET $key
    if not $value
        $value = 0
    $value = $value + 1
    SET $key, $value
    return $value

  If Redis only while connected to a client, then the above code without any problems (in fact not a member of error handling, but the focus of this is not discussed here). When at the same time can have a plurality of clients connected to the pit is Redis race condition (race condition) occurs. For example, there are two clients, A and B have to perform incr function of our own and are ready to implement the increment key with a key, and when they happen to both the code execution to the second row is read at the same time is the key the same, such as "5", then they are of their own to speak of the value is incremented to "6" and use the SET command to speak their assigned a primary key, although the results of the implementation of the quantifiers of key increment operation (atomic operation), no matter how many clients simultaneous connections, will not appear the situation. Then we will introduce the use of atomic transactions and scripts implement custom actions.

Guess you like

Origin www.cnblogs.com/gaara0305/p/10955139.html
Recommended