redis is a database of key-value pairs. There are 5 main data types:
String type (string), hash type (hash), list type ( list), set type (set), ordered set type (zset)
Recommended: redis tutorial
Several basic commands:
Function | Description |
---|---|
Get all keys of the current database | |
Determine whether the key exists and return the number. If the key has the same key, it will also be a superposition number | |
Delete key, return the number of deleted items | |
Get the reduced value Data type (string, hash, list, set, zset) | |
Clear all databases | |
redis configuration |
Function | Syntax |
---|---|
The most basic commands: GET, SET | GET key, SET key value If there are spaces, double quotes are needed to distinguish the value. |
Integer increment: INCR | INCR key The default value is 0, So first execute the command to get 1, which is not an integer and prompts an error |
Increase the specified integer: INCRBY | INCRBY key increment |
Decrease the integer: DECR | DECR key The default value is 0, so first execute the command to get -1, which is not an integer and prompt an error |
Decrease the specified integer: DECRBY | DECRBY key increment |
Increase the specified floating point number: INCRBYFLOAT | INCRBYFLOAT key increment is similar to the INCR command, except that it can increment a double-precision floating point number |
Append value to the end: APPEND | APPEND key value The redis client does not output the appended string, but outputs the total length of the string |
Get the string length: STRLEN | STRLEN key If the key does not exist, return 0. Note that if there is Chinese, the length of a Chinese character is 3, and redis uses UTF-8 to encode Chinese |
Get multiple key values: MGET | MGET key [key ...] For example: MGET key1 key2 |
Settings Multiple key values: MSET | MSET key value [key value ...] For example: MSET key1 1 key2 "hello redis" |
Binary specified position value: GETBIT | GETBIT key offset For example: GETBIT key1 2, key1 is hello and returns 1. The returned value is only 0 or 1. When the key does not exist or exceeds the actual length, it is 0 |
Set the binary position value: SETBIT | SETBIT key offset value, return the old value of the position |
The binary number is 1: BITCOUNT | BITCOUNT key [start end], start and end are the start and end bytes |
Bit operation: BITOP | BITOP operation destkey key [ key ...], operation supports AND, OR, XOR, NOT |
Offset: BITPOS | BITPOS key bit [start] [end] |
Two: Hash type (hash)
Function | Syntax |
---|---|
Set a single: HSET | HSET key field value, returns 1 if it does not exist, returns 0 if it exists, there is no distinction between update and insertion |
Set multiple: HMSET | HMSET key field value [field value ...] |
Read a single: HGET | HGET key field, if it does not exist, return nil |
Read multiple: HMGET | HMGET key field [field ...] |
Read all: HGETALL | HGETALL key, return a list of fields and field values |
Judge whether the field exists: HEXISTS | HEXISTS key field, return 1 if it exists, if not Existence returns 0 |
Assignment when the field does not exist: HSETNX | HSETNX key field value, different from the hset command, hsetnx sets the value when the key does not exist |
Increase number: HINCRBY | HINCRBY key field increment, return the increased number, if it is not an integer, an error will be prompted |
Delete field: HDEL | HDEL key field [field ...], returns the number of deleted fields |
Get only the field name: HKEYS | HKEYS key , return all field names of the key |
Get only field values: HVALS | HVALS key , return all field values of the key |
Number of fields: HLEN | HLEN key, return the total number of fields |
Three: List type (list)
Internally implemented using a doubly linked list, so the elements closer to both ends are obtained faster, but access through indexes will be slower
Function | Syntax |
---|---|
Add the left element: LPUSH | LPUSH key value [value .. .] , returns the total number of added list elements |
Add the right element: RPUSH | RPUSH key value [value ...] , returns the added The total number of list elements |
Remove the first element on the left: LPOP | LPOP key, return the value of the removed element |
Remove the first element on the right: RPOP | RPOP key, return the value of the removed element |
Number of elements in the list: LLEN | LLEN key, returns 0 when it does not exist. Redis directly reads the ready-made value, not the statistical number |
Get the list fragment: LRANGE | LRANGE key start stop, if start is later than stop, an empty list is returned, 0 -1 returns the entire list when it is a positive number: start starts the index value, stop ends the index value (the index starts from 0) when it is a negative number: for example, lrange num -2 -1, -2 means the second from the right, -1 means the first from the right, |
Delete the specified value: LREM |
LREM key count value, returns the deleted number count>0, deletes the first count elements with value from the left count count=0, delete all elements whose value is value |
Index element value: LINDEX | LINDEX key index, returns the element value of the index, -1 means setting the element value from the rightmost first position |
: LSET | LSET key index value |
Keep list fragment: LTRIM | LTRIM key start stop, start, top refer to lrange command |
Transfer one list to another List: RPOLPPUSH | RPOPLPUSH source desctination, transfer from the source list to the desctination list. This command is divided into two steps. First, remove the source list RPOP right, and then remove the desctination list LPUSH |
Four: Set type (set)
Set type values are unique. Common operations are to add, delete, and determine whether a value exists in the set. The internal set is implemented using a hash table with empty values. of.
Function | Syntax |
---|---|
Add element: SADD | SADD key member [member ...], adds one or more elements to a set. Because of the uniqueness of the set, adding the same value will be ignored. Returns the number of successfully added elements. |
Delete elements: SREM | SREM key member [member ...] Delete one or more elements in the collection and return the number of successfully deleted elements. |
Get all elements: SMEMBERS | SMEMBERS key, return all elements of the collection |
Whether the value exists: SISMEMBER |
SISMEMBER key member, if it exists, it returns 1, if it does not exist, it returns 0 |
Difference operation: SDIFF | SDIFF key [key ...], For example: set A and set B, the difference set represents A-B, if there are elements in A that are not in B, the difference set is returned; multiple sets (A-B)-C |
intersection operation: SINTER | SINTER key [key ...], returns the intersection set, each set has elements |
And operation: SUNION | SUNION key [key...], returns the union set, the elements of all sets |
The number of set elements: SCARD | SCARD key, returns the number of set elements |
Storage results after set operation | SDIFFSTROE destination key [key ...], difference operation and store in destination new setSINTERSTROE destination key [key .. .], intersect and store it in the new collection of destination SUNIONSTROE destination key [key ...], and perform the operation and store it in the new collection of destination |
Randomly obtain elements: SRANDMEMGER | SRANDMEMBER key [count], there are different results depending on the count. When the count is greater than the total number of elements, all elements count>0 are returned. Count |
Pop element: SPOP | SPOP key [count], because the collection is unordered, So spop will randomly pop up an element |
Five: Ordered set type zset (sorted set: ordered set)
Redis zset, like set, is also a collection of string type elements, and duplicate members are not allowed.
The difference is that each element is associated with a double type score.
Redis uses scores to sort the members of the set from small to large. The members of zset are unique, but the scores can be repeated.
Function | Syntax |
---|---|
ZADD key [NX|XX] [CH] [INCR] score member [score member ...], there is no addition, there is update. | |
ZSCORE key member, return the score score of the element member | |
ZRANGE key start top [WITHSCORES], refer to LRANGE, plus withscores to return the band elements, that is, elements, scores. When the scores are the same, sort by elements | |
ZREVRANGE key start [WITHSCORES], the difference from zrange is that zrevrange is sorted from large to small | |
ZRANGEBYSCORE key min max [WITHSCORE] [LIMIT offest count] returns the elements between min and max from small to large, ( symbol means not included, for example: 80-100, (80 100, withscore returns the mixed fraction limit offest count, offset offset count to the left, and obtain the first count elements | |
ZREVRANGEBYSCORE key max min [WITHSCORE] [LIMIT offest count] is similar to zrangebyscore, except that the command is sorted from large to small. | |
ZINCRBY key increment member, note that the score is increased and the increased score is returned; if the member does not exist, a member of 0 is added. |
Related recommendations: mysql video tutorial:
The above is the detailed content of Introduction to basic commands of 5 data types in redis. For more information, please follow other related articles on the PHP Chinese website!

Redis's database methods include in-memory databases and key-value storage. 1) Redis stores data in memory, and reads and writes fast. 2) It uses key-value pairs to store data, supports complex data structures such as lists, collections, hash tables and ordered collections, suitable for caches and NoSQL databases.

Redis is a powerful database solution because it provides fast performance, rich data structures, high availability and scalability, persistence capabilities, and a wide range of ecosystem support. 1) Extremely fast performance: Redis's data is stored in memory and has extremely fast read and write speeds, suitable for high concurrency and low latency applications. 2) Rich data structure: supports multiple data types, such as lists, collections, etc., which are suitable for a variety of scenarios. 3) High availability and scalability: supports master-slave replication and cluster mode to achieve high availability and horizontal scalability. 4) Persistence and data security: Data persistence is achieved through RDB and AOF to ensure data integrity and reliability. 5) Wide ecosystem and community support: with a huge ecosystem and active community,

Key features of Redis include speed, flexibility and rich data structure support. 1) Speed: Redis is an in-memory database, and read and write operations are almost instantaneous, suitable for cache and session management. 2) Flexibility: Supports multiple data structures, such as strings, lists, collections, etc., which are suitable for complex data processing. 3) Data structure support: provides strings, lists, collections, hash tables, etc., which are suitable for different business needs.

The core function of Redis is a high-performance in-memory data storage and processing system. 1) High-speed data access: Redis stores data in memory and provides microsecond-level read and write speed. 2) Rich data structure: supports strings, lists, collections, etc., and adapts to a variety of application scenarios. 3) Persistence: Persist data to disk through RDB and AOF. 4) Publish subscription: Can be used in message queues or real-time communication systems.

Redis supports a variety of data structures, including: 1. String, suitable for storing single-value data; 2. List, suitable for queues and stacks; 3. Set, used for storing non-duplicate data; 4. Ordered Set, suitable for ranking lists and priority queues; 5. Hash table, suitable for storing object or structured data.

Redis counter is a mechanism that uses Redis key-value pair storage to implement counting operations, including the following steps: creating counter keys, increasing counts, decreasing counts, resetting counts, and obtaining counts. The advantages of Redis counters include fast speed, high concurrency, durability and simplicity and ease of use. It can be used in scenarios such as user access counting, real-time metric tracking, game scores and rankings, and order processing counting.

Use the Redis command line tool (redis-cli) to manage and operate Redis through the following steps: Connect to the server, specify the address and port. Send commands to the server using the command name and parameters. Use the HELP command to view help information for a specific command. Use the QUIT command to exit the command line tool.

Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

Notepad++7.3.1
Easy-to-use and free code editor

Atom editor mac version download
The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.