search
HomeDatabaseRedisWhat are the common commands for keys and strings in Redis?

    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!

    Statement
    This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
    Redis: Caching, Session Management, and MoreRedis: Caching, Session Management, and MoreMay 01, 2025 am 12:03 AM

    Redis's functions mainly include cache, session management and other functions: 1) The cache function stores data through memory to improve reading speed, and is suitable for high-frequency access scenarios such as e-commerce websites; 2) The session management function shares session data in a distributed system and automatically cleans it through an expiration time mechanism; 3) Other functions such as publish-subscribe mode, distributed locks and counters, suitable for real-time message push and multi-threaded systems and other scenarios.

    Redis: Exploring Its Core Functionality and BenefitsRedis: Exploring Its Core Functionality and BenefitsApr 30, 2025 am 12:22 AM

    Redis's core functions include memory storage and persistence mechanisms. 1) Memory storage provides extremely fast read and write speeds, suitable for high-performance applications. 2) Persistence ensures that data is not lost through RDB and AOF, and the choice is based on application needs.

    Redis's Server-Side Operations: What It OffersRedis's Server-Side Operations: What It OffersApr 29, 2025 am 12:21 AM

    Redis'sServer-SideOperationsofferFunctionsandTriggersforexecutingcomplexoperationsontheserver.1)FunctionsallowcustomoperationsinLua,JavaScript,orRedis'sscriptinglanguage,enhancingscalabilityandmaintenance.2)Triggersenableautomaticfunctionexecutionone

    Redis: Database or Server? Demystifying the RoleRedis: Database or Server? Demystifying the RoleApr 28, 2025 am 12:06 AM

    Redisisbothadatabaseandaserver.1)Asadatabase,itusesin-memorystorageforfastaccess,idealforreal-timeapplicationsandcaching.2)Asaserver,itsupportspub/submessagingandLuascriptingforreal-timecommunicationandserver-sideoperations.

    Redis: The Advantages of a NoSQL ApproachRedis: The Advantages of a NoSQL ApproachApr 27, 2025 am 12:09 AM

    Redis is a NoSQL database that provides high performance and flexibility. 1) Store data through key-value pairs, suitable for processing large-scale data and high concurrency. 2) Memory storage and single-threaded models ensure fast read and write and atomicity. 3) Use RDB and AOF mechanisms to persist data, supporting high availability and scale-out.

    Redis: Understanding Its Architecture and PurposeRedis: Understanding Its Architecture and PurposeApr 26, 2025 am 12:11 AM

    Redis is a memory data structure storage system, mainly used as a database, cache and message broker. Its core features include single-threaded model, I/O multiplexing, persistence mechanism, replication and clustering functions. Redis is commonly used in practical applications for caching, session storage, and message queues. It can significantly improve its performance by selecting the right data structure, using pipelines and transactions, and monitoring and tuning.

    Redis vs. SQL Databases: Key DifferencesRedis vs. SQL Databases: Key DifferencesApr 25, 2025 am 12:02 AM

    The main difference between Redis and SQL databases is that Redis is an in-memory database, suitable for high performance and flexibility requirements; SQL database is a relational database, suitable for complex queries and data consistency requirements. Specifically, 1) Redis provides high-speed data access and caching services, supports multiple data types, suitable for caching and real-time data processing; 2) SQL database manages data through a table structure, supports complex queries and transaction processing, and is suitable for scenarios such as e-commerce and financial systems that require data consistency.

    Redis: How It Acts as a Data Store and ServiceRedis: How It Acts as a Data Store and ServiceApr 24, 2025 am 12:08 AM

    Redisactsasbothadatastoreandaservice.1)Asadatastore,itusesin-memorystorageforfastoperations,supportingvariousdatastructureslikekey-valuepairsandsortedsets.2)Asaservice,itprovidesfunctionalitieslikepub/submessagingandLuascriptingforcomplexoperationsan

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    ZendStudio 13.5.1 Mac

    ZendStudio 13.5.1 Mac

    Powerful PHP integrated development environment

    DVWA

    DVWA

    Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

    mPDF

    mPDF

    mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

    SublimeText3 English version

    SublimeText3 English version

    Recommended: Win version, supports code prompts!

    MantisBT

    MantisBT

    Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.