Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the data types of Redis

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what data types does Redis have". In daily operation, I believe many people have doubts about what data types Redis has. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what data types does Redis have?" Next, please follow the editor to study!

Redis data type

Redis's command ignores case.

I. String type

Assignment

> set name Wang Wu

> select 1

> set name zhangsan

> get name

Set / get multiple key values

Mset age 25 gender male city Hangzhou

> get age

> mget age city

Take a value and then assign a value

Getset s2111

Getset s2222

Tail added value

> append name hello

Delete

Del S2

Numerical increase or decrease

Increment

When the stored string is an integer, use the command incr to add 1 to the current key value and return the incremented value

> incr num

Specify step size

> incrby num 2

Decreasing

> decr num

> decrby num 2

Application scenarios to store commodity information

> incr items:id

II. Hash type

The hset command does not distinguish between insert and update operations, insert operation returns 1, update returns 0

Set one field at a time

> hset user name lizi

Set multiple fields at a time

> hmset user age 25 gender male

Assign a value when the field does not exist, and no action is performed if it exists

> hsetnx user address hangzhou

Get one field at a time

> hget user name

Get more than one field at a time

> hmget user name age

Get all field values at once

> hgetall user

Delete one or more fields and return the number of deleted fields

> hdel user age address

Digital increase

> hset user age 20

> hincrby user age 2

> hget user age

Determine whether the field exists

> hexists user age

Get only the field name or field value

> hkeys user

> hvals user

Get the number of fields

> hlen user

Application scenarios to store commodity information

Hmset items:1001 id 1001 name iphonex price 1999 color black

Hmset items:1002 id 1002 name iphonex price 1999 color black

Get commodity information

> hget items:1001 name

> hgetall items:1001

III. List type

Add an element to the left of the list

> lpush list 0 9 8

Lpush list:1 1 2 3 4

Add an element to the right of the list

Rpush list:1 5 6 7

View list

> lrange list:1 0 3

> lrange list:1 0-1 read all

Pops the element from both ends of the list and returns the value of the removed element

> lpop list:1

> rpop list:1

Gets the number of elements in the list

> llen list:1

Delete the value specified in the list

The LREM command deletes the elements in the list whose previous count value is value, returning the actual number of elements deleted. Depending on the count value, the command is executed differently:

When count > 0, LREM is deleted from the left side of the list.

When count lrem list:1 1 5

> lrem list:1-1 5

> lrem list:1 0 5

Gets the element value of the specified index, starting at 0

> lindex list:1 0

Sets the element value of the specified index

> lset list:1 0 11

Keep the elements in the specified range of the list

> ltrim list:1 0 2

Inserts an element before and after the specified element value and returns the number of elements in the list

Linsert list:1 before 2 10

Linsert list:1 after 2 10

Move an element from one list to another

> rpoplpush list:1 list:2

Application scenarios, product reviews

> lpush items:comment:1001'{"comment": "good product", "date": 1430295077289}'

> lpush items:comment:1001'{"comment": "bad product", "date": 1430295078888}'

4. Set type. Elements are not duplicated and unordered.

Add

> sadd set1 a b c

Delete

> srem set1 b

Get

> smembers set1

To determine whether the element exists, it returns 1 if it exists, and 0 if it does not exist.

> sismember set1 a

Subtraction operation, a set of elements that belong to A but not to B, Amurb

> sadd setA 1 2 3

Sadd setB 2 3 4

> sdiff setA setB

> sdiff setB setA

Intersection, A ∩ B

> sinter setA setB

Union, A ∪ B

> sunion setA setB

Get the number of elements

> scard setA

An element pops up at random

> spop setA

5. SortedSet type, zset, ordered

Add

Zadd english:scoreboard 80 zhangsan 70 lisi 90 wangwu

> zadd english:scoreboard 60 maliu

Modify

> zadd english:scoreboard 85 zhangsan

Get the score of the element

> zscore english:scoreboard maliu

Delete element

> zrem english:scoreboard maliu

Gets a list of element values ranked in an index range

Arranged according to the score value from small to large

> zrange english:scoreboard 0 1

> zrange english:scoreboard 0-1

> zrange english:scoreboard 0-1 withscores

Arrange according to the value of score from large to small

> zrevrange english:scoreboard 0-1

> zrevrange english:scoreboard 0-1 withscores

Gets the elements within the specified score range

Zrangebyscore english:scoreboard 80 90 withscores

Zrangebyscore english:scoreboard 80 90 withscores limit 1 2

Increase the score of an element and return the changed score

> zincrby english:scoreboard 5 wangwu

Gets the number of elements in the collection

> zcard english:scoreboard

Gets the number of elements within a specified score

> zcount english:scoreboard 80 90

Delete elements according to the specified index range according to ranking

> zremrangebyrank english:scoreboard 0 1

Delete elements by score range

> zremrangebyscore english:scoreboard 80 90

Get the ranking of elements

From small to big

> zrank english:scoreboard lisi

From big to small

> zrevrank english:scoreboard lisi

Application scenarios, commodity sales rankings

Zrank items:sellsort 9 1001 10 1002

Zincrby items:sellsort 1 1001

> zrevrange items:sellsort 0 9 withscores

Set the time to live for key

> set test hello

> expire test 5

> ttl test [- 1 has not set survival time,-2 has been deleted]

> get test

> persist test

Matching query key

> keys user*

Whether key exists in the database

> exists user

Delete key

> del user

Rename key

> rename user userabc

Return data type

> type setA

At this point, the study of "what data types does Redis have" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report