Home  >  Article  >  Database  >  What are the common operation commands for Redis’s basic data type String?

What are the common operation commands for Redis’s basic data type String?

王林
王林forward
2023-05-31 11:16:071600browse

    Redis data type String operation command

    1. append append string

    append name 2222

    What are the common operation commands for Redis’s basic data type String?

    2. strlen gets the key string length

    strlen name

    What are the common operation commands for Redis’s basic data type String?

    3. Self-increment, self-decrement

    Article views, likes can be used This realization.

    incr agedecr age

    Note that this must be a number to proceed, so a key is reset.

    The String type can store not only strings but also numbers.

    What are the common operation commands for Redis’s basic data type String?

    If you want to bring the step size:

    incrby age 5decrby age 8

    What are the common operation commands for Redis’s basic data type String?

    ##4. String range

    getrange name 1 3

    What are the common operation commands for Redis’s basic data type String?

    getrange name 0 -1

    View all, similar to the string interception operation in python.

    What are the common operation commands for Redis’s basic data type String?

    5. Replace the string

    Start replacing the string at the specified position

    setrange name 0 test

    What are the common operation commands for Redis’s basic data type String?

    6. Set the value and its expiration time

    setex
    setex mykey 60 redis
    Set the value and its expiration time for the specified key. If key already exists, the SETEX command will replace the old value.

    What are the common operation commands for Redis’s basic data type String?

    setnx
    The Setnx(SET if Not eXists) command sets the specified value for the key when the specified key does not exist. This is often used in distributed locks.

    setnx mykey redis333

    What are the common operation commands for Redis’s basic data type String?

    #key exists, and the setting fails.

    7. Batch operations

    1. mset, mget
    mset, set multiple at one time.

    mset k1 v1 k2 v2 k3 v3

    mget, get multiple at one time.

    mget k1 k2 k3

    What are the common operation commands for Redis’s basic data type String?

    2. msetnx
    Note that when setting multiple values ​​here, as long as one of them fails, none of them will succeed.

    msetnx k1 v1 k4 v4

    What are the common operation commands for Redis’s basic data type String?

    8. Set a json object

    In actual applications, you may often need to save an object, so you can use colon: in redis Make some clever designs.

    For example, if you want it now

    {name: pingguo, age:22}set it to a user1, you can do this:

    mset user:1:name pingguo user:1:age 22mget user:1:name user:1:agemset user:1:name pingguo user:1:age 22
     
    mget user:1:name user:1:age

    What are the common operation commands for Redis’s basic data type String?

    9. Getset first gets and then sets

    Just like the literal meaning, the value will be obtained first and then set.

    If the value does not exist, return
    nil. If it exists, get the original value and set the new value.

    getset db mongodb

    What are the common operation commands for Redis’s basic data type String?

    The above is the detailed content of What are the common operation commands for Redis’s basic data type String?. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete