Home > Database > Redis > What are the common commands for keys and strings in Redis?

What are the common commands for keys and strings in Redis?

WBOY
Release: 2023-06-02 19:20:35
forward
915 people have browsed it

    Redis related knowledge

    The default port number of Redis is 6379

    The default 16 databases, similar array subscripts start from 0, The initial default is to use library No. 0.
    Use the command select <dbid></dbid> to switch databases. Such as: select 8.
    Unified password management, all libraries have the same password.

    dbsizeView the number of keys in the current database.
    flushdbClear the current library.
    flushall Kill all libraries.

    Redis is a single-threaded multi-channel IO multiplexing technology.
    Multiplexing refers to using one thread to check the readiness status of multiple file descriptors (Socket), such as calling the select and poll functions and passing in multiple file descriptors. If one file descriptor is ready, then Return, otherwise block until timeout. After getting the ready state, the actual operation can be performed in the same thread, or thread execution can be started (such as using a thread pool).

    Serial VS Multi-thread lock (memcached) VS Single-threaded multi-channel IO multiplexing(Redis)
    Redis and Memcache Three differences:

    • Support multiple data types

    • Support persistence

    • Single thread multiple Road IO multiplexing

    Data types in Redis

    What are the common commands for keys and strings in Redis?

    redis key

    keys *: View all keys in the current library (matching: keys *1)

    What are the common commands for keys and strings in Redis?

    ##exists key: Determine whether a key exists.
    type key: Check what type your key is.
    del key: Delete the specified key data,

    unlink key: Select non-blocking deletion based on value. Only keys are deleted from keyspace metadata, and the actual deletion will be performed asynchronously later. .
    expire key 10: 10 seconds, set the expiration time for the given key,
    ttl key: Check how many seconds there are left to expire, -1 means never Expired, -2 means expired.

    selectCommand to switch databases,
    dbsizeView the number of keys in the current database.
    flushdbClear the current library.
    flushallkill all libraries

    Redis string (String)

    String is the most basic type of Redis, one key corresponds to one value.

    String type is binary safe. It means that Redis string can contain any data. For example, jpg pictures
    or serialized objects.
    String type is the most basic data type of Redis. A string value in Redis can be up to 512M.

    Commonly used commands

    set, get, etc.

    set : Add key-value pairs. When a key with a set value is set to a new value, the new value will overwrite the old one.

    What are the common commands for keys and strings in Redis?

    *NX: When the key does not exist in the database, the key-value can be added to the database.

    *XX: When the key exists in the database, key-value can be added to the database, which is mutually exclusive with NX parameters.
    *EX: Key timeout seconds.
    *PX: key’s timeout in milliseconds, mutually exclusive with EX.

    get Query the corresponding key value.
    append Append the given Append to the end of the original value,
    strlen Get the length of the value.
    setnx Set the value of the key only when the key does not exist.

    Increase or decrease the value created

    incr

    Increase the numerical value stored in key by 1.

    Can only operate on numeric values. If it is empty, the new value is 10

    decr

    Decrease the numeric value stored in key by 1 .

    Can only operate on numeric values. If it is empty, the new value is -1.

    inrjy/ decrby <step> Increase or decrease the numerical value stored in key. Custom step size. </step>

    Note:

    incr and decr are atomic operations
    but i in java is not an atomic operation

    mset, mget and msetnx

    mset ....

    Set one or more key-value pairs at the same time.

    What are the common commands for keys and strings in Redis?

    ##mget

    Get one or more values ​​at the same time.

    msetnx

    It is atomic
    Set one or more key-value pairs at the same time ,succeeds if and only if all given keys do not exist.

    What are the common commands for keys and strings in Redis?

    If one of them exists before, it will not succeed

    What are the common commands for keys and strings in Redis?

    getrange, setrange

    getrange

    Get the range of values, similar to substring in java, front package, back package

    setrange

    Overwrites the stored string value with, starting from

    What are the common commands for keys and strings in Redis?

    ##Set expiration time

    setex

    While setting the key value, set the expiration time in seconds.

    What are the common commands for keys and strings in Redis?##Replace the old value

    getset

    Replace the old value with the new one, The new value is set and the old value is obtained.

    What are the common commands for keys and strings in Redis?String data structure

    SDS is the abbreviation of String data structure, which represents the data structure of a simple dynamic string. It is a string that can be modified. The internal structure is similar to Java's ArrayList. It uses pre-allocated redundant space to reduce frequent allocation of memory.

    What are the common commands for keys and strings in Redis?#The internal space capacity is usually larger than the actual string length len, as shown in the figure. If the string length is less than 1M, the existing space will be doubled when expanding. But if the length exceeds 1M, only 1M of space will be added each time it is expanded. It should be noted that the maximum length of the string is 512M.

    The above is the detailed content of What are the common commands for keys and strings in Redis?. 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