【SSM】整合Redis

前言

     服务端缓存的意义大多数在于减轻数据库压力,提供响应速度,而缺点也是显而易见的,会带来缓存与数据库一致性问题。当然,Redis还可以作为分布式锁。

Redis

    想在项目中使用Redis需要做的事情不多,改动图中几个文件

    

   

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    DeptDOMapper deptDOMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<DeptInfo> listDeptInfo(String name) {

        List<DeptInfo> object = (List<DeptInfo>) redisTemplate.opsForValue().get(name);
        if (object != null) {
            return object;
        }else
        {
            List<DeptDO> deptDOList = deptDOMapper.listDept(name);
            List<DeptInfo> deptInfos = new ArrayList<>();
            deptDOList.stream().forEach(x -> {
                DeptInfo info = new DeptInfo();
                info.setName(x.getName());
                info.setId(x.getId());
                deptInfos.add(info);
            });
            redisTemplate.opsForValue().set(name,deptInfos);
            return deptInfos;
        }
    }
}
DeptServiceImpl.java
redis.host=127.0.0.1
redis.port=6379
redis.maxIdle=400
redis.maxTotal=6000
redis.maxWaitMillis=1000
redis.blockWhenExhausted=true
redis.testOnBorrow=true
redis.timeout=10000
redis.properties

applicationContext.xml导入配置

   <import resource="spring-redis.xml"></import>
<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.3.xsd">

    <!-- 加载配置文件 -->
    <context:property-placeholder location="classpath:redis.properties" />
    <!-- redis数据源 -->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!-- 最大空闲数 -->
        <property name="maxIdle" value="${redis.maxIdle}" />
        <!-- 最大空连接数 -->
        <property name="maxTotal" value="${redis.maxTotal}" />
        <!-- 最大等待时间 -->
        <property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
        <!-- 连接超时时是否阻塞,false时报异常,ture阻塞直到超时, 默认true -->
        <property name="blockWhenExhausted" value="${redis.blockWhenExhausted}" />
        <!-- 返回连接时,检测连接是否成功 -->
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
    </bean>

    <!-- Spring-redis连接池管理工厂 -->
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <!-- IP地址 -->
        <property name="hostName" value="${redis.host}" />
        <!-- 端口号 -->
        <property name="port" value="${redis.port}" />
        <!-- 超时时间 默认2000-->
        <property name="timeout" value="${redis.timeout}" />
        <!-- 连接池配置引用 -->
        <property name="poolConfig" ref="poolConfig" />
        <!-- usePool:是否使用连接池 -->
        <property name="usePool" value="true"/>
    </bean>

    <!-- redis template definition -->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory" />
        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
        </property>
        <property name="hashKeySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="hashValueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
        </property>
        <!--开启事务  -->
        <property name="enableTransactionSupport" value="true"></property>
    </bean>


</beans>
spring-redis.xml

pom.xml引入jar包,jedis包的版本要注意一下,过高可能不兼容,启动时抛错

        <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>2.1.6.RELEASE</version>
        </dependency>

缓存几个问题

1.缓存雪崩

 加上一个随机过期时间

2.缓存穿透

 布隆过滤器

 缓存空数据,设置短过期时间

小结

       缓存这方面的知识点非常多,这里只是做一个接入的记录。

原文地址:https://www.cnblogs.com/caizl/p/10669490.html