Table of Contents
1. The 5 data types of Redis
5.3 Command Demonstration
2.4 Execution effect
3. Hash
6.1 Overview
6.2 Common commands
4. List type list
4.3 Command demonstration
6. Ordered set zset
Add the key country, the score is 1, the value is China , the score is 120, and the value is Korea
Redis video tutorial
Home Database Redis Summary of Redis's five data types

Summary of Redis's five data types

Jun 07, 2022 pm 06:55 PM
redis

This article brings you relevant knowledge about Redis, which mainly introduces related issues about data types, including string types, hash types, list types, collection types and Orderly collection, let’s take a look at it, I hope it will be helpful to everyone.

Summary of Redis's five data types

Recommended learning: Redis video tutorial

1. The 5 data types of Redis

redis is a An advanced key-value storage system, in which value supports five data types:

Key values ​​supported by Redis Data type

stringString Type

hash table type

list list type

##set collection type

zset ordered set type

    Regarding the definition of key, please note the following points:
    It is not recommended that the key name is too long, usually no more than 1024, if it is too long it will Affects query speed.
  1. It is not recommended to be too short, as it will reduce readability.
  2. Generally in companies, there is a unified naming standard.
2. String type string

2.1 Overview

The string type is the most basic data storage type in Redis. It is stored in binary in Redis. There is no encoding and decoding process. Regardless of whether the type stored is a string, integer, or floating point type, it will be written as a string. In Redis, the maximum data length that the string type Value can hold is 512M. This is the most commonly used data type in the future.

2.2 Common commands

Add 1 to the redis database Key and value of string type, return OK to indicate successful addition. The one with the same name will replace from the database Take out the value of a specified key, if there is a return value, if not return nilDelete the specified key and value. If the deletion is successful, return the number deleted. Otherwise return 0 If the setting is successful, 1 will be returned. Setup fails and returns 0.

2.3 Command Demonstration

Requirements:

  1. Add a key as company, the value is itcast
  2. Then set a key as company, the value is heima
  3. Get the company element
  4. Delete the company element
  5. Delete the company again to see if the return value is the same
  6. Get the company and see what the return value is
  7. Set the key to job and the value to programmer
  8. Set the value of job to code-farmer again and query the value of job

2.4 Execution effect

3. Hash

3.1 Overview

# Redis can be regarded as the MAP container with the key and String of String, each hash can store 40 Billions of key-value pairs.

So this type is very suitable for storing object information. If a user has name, password, age and other information, it can have keys such as username, password and age. Its storage structure is as follows:

3.2 Common commands

##Command

Behavior

set key value

get key

del key

##setnx key

value

When the specified key does not exist, set the specified value for the key.


##mulitple , set multiple field names and values ​​to a key at one time hdel key##hgetall key

Order

Behavior

##hset key field value

Add a pair of hash type field names and values ​​to the specified key

hget key Field

Get the value of the specified field of the specified key

hmset key Field valueField value

##hmget

## Key Field fieldGet the values ​​of multiple fields from the specified key at one time

field field Delete one or more fields in a key

Get all field values ​​​​of a certain key

#

3.3 Command Demonstration

Requirements:

Create a hash type key as user, and add a field as username, with a value of newboy

Add a field to user as password, the value is 12345

Add the field age to user, the value is 18

Get the field values ​​​​of username, password and age in user respectively

Add multiple fields and values ​​to user at the same time, birthday 2018-01-01 sex male

Get multiple fields at the same time: age and sex

Get all the fields and values ​​​​in user

Delete the birthday and password fields in user

4. List type list

4.1 Overview

In Redis, the List type is a linked list of strings sorted in insertion order. Like an ordinary linked list in the data structure, we can add new elements to its left and right parts. During insertion, if the key does not exist, Redis will create a new linked list for the key. If the key already exists, it will add elements to the list. In contrast, if all elements in the linked list are removed, the key will also be deleted from the database. The maximum number of elements that can be contained in a List is 4 billion.

4.2 Common commands

right push adds a list element to the specified key on the right side of the list#left pop pops an element from the left of the specified key, and the element in the list is deleted. right pop from the specified key When an element pops up on the right, the element in the list is deleted. ##llen

##Command

Behavior

lpush key element element

left push to the left of the list Adds a list element to the specified key. If the key does not exist, Redis will create a new linked list for the key. If the key already exists, it will add elements to the list.

##rpush key element element

lpop key

rpop key

##lrange key

Start end

Retrieve a list of elements in the specified range from the list of the specified key, counting from the left starting from 0, and counting from the right starting from -1. If you want to take the entire list, the start is 0 and the end is -1

KeyGet the length of the specified list

4.3 Command demonstration

Execution effect

Requirements:

To the list of mylist keys, add a b c from the left Element

Add one two three three elements from the right

Query all elements

Add a repeated element three# from the right

##Delete the rightmost element three

Delete the leftmost element c

Get the elements in the list Number of

5. Set type set

5.1 Overview

In Redis, we can regard the Set type as unsorted Character collection, like the List type, we can also perform operations such as adding, deleting, or determining whether an element exists on the data values ​​of this type.

The maximum number of elements that a Set can contain is 4 billion. Unlike the List type, duplicate elements are not allowed in the Set collection.

5.2 Common commands

##smemberssismembersrems

##Command

Behavior

s##add key element element

Add 1 or more elements to the set collection

KeyQuery all elements in the specified collection

Key ElementDetermine whether the specified element is in a certain collection. If it exists, return 1, otherwise return 0

Key Element Elementremove Delete the specified element or elements

union key 1 key 2 Returns the union of the given sets. A set key that does not exist is considered an empty set.

5.3 Command Demonstration

Requirements:

Add six elements A B C 1 2 3 to the myset collection

Add the B element to myset to see if it can be added Success

Displays all members and finds that the order of the elements is different from that of the added elements. The elements are unordered

Delete the C element and check the result again

Determine whether A In the myset set

Determine whether D is in the myset set

Create a set with the key set1: the element is a b c

Create a set with the key set2: the element is a b d

Get the union of set1 and set2, and display

6. Ordered set zset

6.1 Overview

Redis ordered collections, like sets, are unordered and cannot be repeated.

The difference is that each element is associated with a score. Redis uses scores to sort the members of the collection from small to large. The members of an ordered set are unique, but the scores can be repeated, and each set can store more than 4 billion members.

6.2 Common commands

Return the members in the specified range in the ordered set through the index range##zcard key##zscore keyAdd the key country, the score is 10, the value is JapanAdd the key country, the score is 5, the value is USA

##Command

Behavior

zadd key Score value Score value

To an ordered set Add one or more members

##zrange key Start index End index

##zrem Key

Value

Remove one or more members from the ordered set

zrank key

value

Returns the index of the specified member in the ordered set

Get the number of members of the ordered set

ValueGet the score of the specified member

## 6.3 Command Demonstration

Add the key country, the score is 1, the value is China , the score is 120, and the value is Korea

Query all elements in country

Query the index number of Japan (starting from 0)

Delete elements with value USA

Query how many elements there are in country

6.4 Effect

Recommended learning:

Redis video tutorial

The above is the detailed content of Summary of Redis's five data types. For more information, please follow other related articles on the PHP Chinese website!

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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to build the redis cluster mode How to build the redis cluster mode Apr 10, 2025 pm 10:15 PM

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

How to clear redis data How to clear redis data Apr 10, 2025 pm 10:06 PM

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

How to read redis queue How to read redis queue Apr 10, 2025 pm 10:12 PM

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

How to use the redis command How to use the redis command Apr 10, 2025 pm 08:45 PM

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

How to use redis lock How to use redis lock Apr 10, 2025 pm 08:39 PM

Using Redis to lock operations requires obtaining the lock through the SETNX command, and then using the EXPIRE command to set the expiration time. The specific steps are: (1) Use the SETNX command to try to set a key-value pair; (2) Use the EXPIRE command to set the expiration time for the lock; (3) Use the DEL command to delete the lock when the lock is no longer needed.

How to read the source code of redis How to read the source code of redis Apr 10, 2025 pm 08:27 PM

The best way to understand Redis source code is to go step by step: get familiar with the basics of Redis. Select a specific module or function as the starting point. Start with the entry point of the module or function and view the code line by line. View the code through the function call chain. Be familiar with the underlying data structures used by Redis. Identify the algorithm used by Redis.

How to make message middleware for redis How to make message middleware for redis Apr 10, 2025 pm 07:51 PM

Redis, as a message middleware, supports production-consumption models, can persist messages and ensure reliable delivery. Using Redis as the message middleware enables low latency, reliable and scalable messaging.

How to start the server with redis How to start the server with redis Apr 10, 2025 pm 08:12 PM

The steps to start a Redis server include: Install Redis according to the operating system. Start the Redis service via redis-server (Linux/macOS) or redis-server.exe (Windows). Use the redis-cli ping (Linux/macOS) or redis-cli.exe ping (Windows) command to check the service status. Use a Redis client, such as redis-cli, Python, or Node.js, to access the server.

See all articles