• 【Redis】Redis与SSM整合&Redis注解式缓存&Redis解决缓存问题


    一,Redis与ssm整合

    1.1 pom.xml配置

    在pom.xml中配置相关的redis文件

    redis文件:

    1. <redis.version>2.9.0redis.version>
    2. <redis.spring.version>1.7.1.RELEASEredis.spring.version>
    3. <dependency>
    4. <groupId>redis.clientsgroupId>
    5. <artifactId>jedisartifactId>
    6. <version>${redis.version}version>
    7. dependency>

    整个pom.xml文件: 

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <groupId>org.examplegroupId>
    6. <artifactId>ssm2artifactId>
    7. <version>1.0-SNAPSHOTversion>
    8. <packaging>warpackaging>
    9. <name>ssm2 Maven Webappname>
    10. <url>http://www.example.comurl>
    11. <properties>
    12. <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    13. <maven.compiler.source>1.8maven.compiler.source>
    14. <maven.compiler.target>1.8maven.compiler.target>
    15. <maven.compiler.plugin.version>3.7.0maven.compiler.plugin.version>
    16. <spring.version>5.0.2.RELEASEspring.version>
    17. <mybatis.version>3.4.5mybatis.version>
    18. <mysql.version>5.1.44mysql.version>
    19. <pagehelper.version>5.1.2pagehelper.version>
    20. <mybatis.spring.version>1.3.1mybatis.spring.version>
    21. <commons.dbcp2.version>2.1.1commons.dbcp2.version>
    22. <commons.pool2.version>2.4.3commons.pool2.version>
    23. <log4j2.version>2.9.1log4j2.version>
    24. <junit.version>4.12junit.version>
    25. <servlet.version>4.0.0servlet.version>
    26. <lombok.version>1.18.2lombok.version>
    27. <ehcache.version>2.10.0ehcache.version>
    28. <slf4j-api.version>1.7.7slf4j-api.version>
    29. <redis.version>2.9.0redis.version>
    30. <redis.spring.version>1.7.1.RELEASEredis.spring.version>
    31. properties>
    32. <dependencies>
    33. <dependency>
    34. <groupId>org.springframeworkgroupId>
    35. <artifactId>spring-contextartifactId>
    36. <version>${spring.version}version>
    37. dependency>
    38. <dependency>
    39. <groupId>org.springframeworkgroupId>
    40. <artifactId>spring-ormartifactId>
    41. <version>${spring.version}version>
    42. dependency>
    43. <dependency>
    44. <groupId>org.springframeworkgroupId>
    45. <artifactId>spring-txartifactId>
    46. <version>${spring.version}version>
    47. dependency>
    48. <dependency>
    49. <groupId>org.springframeworkgroupId>
    50. <artifactId>spring-aspectsartifactId>
    51. <version>${spring.version}version>
    52. dependency>
    53. <dependency>
    54. <groupId>org.springframeworkgroupId>
    55. <artifactId>spring-webartifactId>
    56. <version>${spring.version}version>
    57. dependency>
    58. <dependency>
    59. <groupId>org.springframeworkgroupId>
    60. <artifactId>spring-testartifactId>
    61. <version>${spring.version}version>
    62. dependency>
    63. <dependency>
    64. <groupId>org.mybatisgroupId>
    65. <artifactId>mybatisartifactId>
    66. <version>${mybatis.version}version>
    67. dependency>
    68. <dependency>
    69. <groupId>mysqlgroupId>
    70. <artifactId>mysql-connector-javaartifactId>
    71. <version>${mysql.version}version>
    72. dependency>
    73. <dependency>
    74. <groupId>com.github.pagehelpergroupId>
    75. <artifactId>pagehelperartifactId>
    76. <version>${pagehelper.version}version>
    77. dependency>
    78. <dependency>
    79. <groupId>org.mybatisgroupId>
    80. <artifactId>mybatis-springartifactId>
    81. <version>${mybatis.spring.version}version>
    82. dependency>
    83. <dependency>
    84. <groupId>org.apache.commonsgroupId>
    85. <artifactId>commons-dbcp2artifactId>
    86. <version>${commons.dbcp2.version}version>
    87. dependency>
    88. <dependency>
    89. <groupId>org.apache.commonsgroupId>
    90. <artifactId>commons-pool2artifactId>
    91. <version>${commons.pool2.version}version>
    92. dependency>
    93. <dependency>
    94. <groupId>org.apache.logging.log4jgroupId>
    95. <artifactId>log4j-coreartifactId>
    96. <version>${log4j2.version}version>
    97. dependency>
    98. <dependency>
    99. <groupId>org.apache.logging.log4jgroupId>
    100. <artifactId>log4j-apiartifactId>
    101. <version>${log4j2.version}version>
    102. dependency>
    103. <dependency>
    104. <groupId>org.apache.logging.log4jgroupId>
    105. <artifactId>log4j-webartifactId>
    106. <version>${log4j2.version}version>
    107. dependency>
    108. <dependency>
    109. <groupId>junitgroupId>
    110. <artifactId>junitartifactId>
    111. <version>${junit.version}version>
    112. <scope>testscope>
    113. dependency>
    114. <dependency>
    115. <groupId>javax.servletgroupId>
    116. <artifactId>javax.servlet-apiartifactId>
    117. <version>${servlet.version}version>
    118. <scope>providedscope>
    119. dependency>
    120. <dependency>
    121. <groupId>org.projectlombokgroupId>
    122. <artifactId>lombokartifactId>
    123. <version>${lombok.version}version>
    124. <scope>providedscope>
    125. dependency>
    126. <dependency>
    127. <groupId>org.springframeworkgroupId>
    128. <artifactId>spring-webmvcartifactId>
    129. <version>${spring.version}version>
    130. dependency>
    131. <dependency>
    132. <groupId>javax.servlet.jspgroupId>
    133. <artifactId>javax.servlet.jsp-apiartifactId>
    134. <version>2.3.3version>
    135. dependency>
    136. <dependency>
    137. <groupId>jstlgroupId>
    138. <artifactId>jstlartifactId>
    139. <version>1.2version>
    140. dependency>
    141. <dependency>
    142. <groupId>taglibsgroupId>
    143. <artifactId>standardartifactId>
    144. <version>1.1.2version>
    145. dependency>
    146. <dependency>
    147. <groupId>commons-fileuploadgroupId>
    148. <artifactId>commons-fileuploadartifactId>
    149. <version>1.3.3version>
    150. dependency>
    151. <dependency>
    152. <groupId>org.hibernategroupId>
    153. <artifactId>hibernate-validatorartifactId>
    154. <version>6.0.7.Finalversion>
    155. dependency>
    156. <dependency>
    157. <groupId>com.fasterxml.jackson.coregroupId>
    158. <artifactId>jackson-databindartifactId>
    159. <version>2.9.3version>
    160. dependency>
    161. <dependency>
    162. <groupId>com.fasterxml.jackson.coregroupId>
    163. <artifactId>jackson-coreartifactId>
    164. <version>2.9.3version>
    165. dependency>
    166. <dependency>
    167. <groupId>com.fasterxml.jackson.coregroupId>
    168. <artifactId>jackson-annotationsartifactId>
    169. <version>2.9.3version>
    170. dependency>
    171. <dependency>
    172. <groupId>org.apache.shirogroupId>
    173. <artifactId>shiro-coreartifactId>
    174. <version>1.3.2version>
    175. dependency>
    176. <dependency>
    177. <groupId>org.apache.shirogroupId>
    178. <artifactId>shiro-webartifactId>
    179. <version>1.3.2version>
    180. dependency>
    181. <dependency>
    182. <groupId>org.apache.shirogroupId>
    183. <artifactId>shiro-springartifactId>
    184. <version>1.3.2version>
    185. dependency>
    186. <dependency>
    187. <groupId>net.sf.ehcachegroupId>
    188. <artifactId>ehcacheartifactId>
    189. <version>${ehcache.version}version>
    190. dependency>
    191. <dependency>
    192. <groupId>org.slf4jgroupId>
    193. <artifactId>slf4j-apiartifactId>
    194. <version>${slf4j-api.version}version>
    195. dependency>
    196. <dependency>
    197. <groupId>org.slf4jgroupId>
    198. <artifactId>jcl-over-slf4jartifactId>
    199. <version>${slf4j-api.version}version>
    200. <scope>runtimescope>
    201. dependency>
    202. <dependency>
    203. <groupId>org.apache.logging.log4jgroupId>
    204. <artifactId>log4j-slf4j-implartifactId>
    205. <version>${log4j2.version}version>
    206. dependency>
    207. <dependency>
    208. <groupId>redis.clientsgroupId>
    209. <artifactId>jedisartifactId>
    210. <version>${redis.version}version>
    211. dependency>
    212. <dependency>
    213. <groupId>org.springframework.datagroupId>
    214. <artifactId>spring-data-redisartifactId>
    215. <version>${redis.spring.version}version>
    216. dependency>
    217. dependencies>
    218. <build>
    219. <finalName>ssm2finalName>
    220. <resources>
    221. <resource>
    222. <directory>src/main/javadirectory>
    223. <includes>
    224. <include>**/*.xmlinclude>
    225. includes>
    226. resource>
    227. <resource>
    228. <directory>src/main/resourcesdirectory>
    229. <includes>
    230. <include>*.propertiesinclude>
    231. <include>*.xmlinclude>
    232. includes>
    233. resource>
    234. resources>
    235. <pluginManagement>
    236. <plugins>
    237. <plugin>
    238. <groupId>org.apache.maven.pluginsgroupId>
    239. <artifactId>maven-compiler-pluginartifactId>
    240. <version>${maven.compiler.plugin.version}version>
    241. <configuration>
    242. <source>${maven.compiler.source}source>
    243. <target>${maven.compiler.target}target>
    244. <encoding>${project.build.sourceEncoding}encoding>
    245. configuration>
    246. plugin>
    247. <plugin>
    248. <groupId>org.mybatis.generatorgroupId>
    249. <artifactId>mybatis-generator-maven-pluginartifactId>
    250. <version>1.3.2version>
    251. <dependencies>
    252. <dependency>
    253. <groupId>mysqlgroupId>
    254. <artifactId>mysql-connector-javaartifactId>
    255. <version>${mysql.version}version>
    256. dependency>
    257. dependencies>
    258. <configuration>
    259. <overwrite>trueoverwrite>
    260. configuration>
    261. plugin>
    262. <plugin>
    263. <artifactId>maven-clean-pluginartifactId>
    264. <version>3.1.0version>
    265. plugin>
    266. <plugin>
    267. <artifactId>maven-resources-pluginartifactId>
    268. <version>3.0.2version>
    269. plugin>
    270. <plugin>
    271. <artifactId>maven-compiler-pluginartifactId>
    272. <version>3.8.0version>
    273. plugin>
    274. <plugin>
    275. <artifactId>maven-surefire-pluginartifactId>
    276. <version>2.22.1version>
    277. plugin>
    278. <plugin>
    279. <artifactId>maven-war-pluginartifactId>
    280. <version>3.2.2version>
    281. plugin>
    282. <plugin>
    283. <artifactId>maven-install-pluginartifactId>
    284. <version>2.5.2version>
    285. plugin>
    286. <plugin>
    287. <artifactId>maven-deploy-pluginartifactId>
    288. <version>2.8.2version>
    289. plugin>
    290. plugins>
    291. pluginManagement>
    292. build>
    293. project>

    1.2 spring-redis.xml配置

    这个配置文件的作用主要用于配置数据源和连接工厂还有配置序列化的用途

    步骤:

    ① 注册redis.properties

    ② 配置数据源[连接]

    ③ 连接工厂

    ④ 配置序列化器

    ⑤ 配置redis的key生成策略

    具体步骤看下代码:

    spring-redis.xml:

    1. "1.0" encoding="UTF-8"?>
    2. "http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xmlns:cache="http://www.springframework.org/schema/cache"
    6. xsi:schemaLocation="http://www.springframework.org/schema/beans
    7. http://www.springframework.org/schema/beans/spring-beans.xsd
    8. http://www.springframework.org/schema/context
    9. http://www.springframework.org/schema/context/spring-context.xsd
    10. http://www.springframework.org/schema/cache
    11. http://www.springframework.org/schema/cache/spring-cache.xsd">
    12. "classpath:redis.properties" />
    13. "poolConfig" class="redis.clients.jedis.JedisPoolConfig">
    14. "maxIdle" value="300"/>
    15. "maxTotal" value="${redis.maxTotal}"/>
    16. "maxWaitMillis" value="${redis.maxWaitMillis}"/>
    17. "minEvictableIdleTimeMillis" value="${redis.minEvictableIdleTimeMillis}"/>
    18. "numTestsPerEvictionRun" value="${redis.numTestsPerEvictionRun}"/>
    19. "timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}"/>
    20. "testOnBorrow" value="${redis.testOnBorrow}"/>
    21. "testWhileIdle" value="${redis.testWhileIdle}"/>
    22. "connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
    23. destroy-method="destroy">
    24. "poolConfig" ref="poolConfig"/>
    25. "hostName" value="${redis.hostName}"/>
    26. "port" value="${redis.port}"/>
    27. "password" value="${redis.password}"/>
    28. "timeout" value="${redis.timeout}"/>
    29. "redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
    30. "connectionFactory" ref="connectionFactory"/>

    redis.properties:

    1. redis.hostName=localhost
    2. redis.port=6379
    3. redis.password=123456
    4. redis.timeout=10000
    5. redis.maxIdle=300
    6. redis.maxTotal=1000
    7. redis.maxWaitMillis=1000
    8. redis.minEvictableIdleTimeMillis=300000
    9. redis.numTestsPerEvictionRun=1024
    10. redis.timeBetweenEvictionRunsMillis=30000
    11. redis.testOnBorrow=true
    12. redis.testWhileIdle=true
    13. redis.expiration=3600

    1.3 spring上下文配置 

    applicationContext.xml

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    5. xmlns:aop="http://www.springframework.org/schema/aop"
    6. 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">
    7. <bean id="propertyConfigurer"
    8. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    9. <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    10. <property name="ignoreResourceNotFound" value="true" />
    11. <property name="locations">
    12. <list>
    13. <value>classpath:jdbc.propertiesvalue>
    14. <value>classpath:redis.propertiesvalue>
    15. list>
    16. property>
    17. bean>
    18. <import resource="applicationContext-mybatis.xml">import>
    19. <import resource="spring-redis.xml">import>
    20. <import resource="applicationContext-shiro.xml">import>
    21. beans>

    二,Redis注解式

    ① @Cacheable (可读取与写入)

       @Cacheable是Spring框架提供的一个缓存注解,用于标记方法的返回结果可以被缓存起来,以提高系统的性能。

    如下:

    测试代码:

    classBiz:

    1. @Cacheable("clz")
    2. Clazz selectByPrimaryKey(Integer cid);

    测试类text:

    1. @Test
    2. public void test1(){
    3. System.out.println(clazzBiz.selectByPrimaryKey(1));
    4. System.out.println(clazzBiz.selectByPrimaryKey(1));
    5. }

     运行结果,只会走一次数据库,之后都是走redis缓存,减少了数据库压力

    数据库:

    value:缓存位置的一段名称,不能为空
    key:缓存的key,默认为空,表示使用方法的参数类型及参数值作为key,支持SpEL
    condition:触发条件,满足条件就加入缓存,默认为空,表示全部都加入缓存,支持SpEL 

    ② CachePut(只读数据)

       @CachePut注解用于更新缓存中的内容。 当使用注解标记的方法被调用时,Spring会更新缓存中的值,而不管缓存中是否已经存在相应的键值对,如下

    classBiz:

    1. @CachePut(value = "xx",key = "'cid:'+#cid")
    2. Clazz selectByPrimaryKey(Integer cid);

    测试类text:

    value:缓存的名称,在 spring 配置文件中定义,必须指定至少一个
    key:缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合
    condition:缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存

    @CacheEvict(清除或缓存)

    @CacheEvict是Spring框架提供的一个缓存注解,用于标记方法执行后清除缓存中的数据

    1. @CacheEvict(value = "xx",key = "'cid:'+#cid",allEntries = true)
    2. int deleteByPrimaryKey(Integer cid);

    value:缓存位置的一段名称,不能为空
    key:缓存的key,默认为空,表示使用方法的参数类型及参数值作为key,支持SpEL
    condition:触发条件,满足条件就加入缓存,默认为空,表示全部都加入缓存,支持SpEL
    allEntries:true表示清除value中的全部缓存,默认为false

    三,Redis缓存的击穿、穿透、雪崩 

    缓存中常见的三种现象:击穿、穿透、雪崩

    ① 击穿

        当缓存中不存在某个key的数据,而有大量并发请求访问这个key时,这些请求会直接穿过缓存,去访问数据库,导致数据库压力过大,甚至宕机

    解决方法:

    使用互斥锁或分布式锁,保证只有一个线程去访问数据库,其他线程等待结果即可

    ② 穿透

        如果在请求数据时,在缓存层和数据库层都没有找到符合条件的数据,也就是说,在缓存层和数据库层都没有命中数据,那么这种情况就叫作缓存穿透。

    解决方法:

    在缓存中预先设置这个key对应的空值或默认值,避免大量请求直接访问数据库

    图例:

    ③ 雪崩

      当缓存中的大量数据同时失效,而且这些数据被大量请求访问时,这些请求会直接访问数据库,导致数据库压力过大,甚至宕机

    解决方法:

    - 缓存数据的失效时间设置随机,避免大量数据同时失效。
    - 使用多级缓存架构,避免单点故障。
    - 使用熔断机制,当缓存失效时,暂时屏蔽对数据库的访问,避免压力过大

    图例:

  • 相关阅读:
    [附源码]计算机毕业设计springboot-大学生健康档案管理
    5分钟就能实现的API监控,有什么理由不做呢?
    ClickHouse副本节点数据损坏恢复
    04.PD与数据库关系模型简介
    tomcat默认最大线程数、等待队列长度、连接超时时间
    javaweb——socket
    “12306”的架构到底有多牛逼?
    postgreSql执行状态与锁冲突问题排查
    TCP三次握手、四次挥手详解
    子2023
  • 原文地址:https://blog.csdn.net/m0_73192864/article/details/134260926