Home > Database > Redis > body text

Summary of Redis's five data types

WBOY
Release: 2022-06-07 18:55:08
forward
4470 people have browsed it

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!

Related labels:
source:csdn.net
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!