Redis(Redis)

一、数据类型

  1、String类型

  1.1 基本的key-value类型,Redis利用C语言写的,但是Redis并没有用C的字符串表示,而是自己构建了一种简单的动态字符串。相比C的原生字符串,Redis的SDS不光可以保存文本数据还可以保存二进制数据,并且获取字符串长度的复杂度为O(1)

  1.2 常用命令:set、get、strlen、incr、decr、setex等

  1.3 应用场景:一般常用在需要计数的场景,比如用户访问次数、热点文章的转发等

  2、list类型

  2.1 list即是一种链表。链表是一种非常常见的数据结构,特点是易于数据元素的插入和删除可以灵活调整链表的长度。许多高级语言java也有linkedlist,redis的list实现是一个双向链表。

  2.2 常用命令rpush,lpop,lpush,rpop,lrange,llen等

  应用场景:发布与定于或者消息队列、慢查询。

  3、Hash

  3.1 类似于jdk8中的hashmap,内部实现也差不多(数组+链表),不过redis的hash做了更多的优化。hash是一个string类型的field和value的映射表,特别适用于存储对象,后续操作的时候,可以仅仅修改对象的某个字段。比如hash数据结构来存储用户信息,商品信息等。

  3.2 常用命令:hset、hmset、hexists、hget、hgetall、hvals等

  3.3 应用场景:系统中对象数据的存储。

  ()

hmset userInfoKey name "guide" description "dev" age "24"

  4、Set

  4.1 类似于java中的hashset,redis中的set类型是一种无需集合,集合中元素没有先后。set可以轻易实现交集、并集、差集等。比如共同粉丝共同关注等。

  4.2 sadd、spop,smembers,sismember,scard,sinterstore,sunion等

  4.3 需要存放的数据不能重复以及需要获得数据的交集和并集等

  5、zset

  5.1 和set相比,sorted增加了一个权重参数score,使得集合中的元素能够按score进行有序排列,还可以通过score的范围获取元素。有点像java hashmap和treeset的结合体

  5.2 常用命令:zadd、zcard,zscore、zrange,zrevrange,zrem等

  5.3 需要对数据某个权重进行排序的场景。比如直播系统中,实时排行信息,包含直播间礼物排行,在线用户列表,各种礼物排行等。

  6、bitmap

  6.1 bitmap存储的是连续的二进制数字(0和1),通过bitmap,只需要一个bit位来表示某个元素对应的值,key就是对应元素本身。bitmap很节省空间

  6.2 常用命令:setbit、getbit、bitcount、bitop

  6.3 应用场景:适合需要保存状态信息(签到、登录)并需要进一步对这些信息进行分析的场景,用户签到情况,活跃用户情况,用户行为统计,比如用户是否点赞过某个视频

       7、

————————

1、 Data type

1. String type

1.1 the basic key value type is written by redis in C language, but redis does not represent it in C string, but constructs a simple dynamic string by itself. Compared with C’s native string, redis’s SDS can save not only text data but also binary data, and the complexity of obtaining the string length is O (1)

1.2 common commands: set, get, strlen, incr, decr, setex, etc

1.3 application scenarios: generally used in scenarios that need to be counted, such as user visits, forwarding of hot articles, etc

2. List type

2.1 list is a linked list. Linked list is a very common data structure, which is easy to insert and delete data elements, and the length of linked list can be flexibly adjusted. Many high-level languages Java also have LinkedList, and redis’s list implementation is a two-way linked list.

  2.2 常用命令rpush,lpop,lpush,rpop,lrange,llen等

Application scenario: Publishing and scheduling, or message queuing, slow query.

3、Hash

3.1 similar to HashMap in jdk8, the internal implementation is similar (array + linked list), but the hash of redis is more optimized. Hash is a mapping table of field and value of string type. It is especially suitable for storing objects. During subsequent operations, you can only modify a field of the object. For example, hash data structure is used to store user information, commodity information, etc.

3.2 common commands: hset, hmset, hexists, hget, hgetall, hvals, etc

3.3 application scenario: storage of object data in the system.

()

hmset userInfoKey name "guide" description "dev" age "24"

4、Set

4.1 similar to HashSet in Java, the set type in redis does not need a set, and the elements in the set have no order. Set can easily realize intersection, union, difference and so on. For example, common fans and common concerns.

  4.2 sadd、spop,smembers,sismember,scard,sinterstore,sunion等

4.3 the data to be stored cannot be repeated and the intersection and union of data to be obtained

5、zset

5.1 compared with set, sorted adds a weight parameter score, so that the elements in the set can be arranged orderly according to the score, and the elements can also be obtained through the range of score. It’s a bit like a combination of Java HashMap and TreeSet

5.2 common commands: zadd, zcard, zscore, zrange, zrevrange, zrem, etc

5.3 scenarios where a weight of data needs to be sorted. For example, in the live broadcast system, the real-time ranking information includes the gift ranking in the live broadcast room, the list of online users, various gift ranking, etc.

6、bitmap

6.1 bitmap stores continuous binary numbers (0 and 1). Through bitmap, only one bit is required to represent the corresponding value of an element, and key is the corresponding element itself. Bitmap saves space

6.2 common commands: setbit, getbit, bitcount, bitop

6.3 application scenario: it is suitable for scenarios that need to save status information (check-in and login) and further analyze these information, including user check-in, active users and user behavior statistics, such as whether users have liked a video

7、