Home > Database > Redis > body text

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

王林
Release: 2023-05-31 11:16:07
forward
1638 people have browsed it

    Redis data type String operation command

    1. append append string

    append name 2222
    Copy after login

    What are the common operation commands for Rediss basic data type String?

    2. strlen gets the key string length

    strlen name
    Copy after login

    What are the common operation commands for Rediss basic data type String?

    3. Self-increment, self-decrement

    Article views, likes can be used This realization.

    incr agedecr age
    Copy after login

    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 Rediss basic data type String?

    If you want to bring the step size:

    incrby age 5decrby age 8
    Copy after login

    What are the common operation commands for Rediss basic data type String?

    ##4. String range

    getrange name 1 3
    Copy after login

    What are the common operation commands for Rediss basic data type String?

    getrange name 0 -1
    Copy after login

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

    What are the common operation commands for Rediss basic data type String?

    5. Replace the string

    Start replacing the string at the specified position

    setrange name 0 test
    Copy after login

    What are the common operation commands for Rediss basic data type String?

    6. Set the value and its expiration time

    setex
    setex mykey 60 redis
    Copy after login
    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 Rediss 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
    Copy after login

    What are the common operation commands for Rediss 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
    Copy after login

    mget, get multiple at one time.

    mget k1 k2 k3
    Copy after login

    What are the common operation commands for Rediss 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
    Copy after login

    What are the common operation commands for Rediss 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
    Copy after login

    What are the common operation commands for Rediss 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
    Copy after login

    What are the common operation commands for Rediss 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!

    Related labels:
    source:yisu.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!