What data types does Redis support?

foreword


The reason why Redis is widely used is not only because it is fast, but also because it supports several data types in addition to the simplest string (String) type:

  • Hash

  • list

  • Collection (Set)

  • Sorted Set

  • Bitmap ( Bitmap )

String


String is the simplest type, a key corresponds to a value, and it can also store binary objects (Bitmap).

127.0.0.1:6379> set mykey "aminglinux.com"
OK
127.0.0.1:6379> get mykey
"aminglinux.com"
127.0.0.1:6379> mset key1 1 key2 a key3 c
127.0.0.1:6379> mget key1 key2 key3
1) "1"
2) "a"
3) "c"

Hash


Hash is a collection of key-value pairs. In Redis, Hash is a mapping between string fields and string values. Therefore, they are suitable for representing objects. Such as the user's nickname, age, gender, points, etc.

Example:

127.0.0.1:6379> hset hash1 name aming
127.0.0.1:6379> hget hash1 name
"aming"
127.0.0.1:6379> hset hash1  age 30
127.0.0.1:6379> hget hash1 age
"30"
127.0.0.1:6379> hgetall hash1
1) "name"
2) "aming"
3) "age"
4) "30

List


List is a linked list structure, the main function is push, pop to get all the values ​​in a range, and so on. The key in the operation is understood as the name of the linked list.

Using the List structure, we can easily implement functions such as ranking the latest news (such as TimeLine of Sina Weibo). Another application of List is the message queue. You can use the push operation of List to store tasks in the List, and then use the pop operation on the worker thread to take out the task for execution.

Example:

127.0.0.1:6379> LPUSH list1 "aminglinux"
127.0.0.1:6379> LPUSH list1  "1 2 3"
127.0.0.1:6379> LPUSH list1 "aaa bbb"
127.0.0.1:6379> LRANGE list1 0 -1
1) "aaa bbb"
2) "1 2 3"
3) "aminglinux“
127.0.0.1:6379> LPOP list1

Set


Set is a set, which is similar to the set concept in our mathematics. The operations on sets include adding and deleting elements, and operations such as intersecting and subtracting multiple sets. The key in the operation is understood as the name of the collection. For example, in a Weibo application, all followers of a user can be stored in a collection, and all fans can be stored in a collection. Because Redis is very user-friendly and provides operations such as intersection, union, and difference for collections, it is very convenient to implement functions such as common attention, common preferences, and second-degree friends. For all the above collection operations, you can also You can choose to return the results to the client or store them in a new collection using different commands.

example

127.0.0.1:6379> SADD set1 a
127.0.0.1:6379> SADD set1 b
127.0.0.1:6379> SADD set1 c
127.0.0.1:6379> SADD set1 d
127.0.0.1:6379> SMEMBERS set1
1) "d"
2) "b"
3) "a"
4) "c"
127.0.0.1:6379> SREM set1 c//删除元素
127.0.0.1:6379> SADD set2 a 2  b
127.0.0.1:6379> SINTER set1 set2 //交集
127.0.0.1:6379> SUNION set1 set2 //并集
127.0.0.1:6379> SDIFF set1 set2 //差集

Sorted set


Sorted Set is an ordered set. It has a weight parameter score more than set, so that the elements in the set can be arranged in an orderly manner according to the score. For example, a Sorted Set that stores the grades of the whole class, its set value can be the student number of the classmate , and the score can be its test score, so that when the data is inserted into the collection, it has already been sorted naturally.

127.0.0.1:6379> ZADD set3 12 abc
127.0.0.1:6379> ZADD set3 2 "cde 123"
127.0.0.1:6379> ZADD set3 24 "123-aaa"
127.0.0.1:6379> ZADD set3 4 "a123a"
127.0.0.1:6379> ZRANGE set3 0 -1
1) "cde 123"
2) "a123a"
3) "abc"
4) "123-aaa"

reverse order

127.0.0.1:6379> ZREVRANGE set3 0 -1
1) "123-aaa"
2) "abc"
3) "a123a"
4) "cde 123"

Bitmap


Bitmap, that is, bitmap, is not an independent data type in the strict sense, but a special String data type. It is a string of continuous binary arrays (0 and 1), elements can be located by offset (offset).

BitMap uses the smallest unit bit to set 0|1 to represent the value or state of an element, and the time complexity is O(1).

Since a bit is the smallest unit in a computer, using it for storage will save space, and is especially suitable for scenarios with large amounts of data and binary statistics.

example

127.0.0.1:6379> setbit aaa:001 10001 1 # 返回操作之前的数值
(integer) 0
127.0.0.1:6379> setbit aaa:001 10002 2 # 如果值不是0或1就报错
(error) ERR bit is not an integer or out of range
127.0.0.1:6379> setbit aaa:001 10002 0
(integer) 0
127.0.0.1:6379> setbit aaa:001 10003 1
(integer) 0

Since this kind of data scene is not used much, so I won't explain it much.

Guess you like

Origin blog.csdn.net/am_Linux/article/details/129677720