当前位置:网站首页>Example analysis of five data types in redis

Example analysis of five data types in redis

2022-06-09 18:23:00 Yisu cloud

Redis Analysis of five data types of

This article mainly explains “Redis Analysis of five data types of ”, Interested friends might as well come and have a look . The method introduced in this paper is simple and fast , Practical . Now let Xiaobian take you to learn “Redis Analysis of five data types of ” Well !

Redis Analysis of five data types of

1.Redis Of 5 Type of data

redis It's a kind of advanced key-value Storage system , among value Five data types are supported :

Redis Supported key data types

string String type

hash Table type

list List the type

set Collection types

zset Ordered set type

  1. About key The definition of , Note the following :

  1. Don't suggest key The name is too long , Usually no more than 1024, If it is too long, the query speed will be affected .

  2. It's not recommended to be too short , Too short reduces readability .

  3. Usually in the company , There's a unified naming convention .

2. String type string

2.1 summary

The string type is Redis The most basic data storage type in , It's in Redis Save in binary , There is no encoding and decoding process . No matter what string is stored 、 Integers 、 Floating point types are written as strings . stay Redis Of string type Value The maximum length of data that can be held is 512M. This is the most commonly used data type in the future .

Redis Analysis of five data types of

2.2 Common commands

command

Behavior

set key value

towards redis Add... To the database 1 Keys and values of string type , return OK Indicates successful addition . The same name will replace

get key

Extract a value of the specified key from the database , If there is a return value , If there is no return nil

del key

Delete the specified key and value , If the deletion is successful , Return the number of deletions . Otherwise return to 0

setnx key value

At the designated key When there is no , by key Set the specified value .
Set up the success , return 1 . Setup failed , return 0 .

2.3 Command demonstration

demand :

  1. Add a key to company, The value is itcast

  2. Set another key to company, The value is heima

  3. obtain company The elements of

  4. Delete company Elements

  5. Delete again company See if the return value is the same

  6. obtain company Look at the return value

  7. Set the key to job, The value is programmer

  8. Set again job The value of is code-farmer, Inquire about job Value

2.4 Execution effect

Redis Analysis of five data types of

3. Hash type hash

3.1 summary

Redis Medium Hash Types can be viewed as having String And String Value Map Containers , every last Hash Can be stored 40 Billion key value pairs .

Redis Analysis of five data types of

So this type is very suitable for storing information about objects . If a user has a name , password , Information such as age , You can have username、password and age Its storage structure is as follows :

Redis Analysis of five data types of

3.2 Common commands

command

Behavior

hset key Field value

Adds a pair of... To the specified key hash Field name and value of type

hget key Field

Get the value of the specified field of the specified key

hmset key Field value Field value

mulitple, Set multiple field names and values to a key at once

hmget key Field Field

Get the values of multiple fields from the specified key at once

hdel key Field Field

Delete one or more fields in a key

hgetall key

Get all the field values of a key

3.3 Command demonstration

demand :

establish hash The key of type is user, And add a field as username, The value is newboy

towards user The field added in is password, The value is 12345

towards user The field added in is age, The value is 18

Get... Separately user Medium username、password and age The field values of the

Redis Analysis of five data types of

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

Redis Analysis of five data types of

Get multiple fields at the same time :age and sex

Redis Analysis of five data types of

obtain user All fields and values in

Redis Analysis of five data types of

Delete user Birthday and password fields in

Redis Analysis of five data types of

4. List the type list

4.1 summary

stay Redis in ,List The type is a list of strings in insertion order . It is the same as the common linked list in data structure , We can be on its left (left) And right (right) Add new elements . When inserting , If the key doesn't exist ,Redis A new linked list will be created for the key , If this key already exists , It's to list Additive elements . On the contrary , If all elements in the list are removed , Then the key will also be removed from the database .List The maximum number of elements that can be contained in is 40 One hundred million .

Redis Analysis of five data types of

4.2 Common commands

command

Behavior

lpush key Elements Elements

left push Add a list element to the specified key on the left side of the list , If the key doesn't exist ,Redis A new linked list will be created for the key , If this key already exists , It's to list Additive elements .

rpush key Elements Elements

right push Add a list element to the specified key on the right side of the list

lpop key

left pop Pop up an element from the left side of the specified key , The elements in the list are deleted .

rpop key

right pop Pop up an element from the right of the specified key , The elements in the list are deleted .

lrange key Start end

Extract the element list of the specified range from the list of the specified key , Count from the left 0 Start , Count from the right -1 Start . If you want to take the whole list , First the 0, The end is -1

llen key

Get the length of the specified list

4.3 Command demonstration

Execution effect

Redis Analysis of five data types of

demand :

towards mylist In the list of keys , Add... From the left a b c Three elements

Add... From the right one two three Three elements

Query all elements

Redis Analysis of five data types of

Add a repeating element from the right three

Redis Analysis of five data types of

Delete the rightmost element three

Redis Analysis of five data types of

Delete the leftmost element c

Get the number of elements in the list

Redis Analysis of five data types of

5. Collection types set

5.1 summary

stay Redis in , We can Set Type as a character set without sorting , and List Same type , We can also add data values of this type 、 Delete or determine whether an element exists, etc .

Set The maximum number of elements that can be included is 40 Billion , and List The difference in type is ,Set Duplicate elements are not allowed in the collection .

Redis Analysis of five data types of

5.2 Common commands

command

Behavior

sadd key Elements Elements

towards set Add... To the collection 1 Elements or elements

smembers key

Query all the elements in the specified collection

sismember key Elements

Determines whether the specified element is in a collection , If there is a return 1, Otherwise return to 0

srem key Elements Elements

remove Delete one or more specified elements

sunion key 1 key 2

Returns the union of a given set . Nonexistent collection key Be regarded as an empty set .

5.3 Command demonstration

demand :

towards myset Add... To the collection A B C 1 2 3 Six elements

Again to myset Add B Elements , See if it can be added successfully

Show all members , It is found that the order of the elements is different from that of the added elements , The elements are disordered

Delete the C This element , And then look at the results

Judge A Whether in myset Collection

Judge D Whether in myset Collection

Create key as set1 Set : The element is a b c

Create key as set2 Set : The element is a b d

obtain set1 and set2 Union , And display

Redis Analysis of five data types of

6. Ordered set zset

6.1 summary

Redis An ordered set, like a set, is disordered and cannot be repeated .

The difference is that each element is associated with a score .redis It's the scores that sort the members of a collection from small to large . Members of an ordered set are unique , But fractions (score) But it can be repeated , Each collection can store 40 More than 100 million members .

Redis Analysis of five data types of

6.2 Common commands

command

Behavior

zadd key fraction value fraction value

Add one or more members... To an ordered collection

zrange key Start index End index

Return the members in the specified interval in the ordered set through the index interval

zrem key value value

Remove one or more members of an ordered collection

zrank key value

Returns the index of a specified member in an ordered collection

zcard key

Get the number of members of the ordered set

zscore key value

Get the score of the designated member

6.3 Command demonstration

Add key country, The score is 10, The value is Japan

Add key country, The score is 5, The value is USA

Add key country, The score is 1, The value is China, The score is 120, The value is Korea

Inquire about country All the elements in

Inquire about Japan The index number of ( from 0 Start )

The deletion value is USA The elements of

Inquire about country How many elements are there in

6.4 effect

Redis Analysis of five data types of

Here we are , I'm sure you're right “Redis Analysis of five data types of ” Have a deeper understanding of , You might as well put it into practice ! This is the Yisu cloud website , For more relevant contents, you can enter the relevant channels for inquiry , Pay attention to our , Continue to learn !

原网站

版权声明
本文为[Yisu cloud]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206091819314364.html