站长之家是干嘛的,wordpress变成圆角,wordpress 修改页面,springcloud项目搭建文章目录 引言1. Spring整合Redis1.1. 为什么选择Redis作为缓存解决方案#xff1f;Redis的特点和优势Redis与传统关系数据库的对比 1.2. Spring与Redis整合的基本步骤 2. Redis注解式缓存2.1. Spring提供的缓存注解介绍2.2. 使用注解实现方法级别的缓存 3. Redis的击穿、穿透… 文章目录 引言1. Spring整合Redis1.1. 为什么选择Redis作为缓存解决方案Redis的特点和优势Redis与传统关系数据库的对比 1.2. Spring与Redis整合的基本步骤 2. Redis注解式缓存2.1. Spring提供的缓存注解介绍2.2. 使用注解实现方法级别的缓存 3. Redis的击穿、穿透和雪崩问题及解决方案3.1. 几种常见的Redis缓存问题3.2. 解决方案和优化策略 总结 引言
在现代应用开发中缓存是提升性能和扩展性的关键技术之一。而Redis作为一种高性能、内存存储型数据库与Spring框架的结合可以带来更加优雅和灵活的缓存解决方案。本篇博客将深入探讨Spring整合Redis的方法和最佳实践并重点介绍Redis注解式缓存以及解决常见的Redis问题。
1. Spring整合Redis
1.1. 为什么选择Redis作为缓存解决方案
Redis的特点和优势 内存存储Redis将数据存储在内存中因此具有非常高的读写速度。相比传统数据库的磁盘存储Redis能够快速处理大量请求。 键值存储Redis使用键值对key-value的数据结构来存储数据。这种简单的数据模型使得数据操作非常高效和灵活适用于各种场景。 支持多种数据类型Redis支持丰富的数据类型包括字符串、哈希、列表、集合、有序集合等。这些数据类型的支持使得开发者可以更方便地构建复杂的应用逻辑。 缓存功能Redis常被用作缓存服务器可以将频繁访问的数据存储在内存中从而加快数据读取速度。同时Redis还提供了一些高级的缓存策略如过期时间、LRU淘汰等。 发布订阅系统Redis提供了发布订阅pub/sub功能允许客户端通过订阅某个频道来接收消息的推送。这种消息发布订阅模式对于实时通信、消息队列等场景非常有用。 分布式支持Redis支持分布式部署可以将数据分布在多个节点上进行存储和访问。这种分布式的特性使得Redis具备高可用性和横向扩展的能力。
Redis与传统关系数据库的对比 数据模型Redis采用键值对的数据模型而传统关系数据库使用表格模型。这使得Redis在处理简单查询和高并发读写方面更加高效而关系数据库更适合复杂的数据关联和查询操作。 存储方式Redis将数据存储在内存中而关系数据库通常将数据持久化到磁盘中。因此Redis在读写速度方面更快但关系数据库在数据持久化和容量方面具有优势。 功能特性Redis提供了丰富的功能特性如发布订阅、缓存策略等而关系数据库更加注重数据一致性、事务管理以及复杂的查询能力。 扩展性Redis支持分布式部署和横向扩展可以通过添加新的节点来提高性能和容量。而关系数据库通常需要进行垂直扩展即增加更强大的硬件来应对高负载。 数据一致性Redis默认情况下是单机的不保证数据的强一致性而关系数据库通常会保证数据的一致性。
1.2. Spring与Redis整合的基本步骤
准备步骤
打开redis服务导入spring整合redis项目
当spring-context.xml文件需要注册多个.properties结尾的配置文件不能在spring-*中添加注册
applicationContext.xml
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/context xmlns:txhttp://www.springframework.org/schema/txxmlns:aophttp://www.springframework.org/schema/aopxsi:schemaLocationhttp://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 idpropertyConfigurerclassorg.springframework.beans.factory.config.PropertyPlaceholderConfigurerproperty namesystemPropertiesModeName valueSYSTEM_PROPERTIES_MODE_OVERRIDE /property nameignoreResourceNotFound valuetrue /property namelocationslistvalueclasspath:jdbc.properties/valuevalueclasspath:redis.properties/value/list/property/bean!-- 随着后续学习框架会越学越多不能将所有的框架配置放到同一个配制间否者不便于管理 --import resourceapplicationContext-mybatis.xml/importimport resourcespring-redis.xml/importimport resourceapplicationContext-shiro.xml/import
/beans
redis.properties
redis.hostNamelocalhost
redis.port6379
redis.password123456
redis.timeout10000
redis.maxIdle300
redis.maxTotal1000
redis.maxWaitMillis1000
redis.minEvictableIdleTimeMillis300000
redis.numTestsPerEvictionRun1024
redis.timeBetweenEvictionRunsMillis30000
redis.testOnBorrowtrue
redis.testWhileIdletrue
redis.expiration3600spring-redis.xml
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxmlns:cachehttp://www.springframework.org/schema/cachexsi:schemaLocationhttp://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 locationclasspath:redis.properties /--!-- 2. redis连接池配置--bean idpoolConfig classredis.clients.jedis.JedisPoolConfig!--最大空闲数--property namemaxIdle value${redis.maxIdle}/!--连接池的最大数据库连接数 --property namemaxTotal value${redis.maxTotal}/!--最大建立连接等待时间--property namemaxWaitMillis value${redis.maxWaitMillis}/!--逐出连接的最小空闲时间 默认1800000毫秒(30分钟)--property nameminEvictableIdleTimeMillis value${redis.minEvictableIdleTimeMillis}/!--每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3--property namenumTestsPerEvictionRun value${redis.numTestsPerEvictionRun}/!--逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1--property nametimeBetweenEvictionRunsMillis value${redis.timeBetweenEvictionRunsMillis}/!--是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个--property nametestOnBorrow value${redis.testOnBorrow}/!--在空闲时检查有效性, 默认false --property nametestWhileIdle value${redis.testWhileIdle}//bean!-- 3. redis连接工厂 --bean idconnectionFactory classorg.springframework.data.redis.connection.jedis.JedisConnectionFactorydestroy-methoddestroyproperty namepoolConfig refpoolConfig/!--IP地址 --property namehostName value${redis.hostName}/!--端口号 --property nameport value${redis.port}/!--如果Redis设置有密码 --property namepassword value${redis.password}/!--客户端超时时间单位是毫秒 --property nametimeout value${redis.timeout}//bean!-- 4. redis操作模板,使用该对象可以操作redishibernate课程中hibernatetemplete相当于session专门操作数据库。--bean idredisTemplate classorg.springframework.data.redis.core.RedisTemplateproperty nameconnectionFactory refconnectionFactory/!--如果不配置Serializer那么存储的时候缺省使用String如果用User类型存储那么会提示错误User cant cast to String --property namekeySerializerbean classorg.springframework.data.redis.serializer.StringRedisSerializer//propertyproperty namevalueSerializerbean classorg.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer//propertyproperty namehashKeySerializerbean classorg.springframework.data.redis.serializer.StringRedisSerializer//propertyproperty namehashValueSerializerbean classorg.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer//property!--开启事务 --property nameenableTransactionSupport valuetrue//bean!-- 5.配置缓存管理器 --bean idredisCacheManager classorg.springframework.data.redis.cache.RedisCacheManagerconstructor-arg nameredisOperations refredisTemplate/!--redis缓存数据过期时间单位秒--property namedefaultExpiration value${redis.expiration}/!--是否使用缓存前缀与cachePrefix相关--property nameusePrefix valuetrue/!--配置缓存前缀名称--property namecachePrefixbean classorg.springframework.data.redis.cache.DefaultRedisCachePrefixconstructor-arg index0 value-cache-//bean/property/bean!--6.配置缓存生成键名的生成规则--bean idcacheKeyGenerator classcom.zking.ssm.redis.CacheKeyGenerator/bean!--7.启用缓存注解功能--cache:annotation-driven cache-managerredisCacheManager key-generatorcacheKeyGenerator/
/beansresources的配置文件的配置必须要涵盖读取.properties结尾的文件 redisTemplate的使用可以参照jdbcTemplate、amqpTemplate、rabbitMQtemplate等
2. Redis注解式缓存
2.1. Spring提供的缓存注解介绍
package com.zking.ssm.biz;import com.zking.ssm.model.Clazz;
import com.zking.ssm.util.PageBean;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;import java.util.List;
import java.util.Map;public interface ClazzBiz {CacheEvict(value xx,key cid:#cid,allEntries true)int deleteByPrimaryKey(Integer cid);int insert(Clazz record);int insertSelective(Clazz record);// xxcache-cid:1
// key的作用改变原有的key生成规则
// Cacheable(value xx,key cid:#cid,condition #cid 6)CachePut(value xx,key cid:#cid,condition #cid 6)Clazz selectByPrimaryKey(Integer cid);int updateByPrimaryKeySelective(Clazz record);int updateByPrimaryKey(Clazz record);ListClazz listPager(Clazz clazz, PageBean pageBean);ListMap listMapPager(Clazz clazz, PageBean pageBean);
}Cacheable缓存方法的返回值,可读写 CachePut更新缓存的方法,只写不读,适合读写分离 CacheEvict移除缓存中的数据
2.2. 使用注解实现方法级别的缓存
在Spring Boot项目中开启注解式缓存 定义缓存的Key和Value的生成策略 编写示例代码演示注解式缓存的使用方式
3. Redis的击穿、穿透和雪崩问题及解决方案 3.1. 几种常见的Redis缓存问题
击穿大量请求同时查询一个不存在的Key导致请求都落到数据库上 穿透恶意请求查询一个不存在的Key导致请求直接落到数据库上 雪崩大量缓存失效导致所有请求都落到数据库上
3.2. 解决方案和优化策略
击穿解决方案设置热点数据永不过期、互斥锁、布隆过滤器等 穿透解决方案空值缓存、参数校验、布隆过滤器等 雪崩解决方案限流、降级、异步更新缓存等
总结
在本篇博客中我们深入探讨了Redis与Spring的整合以及注解式缓存的使用方式。通过学习和实践我们了解了Redis作为一种高性能缓存解决方案的优势并且掌握了基于Spring框架使用Redis的方法。