默认有三种方式连接redis.
第一种:jedis—传统的项目–ssm
第二种:lettuce:---->刚出现没有多久就被springboot整合进来。
第三种:springboot连接redis
(1)引入jedis依赖
redis.clients jedis4.3.1
(2)编写相关的代码
@Test
public void test01(){
// Jedis(String host, int port)
Jedis jedis = new Jedis("192.168.80.128",6379);
// 该类包含很多对redis操作的方法,这些方法和原来我们使用的命令一样
Set keys = jedis.keys("*");
System.out.println(keys);
// 对string数据类型操作
jedis.set("k1", "坤");
String k1 = jedis.get("k1");
System.out.println("k1: " + k1);
jedis.setex("k2",16,"有很多");
String k2 = jedis.get("k2");
System.out.println("k2: " + k2);
// 对hash数据类型操作
Map map = new HashMap<>();
map.put("name","座右铭");
map.put("age","18");
map.put("hobby","优惠点");
jedis.hset("k3",map);
Map k3 = jedis.hgetAll("k3");
System.out.println(k3);
// 对list类型数据操作
jedis.lpush("k4","座右铭","优惠点","老油条","屏幕膜","万美元");
List k4 = jedis.lrange("k4", 0, -1);
System.out.println(k4);
jedis.close();
}
每次使用jedis对象时 都需要自己创建,当使用完后,需要关闭该对象。===>jedis中也存在连接池.
@Test
public void testPool(){
//连接池的配置信息
JedisPoolConfig config=new JedisPoolConfig();
config.setMaxTotal(10);//最多的连接个数
config.setMaxIdle(10); //最多空闲的连接个数
config.setMinIdle(2); //最小的空闲个数
config.setTestOnBorrow(true);//在获取连接对象时是否验证该连接对象的连通性
//创建连接池对象
JedisPool jedisPool=new JedisPool(config,"192.168.80.128",6379);
Jedis jedis = jedisPool.getResource();
//清空当前库
jedis.flushDB();
String set = jedis.set("k1", "v1");
String k1 = jedis.get("k1");
System.out.println(k1);
jedis.close();//归还连接池
}
@Test
public void test03(){
//连接池的配置信息
JedisPoolConfig config=new JedisPoolConfig();
config.setMaxTotal(100);//最多的连接个数
config.setMaxIdle(10); //最多空闲的连接个数
config.setMinIdle(2); //最小的空闲个数
config.setTestOnBorrow(true);//在获取连接对象时是否验证该连接对象的连通性
//创建连接池对象
JedisPool jedisPool=new JedisPool(config,"192.168.80.128",6379);
long start = System.currentTimeMillis();
for(int i=0;i<10000;i++){
Jedis jedis = jedisPool.getResource();
String ping = jedis.ping();
jedis.close();
}
long end = System.currentTimeMillis();
System.out.println("耗时:"+(end-start));
}
@Test
public void test02(){
long start = System.currentTimeMillis();
//Jedis(String host, int port)
for(int i=0;i<10000;i++){
Jedis jedis=new Jedis("192.168.80.128",6379);
String ping = jedis.ping();
jedis.close();
}
long end = System.currentTimeMillis();
System.out.println("耗时:"+(end-start));
}
springboot在整合redis时提高两个模板类,StringRedisTemplate和RedisTemplate.以后对redis的操作都在该模板类中。StringRedisTemplate是RedisTemplate的子类。
org.springframework.boot spring-boot-starter-data-redis org.apache.commons commons-pool2
修改配置文件
#redis的配置信息 spring.data.redis.host=192.168.80.128 spring.data.redis.port=6379 #最多获取数 spring.data.redis.lettuce.pool.max-active=8 spring.data.redis.lettuce.pool.max-wait=-1ms spring.data.redis.lettuce.pool.max-idle=8 spring.data.redis.lettuce.pool.min-idle=0
测试:
package com.zym;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.*;
import java.util.*;
@SpringBootTest
class SpringbootRedisApplicationTests {
//因为springboot整合redis时会把StringRedisTemplate创建并交于spring容器管理
@Autowired
private StringRedisTemplate redisTemplate;
@Test
void contextLoads() {
//关于key的操作
System.out.println(redisTemplate.keys("*"));//获取所有的key
//是否存在指定的key
Boolean k1 = redisTemplate.hasKey("k1");
System.out.println("是否存在指定的key:" + k1);
// 删除指定的key
Boolean k11 = redisTemplate.delete("k1");
System.out.println("删除是否成功:" + k11);
}
@Test
void test1() {
//操作字符串===StringRedisTemplate会把对每一种数据的操作单独封装成一个类。
//ValueOperations专门操作字符串
ValueOperations forValue = redisTemplate.opsForValue();
//set 向指定key中设置value
forValue.set("k1", "你好");
//get 获取指定key的值
String k12 = forValue.get("k1");
System.out.println("k12==" + k12);
//mset 同时设置一个或多个 key-value 对
Map map = new HashMap<>();
map.put("k1", "座右铭");
map.put("k2", "有很多");
map.put("k3", "另一条");
map.put("k4", "屏幕膜");
forValue.multiSet(map);
//mget 获取所有(一个或多个)给定 key 的值
List list = new ArrayList<>();
list.add("k1");
list.add("k2");
list.add("k3");
list.add("k4");
List stringList = forValue.multiGet(list);
System.out.println(stringList);
//setnx
Boolean aBoolean = forValue.setIfAbsent("k2", "你也好");
System.out.println("是否存入成功:" + aBoolean);
//incr decr
Long k3 = forValue.increment("k3", 20);
System.out.println("k3:" + k3);
Long decrement = forValue.decrement("k3", 20);
System.out.println("k3:" + decrement);
}
@Test
void test2() {
//可以hash操作
//hset
HashOperations forHash = redisTemplate.opsForHash();
forHash.put("k1", "name", "平家祥");
forHash.put("k1", "age", "16");
forHash.put("k1", "sex", "男");
Map map = new HashMap<>();
map.put("name", "胡萌");
map.put("age", "17");
map.put("sex", "女");
forHash.putAll("k2", map);
//hget
System.out.println(forHash.get("k1", "name"));
//hgetall
Map
如果每次使用都人为指定序列化方式,统一设置redisTemplate的序列化
package com.zym.springbootredis.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@SuppressWarnings("all")
@Bean //该方法的返回对象交于spring容器管理
public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
RedisTemplate template = new RedisTemplate<>();
RedisSerializer redisSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
template.setConnectionFactory(factory);
//key序列化方式
template.setKeySerializer(redisSerializer);
//value序列化
template.setValueSerializer(jackson2JsonRedisSerializer);
//value hashmap序列化
template.setHashValueSerializer(jackson2JsonRedisSerializer);
//field序列化 key field value
template.setHashKeySerializer(redisSerializer);
return template;
}
}
上面的连接都是连接的单机版的redis,真实项目它们的redis都是集群模式.
server.port=8888 spring.datasource.url=jdbc:mysql://192.168.1.51:3306/qy163 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.username=root spring.datasource.password=123456 #日志 mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl #redis的配置信息 #spring.redis.host=192.168.80.128 #spring.redis.port=6379 #最多获取数 spring.redis.lettuce.pool.max-active=8 spring.redis.lettuce.pool.max-wait=-1ms spring.redis.lettuce.pool.max-idle=8 spring.redis.lettuce.pool.min-idle=0 # 设置redis重定向的次数---根据主节点的个数 spring.redis.cluster.max-redirects=3 spring.redis.cluster.nodes=192.168.80.128:7001,192.168.80.128:7002,192.168.80.128:7003,192.168.80.128:7004,192.168.80.128:7005,192.168.80.128:7006
(1) 缓存的原理

(2)缓存的作用:
减少访问数据库的频率。–提高系统的性能。
(3)什么样的数据适合放入缓存
- 查询频率高的
- 修改频率低的
- 数据安全性要求低的。
(4)如何使用缓存
4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.12.RELEASE com.zym demo111 0.0.1-SNAPSHOT demo111 demo111 8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
配置文件
server.port=8888 spring.datasource.url=jdbc:mysql:///qy163 spring.datasource.password=123456 spring.datasource.username=root spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl #redis的配置信息 spring.redis.host=192.168.80.128 spring.redis.port=6379 #最多获取数 spring.redis.lettuce.pool.max-active=8 spring.redis.lettuce.pool.max-wait=-1ms spring.redis.lettuce.pool.max-idle=8 spring.redis.lettuce.pool.min-idle=0
servie
@Service
public class StudentServiceImpl implements StudentService {
@Resource
private StudentMapper studentMapper;
@Resource
private RedisTemplate redisTemplate;
@Override
public List findAll() {
ValueOperations forValue = redisTemplate.opsForValue();
Object student = forValue.get("students");
if (student != null) {
return (List) student;
}
List students = studentMapper.selectList(null);
if (students != null) {
forValue.set("students", students);
}
return students;
}
@Override
public Student getById(Integer id) {
ValueOperations forValue = redisTemplate.opsForValue();
Object o = forValue.get("student" + id);
if (o != null) {
if (o instanceof NullObject) {
return null;
}
return (Student) o;
}
Student student = studentMapper.selectById(id);
if (student != null) {
forValue.set("student" + id, student);
} else {
forValue.set("student" + id, new NullObject(), 5, TimeUnit.MINUTES);
}
return student;
}
public Integer insert(Student student) {
redisTemplate.delete("student" + student.getId());
return studentMapper.insert(student);
}
public Integer update(Student student) {
redisTemplate.delete("student" + student.getId());
return studentMapper.updateById(student);
}
public Integer delete(Integer id) {
redisTemplate.delete("student" + id);
return studentMapper.deleteById(id);
}
}
当执行增删改操纵时必须保证缓存和数据库数据一致性。—删除缓存
(1)通过使用jmeter压测工具测试

同一个库存数被多个线程卖,线程安全问题。—思考:之间出现线程安全问题时如何解决。
可以使用锁解决:----synchronized和Lock锁
package com.ykq.service;
import com.ykq.dao.StockDao;
import com.ykq.entity.Stock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* @program: redis-lock-qy145
* @description:
* @author: 闫克起2
* @create: 2022-03-01 11:13
**/
@Service
public class StockService_lock_syn {
@Autowired
private StockDao stockDao;
public static Object o=new Object();
Lock lock=new ReentrantLock();
public /*synchronized*/ String jianStock(Integer pid){
try {
lock.lock();//加锁
//1. 查询指定的商品库存
Stock stock = stockDao.selectById(pid);
if (stock.getNum() > 0) {
//2.库存减1
stock.setNum(stock.getNum() - 1);
stockDao.updateById(stock);
System.out.println("库存剩余数量:" + stock.getNum());
return "减库存成功";
} else {
System.out.println("库存不足");
return "库存减失败";
}
}
finally {
lock.unlock(); //释放锁
}
}
}
上面的synchronized或Lock锁是否适合集群模式|分布式系统。不适合、因为synchronized都是基于JVM的本地锁。

需要在idea中跑项目的集群
配置nginx
启动nginx
jmeter压测
两台集群出现重卖问题。

@Autowired
private StringRedisTemplate redisTemplate;
/**
* 2.0
* @param pid
* @return
*/
public String jianStock(Integer pid){
//占锁
ValueOperations forValue = redisTemplate.opsForValue();
//占锁失败
while (!forValue.setIfAbsent("product::" + pid, "", 30, TimeUnit.SECONDS)){
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//占锁成功
try {
//1. 查询指定的商品库存
Stock stock = stockDao.selectById(pid);
if (stock.getNum() > 0) {
//2.库存减1
stock.setNum(stock.getNum() - 1);
stockDao.updateById(stock);
System.out.println("库存剩余数量:" + stock.getNum());
return "减库存成功";
} else {
System.out.println("库存不足");
return "库存减失败";
}
}finally {
//释放锁资源
redisTemplate.delete("product::"+pid);
}
}
如果你的业务代码的执行时间超过30s,当前线程删除的是其他线程的锁资源。 --watchDog机制
每个10s检测当前线程是否还持有所资源,如果持有则为当前线程延迟。—可以自己设置watchDog机制,—第三方Redission完美的解决分布式锁。

org.redisson redisson 3.13.4
(2)main函数
@Bean //创建redisson交于spring容器来管理
public RedissonClient redisson() {
Config config = new Config();
config.useSingleServer().setAddress("redis://192.168.80.128:6379");
RedissonClient redisson = Redisson.create(config);
return redisson;
}
(3)使用
@Autowired
private RedissonClient redisson;
/**
* 3.0
* @param pid
* @return
*/
public String jianStock(Integer pid){
RLock lock = redisson.getLock("product::" + pid);
try {
lock.lock(30,TimeUnit.SECONDS);//加锁: 如果程序执行是出现一次
//1. 查询指定的商品库存
Stock stock = stockDao.selectById(pid);
if (stock.getNum() > 0) {
//2.库存减1
stock.setNum(stock.getNum() - 1);
stockDao.updateById(stock);
System.out.println("库存剩余数量:" + stock.getNum());
return "减库存成功";
} else {
System.out.println("库存不足");
return "库存减失败";
}
}finally {
lock.unlock();
}
}
一、Redis是什么?
redis是使用C语言编写的一个高速缓存数据库,它以key-value形式存储数据,而且它支持的数据类型非常丰富。
二、Redis都有哪些使用场景?
- 热点数据的缓存
- 计数器
- 排行榜。
- 实现分布式锁
- 使用session的共享–后面项目时使用
三、Redis支持的数据类型有哪些?
- String 2. Hash 3.List 4.Set 5.ZSet(sorted set)
四、Redis为什么是单线程的?
因为Redis是基于内存的操作,CPU不是Redis的瓶颈,Redis的瓶颈最有可能是机器内存的大小或者网络带宽。既然单线程容易实现,而且CPU不会成为瓶颈,那就顺理成章地采用单线程的方案了
五、Redis真的是单线程的吗?
并不是真的单线程,比如:RDB—Bgsave时,创建一个子线程
六、Redis持久化有几种方式?
RDB(快照):save,bgsave,配置文件自动
AOF:将每一条命令写入AOF文件中
七、什么是缓存穿透?怎么解决?
- 查询的数据在数据库中不存在,缓存中也不存在,这时有可能有人恶意访问这种数据。这些请求都会访问数据库,从而出现数据库压力过大。
情景:(1) 比如id不合法— (2)确实数据库中不存在。
解决: 1. 在controller加校验
2. 我们可以在缓存中存入一个空对象,但是对象的过期时间不要太长,一般不会
超过5分钟。
3. 可以使用布隆过滤器。
八、怎么保证缓存和数据库数据的一致性?
- 设置合理的过期时间
- 当执行增删改时需要删除缓存数据
九、Redis,什么是缓存雪崩?怎么解决?
缓存雪崩:就是在某一时刻出现大量数据过期,而这时就有大量的请求访问该数据,这种现象叫做缓存雪崩。
什么情况下会出现大量数据过期:
- 项目刚刚上线
- redis服务器宕机
- 缓存数据真实过期
解决方案:
- 上线前预热数据。
- 集群
- 设置过期时间时要分散设置。
十、Redis怎么实现分布式锁?
使用redis中的setnx命令–占锁,当业务代码执行完毕后释放锁资源,而释放锁的命令是del。
十一、 redis在实现分布式锁式有什么缺陷
超时问题: 业务代码执行时间超过锁时间。使用:watchDog机制。 我们使用第三方:redisson
十二、Redis 淘汰策略有哪些?
