Redis data types and application scenarios

1. Features of redis

  • All data is stored in memory, high-speed read and write
  • Provide a variety of data types: string, hash, set, sorted set, bitmap, hyperloglog
  • Provides two ways of persisting data, AOF and RDB, to ensure that data is not lost after Redis restarts
  • All operations of Redis are atomic, and it also supports atomic operations after merging several operations, and supports transactions

Usually we store data in relational databases, but in order to improve application performance, we should cache data that is frequently accessed and does not change frequently in memory. . Redis does not have the powerful query function like relational databases such as MySQL. It is necessary to consider how to reasonably correspond the data in the relational database to the cached key-value data structure.

2. Design Redis Key

segmented design

Use colons to separate the various meanings to be expressed in the key. The steps are as follows:

  1. Convert table name to key prefix
  2. Primary key name (or other fields commonly used for searches)
  3. primary key value
  4. Field to store.

eg. user table (user)

id name email
1 zj [email protected]
2 to the [email protected]

This simple table may often have this requirement: > query the user's email address according to the user id, you can choose to store the email address data in redis:

set user:id:1:email 156577812@qq.com;
set user:id:2:email 156577812@qq.com;

3. Application scenarios of String data type

1 Introduction

The string type is the most basic data type in Redis, the most commonly used data type, and is even used by many players as the only data type of redis. The string type is binary safe in redis, which means that the string value cares about the binary string, not the specific format. You can use it to store strings in json format or JPEG image format.

2. Data Model

The string type is the basic Key-Value structure, the Key is the unique identifier of a certain data in Redis, and the Value is the specific data.

Key Value
‘name’ 'say again'
‘type’ ‘string’

3. Application scenarios

(1) Store the value of a field in MySQL

Design key as table name: primary key name: primary key value: field name

eg.

set user:id:1:email 156577812@qq.com

(2) Storage object

The string type supports strings in any format, and the most widely used is to store strings formatted by json or other objects. (Hash data type is recommended in this scenario)

set user:id:1 [{"id":1,"name":"zj","email":"[email protected]"},{"id":1,"name":"zj","email":"[email protected]"}]

(3) Generate auto-increment id

When the value of the string type of redis is in the form of an integer, redis can treat it as an integer and perform an increment (incr) and a decrement (decr) operation. Since all operations of redis are atomic, so the  不必担心多客户端连接时可能出现的事务 problem.

4. Application scenarios of hash data types

1 Introduction

The hash type is very similar to the data table of a relational database. The Key of the hash is a unique value, and the Value part is a hashmap structure.

2. Data Model

Suppose there is a database table as follows:

id name type
1 say again hash

如果要用 redis 的 hash 结构存储,数据模型如下:

hash数据类型在存储上述类型的数据时具有比 string 类型更灵活、更快的优势,具体的说,使用 string 类型存储,必然需要转换和解析 json 格式的字符串,即便不需要转换,在内存开销方面,还是 hash 占优势。

3. 应用场景

hash 类型十分适合存储对象类数据,相对于在 string 中介绍的把对象转化为 json 字符串存储,hash 的结构可以任意添加或删除‘字段名’,更加高效灵活。

hset user:1 name zj email 156577812@qq.com

五、list 数据类型的应用场景

1. 简介

list 是按照插入顺序排序的字符串链表,可以在头部和尾部插入新的元素(双向链表实现,两端添加元素的时间复杂度为 O(1))。插入元素时,如果 key 不存在,redis 会为该 key 创建一个新的链表,如果链表中所有的元素都被移除,该 key 也会从 redis 中移除。

2. 数据模型

常见操作时用 lpush 命令在 list 头部插入元素, 用 rpop 命令在 list 尾取出数据。

3. 应用场景

(1) 消息队列

redis 的 list 数据类型对于大部分使用者来说,是实现队列服务的最经济,最简单的方式。

(2) “最新内容”

因为 list 结构的数据查询两端附近的数据性能非常好,所以适合一些需要获取最新数据的场景,比如新闻类应用的 “最近新闻”。

4.优化建议

(1) list 是链表结构,所有如果在头部和尾部插入数据,性能会非常高,不受链表长度的影响;但如果在链表中插入数据,性能就会越来越差。

六、set 数据类型的应用场景

1. 简介

set 数据类型是一个集合(没有排序,不重复),可以对 set 类型的数据进行添加、删除、判断是否存在等操作(时间复杂度是 O(1) )

set 集合不允许数据重复,如果添加的数据在 set 中已经存在,将只保留一份。

set 类型提供了多个 set 之间的聚合运算,如求交集、并集、补集,这些操作在 redis 内部完成,效率很高。

2. 数据模型

3. 应用场景

set 类型的特点是——不重复且无序的一组数据,并且具有丰富的计算功能,在一些特定的场景中可以高效的解决一般关系型数据库不方便做的工作。

1. “共同好友列表”

社交类应用中,获取两个人或多个人的共同好友,两个人或多个人共同关注的微博这样类似的功能,用 MySQL 的话操作很复杂,可以把每个人的好友 id 存到集合中,获取共同好友的操作就可以简单到一个取交集的命令就搞定。

// 这里为了方便阅读,把 id 替换成姓名
sadd user:wade james melo paul kobe
sadd user:james wade melo paul kobe
sadd user:paul wade james melo kobe
sadd user:melo wade james paul kobe

// 获取 wade 和 james 的共同好友
sinter user:wade user:james
/* 输出:
 *      1) "kobe"
 *      2) "paul"
 *      3) "melo"
 */
 
 // 获取香蕉四兄弟的共同好友
 sinter user:wade user:james user:paul user:melo
 /* 输出:
 *      1) "kobe"
 */
 
 /*
     类似的需求还有很多 , 必须把每个标签下的文章 id 存到集合中,可以很容易的求出几个不同标签下的共同文章;
 把每个人的爱好存到集合中,可以很容易的求出几个人的共同爱好。 
 */

七、sorted set 数据类型的应用场景

1.简介

在 set 的基础上给集合中每个元素关联了一个分数,往有序集合中插入数据时会自动根据这个分数排序。

2.应用场景

在集合类型的场景上加入排序就是有序集合的应用场景了。比如根据好友的“亲密度”排序显示好友列表。

// 用元素的分数(score)表示与好友的亲密度
zadd user:kobe 80 james 90 wade  85 melo  90 paul

// 根据“亲密度”给好友排序
zrevrange user:kobe 0 -1

/**
 * 输出:
 *      1) "wade"
 *      2) "paul"
 *      3) "melo"
 *      4) "james"
 */
 
// 增加好友的亲密度
zincrby user:kobe 15 james

// 再次根据“亲密度”给好友排序
zrevrange user:kobe 0 -1

/**
 * 输出:
 *      1) "james"
 *      2) "wade"
 *      3) "paul"
 *      2) "melo"
 */
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326160692&siteId=291194637