Redis注解式开发结合SSM项目使用与Quartz框架介绍以及击穿、穿透、雪崩问题解决

目录

一、SSM项目整合Redis

1.1 导入pom依赖

1.2 spring-redis.xml

1.3 spring上下文配置 

 

二、Redis注解式开发

2.1 Cacheable 注解

2.2 自定义策略

2.3 CachePut 注解

 

三、Redis中缓冲、击穿、穿透、雪崩问题解决

3.1 缓冲问题 —— Quartz 框架 

3.2 常见的三种问题解决方案 


一、SSM项目整合Redis

 redis是nosql数据库,mysql是sql数据库,都是数据库因此可以参考mysql整合ssm项目的过程。

 

1.1 导入pom依赖

  <properties>    <redis.version>2.9.0</redis.version><redis.spring.version>1.7.1.RELEASE</redis.spring.version></properties><dependencies><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>${redis.version}</version></dependency><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId><version>${redis.spring.version}</version></dependency></dependencies>

1.2 spring-redis.xml

  1. 注册 redis.properties
  2. 配置数据源
  3. 连接工厂
  4. 配置序列化
  5. 配置redis的key生成策略
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:cache="http://www.springframework.org/schema/cache"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/cachehttp://www.springframework.org/schema/cache/spring-cache.xsd"><!-- 1. 引入properties配置文件 --><context:property-placeholder location="classpath:redis.properties" /><!-- 2. redis连接池配置--><bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"><!--最大空闲数--><property name="maxIdle" value="300"/><!--连接池的最大数据库连接数  --><property name="maxTotal" value="${redis.maxTotal}"/><!--最大建立连接等待时间--><property name="maxWaitMillis" value="${redis.maxWaitMillis}"/><!--逐出连接的最小空闲时间 默认1800000毫秒(30分钟)--><property name="minEvictableIdleTimeMillis" value="${redis.minEvictableIdleTimeMillis}"/><!--每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3--><property name="numTestsPerEvictionRun" value="${redis.numTestsPerEvictionRun}"/><!--逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1--><property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}"/><!--是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个--><property name="testOnBorrow" value="${redis.testOnBorrow}"/><!--在空闲时检查有效性, 默认false  --><property name="testWhileIdle" value="${redis.testWhileIdle}"/></bean><!-- 3. redis连接工厂 --><bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"destroy-method="destroy"><property name="poolConfig" ref="poolConfig"/><!--IP地址 --><property name="hostName" value="${redis.hostName}"/><!--端口号  --><property name="port" value="${redis.port}"/><!--如果Redis设置有密码  --><property name="password" value="${redis.password}"/><!--客户端超时时间单位是毫秒  --><property name="timeout" value="${redis.timeout}"/></bean><!-- 4. redis操作模板,使用该对象可以操作redishibernate课程中hibernatetemplete,相当于session,专门操作数据库。--><bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"><property name="connectionFactory" ref="connectionFactory"/><!--如果不配置Serializer,那么存储的时候缺省使用String,如果用User类型存储,那么会提示错误User can't cast to String!!  --><property name="keySerializer"><bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/></property><property name="valueSerializer"><bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/></property><property name="hashKeySerializer"><bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/></property><property name="hashValueSerializer"><bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/></property><!--开启事务  --><property name="enableTransactionSupport" value="true"/></bean><!--  5.配置缓存管理器  --><bean id="redisCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager"><constructor-arg name="redisOperations" ref="redisTemplate"/><!--redis缓存数据过期时间单位秒--><property name="defaultExpiration" value="${redis.expiration}"/><!--是否使用缓存前缀,与cachePrefix相关--><property name="usePrefix" value="true"/><!--配置缓存前缀名称--><property name="cachePrefix"><bean class="org.springframework.data.redis.cache.DefaultRedisCachePrefix"><constructor-arg index="0" value="-cache-"/></bean></property></bean><!--6.配置缓存生成键名的生成规则--><bean id="cacheKeyGenerator" class="com.zking.ssm.redis.CacheKeyGenerator"></bean><!--7.启用缓存注解功能--><cache:annotation-driven cache-manager="redisCacheManager" key-generator="cacheKeyGenerator"/>
</beans>

redis.properties 配置文件:

redis.hostName=localhost
redis.port=6379
redis.password=123456
redis.timeout=10000
redis.maxIdle=300
redis.maxTotal=1000
redis.maxWaitMillis=1000
redis.minEvictableIdleTimeMillis=300000
redis.numTestsPerEvictionRun=1024
redis.timeBetweenEvictionRunsMillis=30000
redis.testOnBorrow=true
redis.testWhileIdle=true
redis.expiration=3600

1.3 spring上下文配置 

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><!--1. 引入外部多文件方式 --><bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /><property name="ignoreResourceNotFound" value="true" /><property name="locations"><list><value>classpath:jdbc.properties</value><value>classpath:redis.properties</value></list></property></bean><!--  随着后续学习,框架会越学越多,不能将所有的框架配置,放到同一个配制间,否者不便于管理  --><import resource="applicationContext-mybatis.xml"></import><import resource="spring-redis.xml"></import><import resource="applicationContext-shiro.xml"></import>
</beans>

 

二、Redis注解式开发

首先需要一个缓冲策略类,用于储存信息

package com.ycxw.ssm.redis;import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.util.ClassUtils;import java.lang.reflect.Array;
import java.lang.reflect.Method;@Slf4j
public class CacheKeyGenerator implements KeyGenerator {// custom cache keypublic static final int NO_PARAM_KEY = 0;public static final int NULL_PARAM_KEY = 53;@Overridepublic Object generate(Object target, Method method, Object... params) {StringBuilder key = new StringBuilder();key.append(target.getClass().getSimpleName()).append(".").append(method.getName()).append(":");if (params.length == 0) {key.append(NO_PARAM_KEY);} else {int count = 0;for (Object param : params) {if (0 != count) {//参数之间用,进行分隔key.append(',');}if (param == null) {key.append(NULL_PARAM_KEY);} else if (ClassUtils.isPrimitiveArray(param.getClass())) {int length = Array.getLength(param);for (int i = 0; i < length; i++) {key.append(Array.get(param, i));key.append(',');}} else if (ClassUtils.isPrimitiveOrWrapper(param.getClass()) || param instanceof String) {key.append(param);} else {//Java一定要重写hashCode和eqaulskey.append(param.hashCode());}count++;}}String finalKey = key.toString();
//        IEDA要安装lombok插件log.debug("using cache key={}", finalKey);return finalKey;}
}

 

2.1 Cacheable 注解

1、定义查询接口使用Cacheable注解

Spring会在其被调用后将其返回值缓存起来,以保证下次利用同样的参数来执行该方法时可以直接从缓存中获取结果,而不需要再次执行该方法。Spring在缓存方法的返回值时是以键值对进行缓存的,值就是方法的返回结果。

package com.ycxw.ssm.biz;import com.zking.ssm.model.Clazz;
import com.zking.ssm.util.PageBean;import org.springframework.cache.annotation.Cacheable;import java.util.List;
import java.util.Map;public interface ClazzBiz {@Cacheable("clz")Clazz selectByPrimaryKey(Integer cid);}

 2、编写测试类

package com.ycxw.shiro;import com.zking.ssm.biz.ClazzBiz;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class ClazzBizTest {@Autowiredprivate ClazzBiz clazzBiz;@Testpublic void test1(){System.out.println(clazzBiz.selectByPrimaryKey(10));System.out.println(clazzBiz.selectByPrimaryKey(10));}}

第一次运行时:(查询两次)

 

第二次运行时:(无查询语句)

 

再次运行相同的数据时不再查询了,直接从缓冲中拿取数据:

2.2 自定义策略

@Cacheable可以指定三个属性,value、key和condition。 

我可定义key值来修改我们保存到redis缓冲的key值,并且可通过condition来制定什么时候需要缓冲,进一步优化性能。

自定义策略,如果查询的cid大于6才进行缓冲 

package com.ycxw.ssm.biz;import com.zking.ssm.model.Clazz;
import com.zking.ssm.util.PageBean;import org.springframework.cache.annotation.Cacheable;import java.util.List;
import java.util.Map;public interface ClazzBiz {@Cacheable(value = "clz",key = "'cid:'+#cid",condition = "#cid > 6")Clazz selectByPrimaryKey(Integer cid);}

2.3 CachePut 注解

它的使用与Cacheable的使用一致,它们的区别:

  • Cacheable:会在redis中存储数据,同时也会读取数据
  • CachePut:只会在redis储存数据,不会进行读取操作
package com.ycxw.ssm.biz;import com.zking.ssm.model.Clazz;
import com.zking.ssm.util.PageBean;import org.springframework.cache.annotation.Cacheable;import java.util.List;
import java.util.Map;public interface ClazzBiz {@CachePut(value = "clz",key = "'cid:'+#cid",condition = "#cid > 6")Clazz selectByPrimaryKey(Integer cid);}

测试:

public class ClazzBizTest {@Autowiredprivate ClazzBiz clazzBiz;@Testpublic void test1(){System.out.println(clazzBiz.selectByPrimaryKey(9));System.out.println(clazzBiz.selectByPrimaryKey(9));}}

不管运行多少次,它还是会查询数据库,即便已经将数据存储到redis中

三、Redis中缓冲、击穿、穿透、雪崩问题解决

3.1 缓冲问题 —— Quartz 框架 

        现在模拟一个场景:我在某系统中增加了一条数据,在主界面中会显示该条数据。而数据是从缓冲中拿取的,而新增的数据并没有立即添加到缓冲中。那我们如何保证redis数据与数据库数据的一致性呢?

方案一:手动刷新数据同步策略

        这样我们每次添加了新的数据,需要手动点击刷新缓冲键,显然这个对管理者不便的。

方案二:利用Quartz作业调度框架,定时刷新任务

        Quartz是一个开源的作业调度框架,用于在Java应用程序中实现任务调度和定时任务管理。它提供了一种简单而强大的方式来安排和执行各种类型的作业,包括定时任务、周期性任务和异步任务。

        Quartz框架的核心概念是作业(Job)和触发器(Trigger)。作业是要执行的任务,而触发器定义了作业执行的时间和频率。通过配置作业和触发器,可以实现灵活的任务调度和执行。

在Redis中,它本身并没有直接使用Quartz框架。然而,我们可以结合使用QuartzRedis来实现一些特定的功能,例如:

  • 使用Quartz调度任务,将任务的执行结果存储到Redis中,以便其他系统或模块可以读取和处理。
  • 使用Quartz定时清理Redis中的过期数据,以确保Redis的存储空间得到有效利用。
  • 使用Quartz定时刷新Redis中的缓存数据,以保持数据的最新性。
  • 使用Quartz和Redis实现分布式锁机制,确保在多个节点上的任务调度不会发生冲突。

3.2 常见的三种问题解决方案 

1、击穿(Cache Miss) 击穿指的是在高并发情况下,当一个缓存键(key)不存在于缓存中,但是被大量请求同时查询时,这些请求会直接访问数据库,导致数据库压力过大。这种情况下,缓存无法发挥作用,而且数据库可能会因此而崩溃。

解决方案:

  • 使用互斥锁(Mutex Lock)或分布式锁(Distributed Lock)来保护数据库访问,确保只有一个请求能够访问数据库,其他请求等待结果。
  • 在缓存中设置短暂的空值(Null Value),以防止大量请求同时查询同一个缓存键。

 

2、穿透(Cache Penetration) 穿透指的是当一个缓存键不存在于缓存中,并且被大量请求同时查询时,这些请求都会直接访问数据库,导致数据库压力过大。与击穿不同的是,穿透是因为查询的键根本不存在于缓存中。

解决方案:

  • 在查询数据库之前,可以添加一个布隆过滤器(Bloom Filter)来快速判断查询的键是否存在于缓存中。如果不存在,可以直接返回空结果,而不是访问数据库。
  • 对于查询结果为空的情况,也可以将空结果缓存一段时间,以避免频繁查询数据库。

 

3、雪崩(Cache Avalanche) 雪崩指的是在缓存中大量的缓存键同时过期或失效,导致大量请求直接访问数据库,造成数据库压力过大,甚至崩溃。

解决方案:

  • 设置缓存键的过期时间时,可以引入随机值,使得缓存键的过期时间分散开来,避免大量缓存键同时过期。
  • 使用热点数据预加载(Cache Pre-warming)策略,提前加载热门数据到缓存中,减少缓存键同时失效的可能性。
  • 使用多级缓存架构,将缓存分为多个层级,以降低整体缓存失效的风险。

 

        总结: 在解决这些问题时,我们需要综合考虑系统的并发性、可用性和性能。通过合理的缓存策略、锁机制、预加载与使用调度框架等手段,可以有效地解决Redis中的击穿、穿透和雪崩问题,提高系统的稳定性和性能。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/133308.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Git查询某次提交属于哪个分支

在Android studio&#xff08;JetBrains系列也类似&#xff09;左下角&#xff0c;可以看到所有提交信息。 选中某一次提交信息&#xff0c;右键&#xff0c;选择“Copy Revision Number”&#xff0c;如下图&#xff1a; 打开Android studio的Terminal&#xff0c;输入git b…

【广州华锐互动】3D全景虚拟旅游在文旅行业的应用场景

随着科技的不断发展&#xff0c;3D全景虚拟旅游正在成为一种新兴的旅游体验方式&#xff0c;它可以帮助旅游者更加深入地了解旅游信息&#xff0c;提升旅游体验。下面我们将详细介绍3D全景虚拟旅游可以应用于哪些场景。 一、旅游规划 3D全景虚拟旅游可以帮助旅游者更加直观地进…

SQL数据库使用方法

首先打开sqlite3.exe所在文件夹&#xff0c;如图1 图1 在文件夹路径中将路径改为cmd&#xff0c;如图2所示 图2 在弹出的cmd窗口中输入如图3所示。 图3 sqlite3 tichiceliang.db 其中tichiceliang是数据库名称。然后按enter&#xff0c;再在cmd中输入.table,可以看到文件夹目…

PC9502双节锂电池充电管理芯片高精度外围元件少SOP8封装

■ 产品概述 PC9502 系列是一款双节锂电池充电管理芯片。 该芯片 包含了充电状态检测、充电过程、温度检测等&#xff0c;内部还集成 一个高精度的基准电压模块&#xff0c;此芯片采用 SOP-8 封装。 PC9502 充电包含三种模式&#xff1a;预充电&#xff0c;恒流充电&#x…

Cassandra介绍(二)

1.8. 数据 存储 &#xff08; CommitLog、MemTable、SSTable &#xff09; 写请求分别到 CommitLog 和 MemTable, 并且 MemTable 的数据会刷写到磁盘 SSTable 上. 除 了写数据,还有索引也会保存到磁盘上. 先将数据写到磁盘中的 commitlog&#xff0c;同时追加到中内存中的数据…

LinkdeList集合

1.LinkdeList集合的特点&#xff1a; 1.LinkedList是集合的一个实现类 2.LinkedList内部封装了一个双向链表 3.LinkedList集合的增删快&#xff0c;查询慢 4.线程不安全 2.LinkedList集合的方法 1.添加 1.boolean add(Object element) 将元素附加到链表末尾 2.boolean add(int…

通过you-get命令行工具下载B站等常见网站高清视频

参考&#xff1a; you-get&#xff1a;https://github.com/soimort/you-get EditThisCookie 源码&#xff1a;https://github.com/ETCExtensions/Edit-This-Cookie EditThisCookie 官网&#xff1a;https://www.editthiscookie.com/ you-get 是一个非常好用的命令行版视频下载工…

【C语言从入门到放弃 3】函数、枚举、指针、函数指针和回调函数详解

摘要&#xff1a;C语言是一种强大而灵活的编程语言&#xff0c;它提供了丰富的函数、枚举、指针、函数指针和回调函数等特性&#xff0c;使得程序员能够更好地进行程序设计和开发。本文将通过简单的示例代码介绍这些特性的用法和优势。 一、函数 函数是C语言中的基本构建块&a…

分享98个节日庆典PPT,总有一款适合您

分享98个节日庆典PPT&#xff0c;总有一款适合您 PPT下载链接&#xff1a;https://pan.baidu.com/s/1gNj_uRLz9a5uTG97ezma7Q?pwd8888 提取码&#xff1a;8888 Python采集代码下载链接&#xff1a;采集代码.zip - 蓝奏云 学习知识费力气&#xff0c;收集整理更不易。知识付…

生产环境中oracle dba权限检查和回收相关命令汇总

一、oracle dba权限检查和回收的作用和意义 在Oracle数据库生产环境中&#xff0c;回收oracle dba权限的作用和意义重大&#xff0c;具体表现在以下三方面&#xff1a; 安全性&#xff1a;回收赋权检查和回收可以确保数据库中的权限控制得到有效管理。通过检查和回收权限&…

目标检测YOLO系列从入门到精通技术详解100篇-【目标检测】机器视觉

目录 前言 几个高频面试题目 像素和像元如何选择?

RK356X Android13.0 HDMI和喇叭同时出声音

补丁适用范围:RK356X Android13.0 Android默认音频输出逻辑,不接HDMI默认喇叭音频输出,若检测到HDMI接入后,关闭喇叭输出,开启HDMI音频输出,但是BOX产品的使用场景需要插入HDMI后,喇叭仍然输出,可加入此补丁 $ vim frameworks/base/services/core/java/com/android/s…

Django初窥门径-自定义用户模型

前言 自定义用户模型在Django应用中是一个重要的话题&#xff0c;它涉及到如何根据您的项目需求以及特定的用户身份验证和授权需求来调整用户模型。在以下前言中&#xff0c;我将讲述为什么自定义用户模型是如此重要以及其潜在的优势&#xff1a; 随着Web应用的不断发展&…

科智牧RFID|生猪养殖信息化管理RFID方案提供

以RFID技术为手段&#xff0c;以网络技术和数据库技术为支撑&#xff0c;构建生猪的养殖信息化管理方案&#xff0c;为养殖企业建立完善的生猪档案系统&#xff0c;实现养殖企业信息化管理&#xff0c;做到生猪养殖环节从饲料、养殖、防疫、治疗的信息记录与管理&#xff0c;也…

[计算机网络]认识“协议”

认识“协议” 文章目录 认识“协议”序列化和反序列化网络计算器引入Sock类设计协议编写服务端类启动服务端编写客户端类启动客户端程序测试 序列化和反序列化 在网络体系结构中&#xff0c;应用层的应用程序会产生数据&#xff0c;这个数据往往不是简单的一段字符串数据&…

社科赛斯预测考研趋势,竞争白热化后,稳上岸还是冲名校?

对于考研党来说&#xff0c;择校应该是备考过程中最纠结的一件事情了。这几年来影响院校选择的情况愈加复杂多变&#xff0c;单一志愿的限制下&#xff0c;如何预测报名走向&#xff0c;如何选择院校才能够成功上岸&#xff0c;不像是一个人的战斗&#xff0c;更像是一场几百万…

利用h5py加速数据集读取

直接使用&#xff1a; def store_in_h5(images, output_filename):with h5py.File(output_filename, w) as hf:data_group hf.create_group(data)label_group hf.create_group(label)for i, (data_patches, label_patches) in enumerate(images):for j, (data, label) in en…

按键精灵中的函数使用

1. 命令名称&#xff1a;Function 函数 &#xff1b; 2. 命令功能&#xff1a;定义一个函数 &#xff1b; 3. 语法格式&#xff1a; Function 函数名([自定义参数1, 自定义参数2, ...]) 执行语句 [函数名返回值]End Function 4. 参数说明&#xff1a; 函…

什么是4A架构

4A架构通常在软件工程或系统架构领域中提到&#xff0c;特别是与企业级应用和系统设计相关。但是&#xff0c;“4A”这个术语可能有不同的解释&#xff0c;具体取决于上下文。以下是几种可能的含义&#xff1a; 身份认证&#xff08;Authentication&#xff09;、授权&#xff…

飞书开发学习笔记(二)-云文档简单开发练习

飞书开发学习笔记(二)-云文档简单开发练习 一.云文档飞书开发环境API 首先还是进入开放平台 飞书开放平台&#xff1a;https://open.feishu.cn/app?langzh-CN 云文档相关API都在“云文档”目录中&#xff0c;之下又有"云空间",“文档”&#xff0c;“电子表格”&a…