• SSM之spring注解式缓存redis


      🏅我是默,一个在CSDN分享笔记的博主。📚📚 

    🌟在这里,我要推荐给大家我的专栏《Linux》。🎯🎯

    🚀无论你是编程小白,还是有一定基础的程序员,这个专栏都能满足你的需求。我会用最简单易懂的语言,带你走进代码的世界,让你从零开始,一步步成为编程大师。🚀🏆

    🌈让我们在代码的世界里畅游吧!🌈

    🎁如果感觉还不错的话请记得给我点赞哦!🎁🎁

    💖期待你的加入,一起学习,一起进步💖💖


    目录

    ​编辑

    一.spring注解redis

    1.导入依赖

    2.具体操作

    配置工厂(spring-redis.xml)

    配置缓存管理器

    二.redsi三个注解的使用

    @Cacheable(可以读取数据,也可以写数据)

    @CachePut(只读数据)

    三.redis击穿穿透雪崩

    1.定义

    Redis击穿

    Redis穿透

    Redis雪崩


    一.spring注解redis

    1.导入依赖

    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>

    2.具体操作

    配置工厂(spring-redis.xml)

    1. <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
    2. destroy-method="destroy">
    3. <property name="poolConfig" ref="poolConfig"/>
    4. <property name="hostName" value="${redis.hostName}"/>
    5. <property name="port" value="${redis.port}"/>
    6. <property name="password" value="${redis.password}"/>
    7. <property name="timeout" value="${redis.timeout}"/>
    8. bean>

      redis操作模板,使用该对象可以操作redis

    1. <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
    2. <property name="connectionFactory" ref="connectionFactory"/>
    3. <property name="keySerializer">
    4. <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
    5. property>
    6. <property name="valueSerializer">
    7. <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
    8. property>
    9. <property name="hashKeySerializer">
    10. <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
    11. property>
    12. <property name="hashValueSerializer">
    13. <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
    14. property>
    15. <property name="enableTransactionSupport" value="true"/>
    16. bean>

    配置缓存管理器

    1. <bean id="redisCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
    2. <constructor-arg name="redisOperations" ref="redisTemplate"/>
    3. <property name="defaultExpiration" value="${redis.expiration}"/>
    4. <property name="usePrefix" value="true"/>
    5. <property name="cachePrefix">
    6. <bean class="org.springframework.data.redis.cache.DefaultRedisCachePrefix">
    7. <constructor-arg index="0" value="-cache-"/>
    8. bean>
    9. property>
    10. bean>
    1. <bean id="cacheKeyGenerator" class="com.zking.ssm.redis.CacheKeyGenerator">bean>
    2. <cache:annotation-driven cache-manager="redisCacheManager" key-generator="cacheKeyGenerator"/>
    3. beans>

    二.redsi三个注解的使用

    @Cacheable(可以读取数据,也可以写数据)

    @Cacheable注解用于标记一个方法的结果可以被缓存。 当调用该方法时,Spring首先检查缓存是否已经存在结果。 如果存在,将直接从缓存中返回结果,而不执行方法。 如果结果不在缓存中,则执行方法并将结果缓存起来,以便后续调用时使用。

    java复制代码

    @Cacheable(value = "books", key = "#isbn") public Book getByIsbn(String isbn) { // 从数据库中获取书籍信息的逻辑 }

    @CachePut(只读数据)

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

    java复制代码

    @CachePut(value = "books", key = "#book.isbn") public Book updateBook(Book book) { // 更新数据库中书籍信息的逻辑 return book; }

    @CacheEvict注解用于从缓存中删除数据。 可以通过指定属性来确定要清除的缓存,通过属性来指定要删除的项。valuekey

    java复制代码

    @CacheEvict(value = "books", key = "#isbn") public void deleteByIsbn(String isbn) { // 从数据库中删除书籍信息的逻辑 }

    这些注解在执行耗时操作(如数据库查询或API调用)的应用程序中非常有用。 通过缓存这些操作的结果,可以加快后续调用的速度,减少应用程序的响应时间。

    三.redis击穿穿透雪崩

    1.定义

    Redis击穿

    Redis击穿是指一个缓存key非常热门,在某一个时间点过期的时候,恰好又有大量的并发请求过来访问这个key,导致所有请求都打到了数据库上,从而导致数据库压力过大,甚至宕机。 为了避免这种情况,可以使用互斥锁(mutex)或者分布式锁来解决。

    Redis穿透

    Redis穿透是指一个请求查询一个不存在的缓存key,这个请求就会直接打到数据库上,如果请求量很大,数据库就会承受巨大的压力。 为了避免这种情况,可以使用布隆过滤器来处理缓存穿透的问题,或者在缓存层做参数校验和异常处理。

    Redis雪崩

    Redis雪崩是指在某一个时间段,缓存集中过期失效,导致所有请求都落到数据库上,造成数据库压力急剧增大,甚至宕机的现象。 为了避免这种情况,可以采用多级缓存架构,设置不同的超时时间,避免缓存同时失效。 同时,也可以设置热点数据永久不过期,避免热点数据集中过期。 另外,还可以使用限流和降级等手段来应对高并发情况。

  • 相关阅读:
    HTML拆分与共享方式——多HTML组合技术
    蓝桥杯[OJ 3791]—珠宝的最大交替和—CPP-贪心
    【学习笔记】ARC147/ARC141/ARC145
    【C语言】解决 “address of stack memory associated with local variable ‘num‘ returned”
    【每日十分钟前端】基础篇20,HTTP与HTTPS、文字小于12px、执行上下文和执行栈
    13009.pycharm搭建python开发环境
    有一个List<Date> ,现在有一个Date 时间,找到这个list 里面和时间最近的一个,进行返回
    优雅的 Controller 层代码
    05-在Idea中编写Servlet程序
    数据结构与算法----详解二叉树的遍历(迭代、递归)
  • 原文地址:https://blog.csdn.net/lz17267861157/article/details/134252471