• SSM之spring注解式缓存redis->redis整合,redis的注解式开发及应用场景,redis的击穿穿透雪崩


    • redis整合
    • redis的注解式开发及应用场景
    • redis的击穿穿透雪崩

    1.redis整合

    mysql整合

            pom配置;

            String-fmybatis.xml --> mybatis.cfg.xml:

                     包扫描;

                     注册了一个jdbc.properties(url/password/username/...);

                     配置数据源(数据库连接池);

                     配置sqlsession,配置会话;

                     配置事务...;

            StringContext.xml中添加spring-mybatis.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" 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. package="com.zlj.ssm"/>
    8. "classpath:jdbc.properties"/>
    9. "dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
    10. destroy-method="close">
    11. "driverClassName" value="${jdbc.driver}"/>
    12. "url" value="${jdbc.url}"/>
    13. "username" value="${jdbc.username}"/>
    14. "password" value="${jdbc.password}"/>
    15. "initialSize" value="10"/>
    16. "maxTotal" value="100"/>
    17. "maxIdle" value="50"/>
    18. "minIdle" value="10"/>
    19. "maxWaitMillis" value="-1"/>
    20. "sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    21. "dataSource" ref="dataSource"/>
    22. "mapperLocations" value="classpath*:com/zlj/ssm/**/mapper/*.xml"/>
    23. "typeAliasesPackage" value="com/zlj/ssm/**/model"/>
    24. "plugins">
    25. "com.github.pagehelper.PageInterceptor">
    26. "properties">
    27. helperDialect=mysql
    28. "/>
    29. " value="sqlSessionFactory"/>
    30. " class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    31. " ref="dataSource" />
    32. " />

    redis整合

            pom配置;

            Spring-redis.xml

                    注册了一个redis.properties.xml;

                    配置注册源;

                    连接工厂;

                    配置序列化器;

                    配置redis的key生成策略;

            StringContext.xml中添加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. "poolConfig" class="redis.clients.jedis.JedisPoolConfig">
    13. "maxIdle" value="${redis.maxIdle}"/>
    14. "maxTotal" value="${redis.maxTotal}"/>
    15. "maxWaitMillis" value="${redis.maxWaitMillis}"/>
    16. "minEvictableIdleTimeMillis" value="${redis.minEvictableIdleTimeMillis}"/>
    17. "numTestsPerEvictionRun" value="${redis.numTestsPerEvictionRun}"/>
    18. "timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}"/>
    19. "testOnBorrow" value="${redis.testOnBorrow}"/>
    20. "testWhileIdle" value="${redis.testWhileIdle}"/>
    21. "connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
    22. destroy-method="destroy">
    23. "poolConfig" ref="poolConfig"/>
    24. "hostName" value="${redis.hostName}"/>
    25. "port" value="${redis.port}"/>
    26. "password" value="${redis.password}"/>
    27. "timeout" value="${redis.timeout}"/>
    28. "redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
    29. "connectionFactory" ref="connectionFactory"/>

    注解1:当spring-content.xml中需要注解多个.propertise结尾的配置文件,那么不能在spring-*.xml添加注册(在applicationContext-mybatis.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" 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. "propertyConfigurer"
    8. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    9. "systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    10. "ignoreResourceNotFound" value="true" />
    11. "locations">
    12. classpath:jdbc.properties
    13. classpath:redis.properties
    14. <import resource="applicationContext-mybatis.xml">import>
    15. <import resource="spring-redis.xml">import>
    16. <import resource="applicationContext-shiro.xml">import>

    注解2:resources的配置必须要涵盖读取.propertis结尾的文件

    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" 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. "propertyConfigurer"
    8. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    9. "systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    10. "ignoreResourceNotFound" value="true" />
    11. "locations">
    12. classpath:jdbc.properties
    13. classpath:redis.properties
    14. <import resource="applicationContext-mybatis.xml">import>
    15. <import resource="spring-redis.xml">import>
    16. <import resource="applicationContext-shiro.xml">import>

    注解3:redistemplate的使用,可以参照jdbcTemplate,amqptemplate,rabbitMQtemplate...

    2.redis的注解式开发及应用场景

    1. package com.zlj.ssm.biz;
    2. import com.zlj.ssm.model.Clazz;
    3. import com.zlj.ssm.util.PageBean;
    4. import org.springframework.cache.annotation.CacheEvict;
    5. import org.springframework.cache.annotation.CachePut;
    6. import java.util.List;
    7. import java.util.Map;
    8. public interface ClazzBiz {
    9. @CacheEvict(value = "xx",key = "'cid:'+#cid",allEntries = true)
    10. int deleteByPrimaryKey(Integer cid);
    11. int insert(Clazz record);
    12. int insertSelective(Clazz record);
    13. // xx=cache-cid:1
    14. // key的作用改变原有的key生成规则
    15. // @Cacheable(value = "xx",key = "'cid:'+#cid",condition = "#cid > 6")
    16. @CachePut(value = "xx",key = "'cid:'+#cid",condition = "#cid > 6")
    17. Clazz selectByPrimaryKey(Integer cid);
    18. int updateByPrimaryKeySelective(Clazz record);
    19. int updateByPrimaryKey(Clazz record);
    20. List listPager(Clazz clazz, PageBean pageBean);
    21. List listMapPager(Clazz clazz, PageBean pageBean);
    22. }

    cacheable会在redis中存储数据,同时也会读取数据

    cacheput只会在redis中写数据,不会读数据

    cacgeEict强行清除缓存(问题:redis与mybatis的性能同步问题)

    cacgeEict(value = "xx",key = "'cid:'+#cid",allEntries = true);

    3.redis的击穿穿透雪崩

    缓存击穿:

    redis中一个热点key刚好过期(大量用户访问该热点key,但是热点key刚好过期)

    缓存击穿解决方案:
    进行预先的热门词汇的设置,进行key时长的调整;
    实时调整,监控哪些数据是热门数据,实时的调整key的过期时长;
    使用锁机制(只有一个线程可以进行热点数据的重构);

    缓存穿透:

    大量请求根本不存在的key

    缓存穿透解决方案:
    对空值进行缓存;
    设置白名单;
    使用布隆过滤器;
    网警;

    缓存雪崩:

    redis中大量key集体同一时间过期

    缓存雪崩解决方案:
    进行预先的热门词汇的设置,进行key时长的调整;
    实时调整,监控哪些数据是热门数据,实时的调整key的过期时长;
    使用锁机制;

  • 相关阅读:
    【剑指 Offer 54. 二叉搜索树的第k大节点】
    完美解决 RabbitMQ可视化界面Overview不显示折线图和队列不显示Messages
    代码随想录算法训练营第十四天|
    数据结构--二叉堆与优先队列
    自动化测试-怎么存储测试用例
    STM32MP157A驱动开发 | 04 - Linux DRM显示驱动框架
    Java递归
    (附源码)ssm财务管理系统 毕业设计 282251
    HTML+CSS
    C++算法学习心得八.动态规划算法(6)
  • 原文地址:https://blog.csdn.net/weixin_73471776/article/details/134256943