Getting Redis study notes (b) redis data structure

This blog belongs to the original author without permission prohibited reproduced, please respect the original! If you have questions, please contact QQ509961766

Redis Unlike traditional databases, the concept of the table, only db Schema corresponding number distinction. In the same db, key as a top level model, its value is flattened. That is the key db namespace.

We all know that a total of five kinds of data structures in redis, the data structures that each usage scenario is what is it?

  • String String-
  • Hash- dictionary
  • List- List
  • Set- collection
  • Sorted Set- ordered set

Here we take a look at a simple summary, the five types of commonly used commands, as well as usage scenarios, what data structures used under what circumstances. Here are just a commonly used commands, followed by a detailed description of each chapter will command, command unfamiliar students can open the client to follow exercises, which helps to understand data types, are also familiar with the commonly used commands. Man of few words said, that is entered.

1.String string

String data structure is the most common type, can be said that a lot of people in addition to String, other basic data types not used, like any other language strings, shaped like a key value, such as hello world, hello is key, world is the value . value not only be a String, it may also be a number. In short, string can be saved everything, what can be saved, but depends on the specific usage scenarios.

command description
SET key value Setting the value of the specified key
GET key Gets the value of the specified key
SETNX key value Only settings key in the key does not exist
SETEX key seconds value Settings key, and the key is set to expire seconds The time (in seconds)
APPEND key value To add a string to the end

Open Client to practice
For example, memory key = hello, value = this is a redis test
Here Insert Picture Description
String of application scenarios

1.储存某个字符串,字段
2.储存某个对象
3.计数器,字符串是整数时,可以使用自增,自减,例如id自增,微信点赞自增
4.Redis的所有操作都是单线程保证了数据的原子性,在高并发场景下保证数据的一致性

2.Hash dictionary

We often some information HashMap packaged into structured, after the client is stored as a sequence of values ​​of a string (usually JSON format), such as the user's nickname, age, sex, and other integral. This time when the need to modify a particular, usually requires the string (JSON) taken out, and then deserialized, change a value of one, then serialized string (JSON) is stored back. Simply modify a property to dry so many things, consumption must be great, it does not apply to a number of possible concurrent operations occasions (such as two concurrent operations need to modify points). The Hash structure of Redis can make you look like Update in the database as a property only modify a particular property values. Redis hash field is a string type and the value of the mapping table, hash is particularly suited for storing objects.

command description
HSET key field value The value of the hash table key field field is set to value
HMSET key field1 value1 [field2 value2 ] While the plurality of field-value (Domain - value) provided to the key in the hash table
HMGET key field1 [field2] Gets the value of all the given field
HKEYS key Get all the fields in the hash table

Open Client to practice
For example, I want to keep Kobe Bryant for example the person's name, age, sex, date of birth, and other information Lakers
command is: HMSET kobe name "bryant" age 42 sex "man" birth "2020-08-23" team "Lakers "
Here Insert Picture Description
Hash application scenarios

hash 类型十分适合存储对象类数据。
相比于在 String 中介绍的把对象转化为 json 字符串存储,
Hash 的结构可以任意添加或删除‘字段名’,更加高效灵活。
而String中存json对象,如果要修改json中某个属性,开销是很大的。
而Hash类型可以很好的解决这个问题,大大减小开销

3.List list (ordered repeatable)

List string list sorted in accordance with the order of insertion, to insert a new element in the head and tail (Redis achieved using a double-ended list List). Use List structure, we can easily implement the latest news ranking functions (such as Sina microblogging TimeLine). Another application is the message queue List, a List can be utilized * PUSH operation, there will be a task List, then the worker thread then taken POP operation execution tasks.

command description
LPUSH key value1 [value2] The one or more values ​​are inserted into the head of the list
LPUSHX key value A value into the head of the list already exists
LRANGE key start stop Get a list of the elements within the specified range
LLEN key Get a list length

Open Client to practice
For example, all the attributes associated memory Bryant to the list
command LPUSH player kobe bryant 42 man 1978-08-23 Lakers PG
then LLEN player can check the length of the list
and then LRANGE player 0 6 may be obtained according to the following standard list, attention must be taken off the mark, or the command error.
Subscript does not go wrong, but will get to the empty set, for example, start is greater than end subscript

Here Insert Picture Description
List of scenarios

1.消息队列,list类型的lpop和rpush能实现队列的功能
2.最新消息,list类型的lpush命令和lrange命令能实现最新列表的功能,
每次通过lpush命令往列表里插入新的元素,然后通过lrange命令读取最新的元素列表,
如朋友圈的点赞列表、评论列表。
3.排行榜,list类型的lrange命令可以分页查看队列中的数据。可将每隔一段时间计算一次的排行榜存储在list类型中
4.由于list 是链表结构,所有如果在头部和尾部插入数据,性能会非常高,不受链表长度的影响;
但如果在链表中插入数据,性能就会越来越差。

4.Set set (disorder not repeat)

Set is a collection, the collection is a combination of the concept of a bunch of distinct values. Set data structure using Redis provided, you may store some of the data set. And among a set of set of elements are not ordered, the element index does not exist. For example, in the micro-blog application, a user can have all the attention the existence of a set of people, there will be a collection of all of its fans. Because the Redis provides a very user-friendly collection of intersection of and union, difference and other operations, then it can be very easy to achieve such common concerns, common preferences, second degree friends and other functions, the set of all operations on the above, you also different commands can use the selection result is stored back to the client to set a new set.

command description
SADD key member1 [member2] Add one or more members to the collection
SCARD key Get the length of the collection
SISMEMBER key member Determine whether the member is a set of key elements of the members
SMEMBERS key Back to all members of the collection
SREM key member1 [member2] Remove the set of one or more members

Open Client to practice
For example, save the NBA players' names to set collection
SADD NBA kobe harden curry james westbrook lillard durant paul irving love anthony embiid
then view the collection SMEMBERS NBA
and then view the collection length SCARD NBA
and then determine whether Jordan in the collection
and then delete elements kobe
Here Insert Picture Description

Set application scenarios

1.共同好友/关注/粉丝/感兴趣/黑名单的人集合,求交集,并集,差集
2.随机集合,由于set是无序集合,适合一些随机使用场景,例如微博推荐,标签等等

5.Sorted Set ordered set

Set and compared, Sorted Sets Set of elements is increased by a weight parameter Score, such elements in the set can be ordered by Score, such as a statistical aggregate NBA players storage technology, which can set the player's number value while the score may be the player's score, so that the data inserted into the collection, it already had a natural order. It also can do with Sorted Sets queues with weights, such as a common message. 1 score, score important message is 2, then the worker thread may be selected to obtain the reverse order of score tasks. Make important task precedence.

command description
ZADD key score1 member1 [score2 member2] ] Adding to the ordered set of one or more members, or update an existing member of the score
ZCARD key Get the length of the collection
ZSCORE key member Return an ordered set, members of the fractional value
ZRANGE key start stop [WITHSCORES] By indexing interval returned an ordered set of members within the specified range

Open Client to practice
E.g. deposit the season NBA players scoring
ZADD SCORE 33.4 HARDEN 29.1 JAMES 28.8 DURANT 27.7 CURRY 27.1 WESTBROOK 27.1 LILLARD
then check the length ZCARD SCORE
then ZSCORE SCORE JAMES See James fraction
then ZRANGE SCORE 0 5 6 See before scoring

Here Insert Picture Description
Sorted Set application scenarios

1.好友亲密度
2.NBA球员得分榜
Published 75 original articles · won praise 44 · views 510 000 +

Guess you like

Origin blog.csdn.net/u013254183/article/details/105226407