Redis 实战
大约 3 分钟
Redis 实战
1 maven 坐标
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
https://spring.io/projects/spring-data-redis
从 springboot 2
开始,redis 默认连接池从 jedis
换成了 lettuce
spring-boot-starter-data-redis
= spring-data-redis
+ lettuce-core
2 properties 配置
spring.redis.cluster.nodes=localhost.ubuntu0.com:6379,localhost.ubuntu0.com:6380,localhost.ubuntu1.com:6379,localhost.ubuntu1.com:6380,localhost.ubuntu2.com:6379,localhost.ubuntu2.com:6380
spring.redis.password=[hidden]
3 数据结构
以下是几道常见的 Redis 面试题:
- Redis 支持的数据类型有哪些?(一般回答 5 种。string、hash、list、set、zset)
- Redis 为什么这么快?或者 Redis 为什么是单线程的
当前 Redis 最新版本 6.2.6,以上答案已经不再正确,数据类型不止 5 种。
我们可以从官方文档找到答案
https://redis.io/topics/data-types-intro
- Binary-safe strings.
- Lists: collections of string elements sorted according to the order of insertion. They are basically linked lists.
- Sets: collections of unique, unsorted string elements.
- Sorted sets, similar to Sets but where every string element is associated to a floating number value, called score. The elements are always taken sorted by their score, so unlike Sets it is possible to retrieve a range of elements (for example you may ask: give me the top 10, or the bottom 10).
- Hashes, which are maps composed of fields associated with values. Both the field and the value are strings. This is very similar to Ruby or Python hashes.
- Bit arrays (or simply bitmaps): it is possible, using special commands, to handle String values like an array of bits: you can set and clear individual bits, count all the bits set to 1, find the first set or unset bit, and so forth.
- HyperLogLogs: this is a probabilistic data structure which is used in order to estimate the cardinality of a set. Don't be scared, it is simpler than it seems... See later in the HyperLogLog section of this tutorial.
- Streams: append-only collections of map-like entries that provide an abstract log data type. They are covered in depth in the Introduction to Redis Streams.
- 二进制安全
字符串
。列表
: 根据插入顺序排序的字符串元素的集合。它们基本上是链表。集合
: 唯一的、未排序的字符串元素的集合。有序集合
: 类似于集合,但其中每个字符串元素都与一个称为 score 的浮点数值相关联。元素总是按照其得分进行排序,所以与集合不同的是,它可以检索一系列元素(例如,你可能会问:给我前 10 个,或后 10 个)。散列
: 它是由与值相关联的字段组成的映射。字段和值都是字符串。这非常类似于 Ruby 或 Python 的散列。位数组(或简单的位图)
: 可以使用特殊的命令来处理像位数组一样的字符串值:你可以设置和清除单个位,计算所有设置为 1 的位,找到第一个设置或未设置的位,等等。HyperLogLogs
: 这是一个概率数据结构,用于估计集合的基数。不要害怕,事情比看起来简单……请参阅本教程后面的 HyperLogLog 部分。流
: 只添加提供抽象日志数据类型的类似映射的条目的集合。在 Redis 流介绍中有详细介绍。
4 事务 multi, exec, discard
组队阶段
:若有报错,整个队列都会被取消。
127.0.0.1:6379> multi
OK
127.0.0.1:6379(TX)> set k1 v1
QUEUED
127.0.0.1:6379(TX)> set k2 v2
QUEUED
127.0.0.1:6379(TX)> set k3
(error) ERR wrong number of arguments for 'set' command
127.0.0.1:6379(TX)> exec
(error) EXECABORT Transaction discarded because of previous errors.
127.0.0.1:6379>
执行阶段
:只有报错的命令不会被执行,其他命令都会被执行,不会回滚。
127.0.0.1:6379> multi
OK
127.0.0.1:6379(TX)> set k1 v1
QUEUED
127.0.0.1:6379(TX)> incr k1
QUEUED
127.0.0.1:6379(TX)> set k2 v2
QUEUED
127.0.0.1:6379(TX)> exec
1) OK
2) (error) ERR value is not an integer or out of range
3) OK
127.0.0.1:6379>
(全文完)