Redis与Spring集成

一.启动Redis服务:

打开VM连接Xhsell6启动安装好的Redis----Redis部署在Linux
在这里插入图片描述
Redis数据库测试连接是否成功:
在这里插入图片描述

二.将Redis配置到Spring容器中:

在这里插入图片描述

spring-redis.xml:

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:cache="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">

    <!-- redis的相关配置已经在applicationContext.xml导入了,因为spring只允许有一个context:property-placeholder -->
    <!-- 所以下面的配置会注释掉了 -->
    <!-- 1. 引入properties配置文件 -->
    <!--<context:property-placeholder ignore-unresolvable="true" location="classpath:redis.properties" />-->

    <!-- 2. 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}"/>
        <!--逐出连接的最小空闲时间 默认1800000毫秒(30分钟)-->
        <property name="minEvictableIdleTimeMillis" value="${redis.minEvictableIdleTimeMillis}"/>
        <!--每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3-->
        <property name="numTestsPerEvictionRun" value="${redis.numTestsPerEvictionRun}"/>
        <!--逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1-->
        <property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}"/>
        <!--是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个-->
        <property name="testOnBorrow" value="${redis.testOnBorrow}"/>
        <!--在空闲时检查有效性, 默认false  -->
        <property name="testWhileIdle" value="${redis.testWhileIdle}"/>
    </bean>

    <!-- 3. redis连接工厂 -->
    <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
          destroy-method="destroy">
        <property name="poolConfig" ref="poolConfig"/>
        <!--IP地址 -->
        <property name="hostName" value="${redis.hostName}"/>
        <!--端口号  -->
        <property name="port" value="${redis.port}"/>
        <!--如果Redis设置有密码  -->
        <property name="password" value="${redis.password}"/>
        <!--客户端超时时间单位是毫秒  -->
        <property name="timeout" value="${redis.timeout}"/>
    </bean>

    <!-- 4. redis操作模板,使用该对象可以操作redis  -->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="connectionFactory"/>
        <!--如果不配置Serializer,那么存储的时候缺省使用String,如果用User类型存储,那么会提示错误User can't cast to String!!  -->
        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
        </property>
        <property name="hashKeySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <property name="hashValueSerializer">
            <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
        </property>
        <!--开启事务  -->
        <property name="enableTransactionSupport" value="false"/>
    </bean>

    <!--静态注入中间类,解决RedisCache中RedisTemplate的静态注入,从而使MyBatis实现第三方缓存-->
    <!--<bean class="com.zking.zf.redis.RedisCacheTransfer">-->
    <!--<property name="redisTemplate" ref="redisTemplate"/>-->
    <!--</bean>-->

    <!--自定义redis工具类,在需要缓存的地方注入此类,建议使用注解配置  -->
    <!--<bean id="redisUtil" class="com.zking.zf.redis.RedisUtil">-->
    <!--<property name="redisTemplate" ref="redisTemplate"/>-->
    <!--</bean>-->

    <!--配置缓存管理器-->
    <bean id="redisCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
        <constructor-arg name="redisOperations" ref="redisTemplate" />
        <!--redis缓存数据过期时间单位秒-->
        <property name="defaultExpiration" value="${redis.expiration}" />

        <property name="usePrefix" value="true"/>

        <property name="cachePrefix">
            <bean class="org.springframework.data.redis.cache.DefaultRedisCachePrefix">
                <constructor-arg index="0" value="-cache-"/>
            </bean>
        </property>

    </bean>

    <!--启用缓存注解功能-->
    <cache:annotation-driven cache-manager="redisCacheManager" key-generator="cacheKeyGenerator"/>

    <!--配置自定义Key生成器CacheKeyGenerator-->
    <bean id="cacheKeyGenerator" class="com.zking.ssm.util.CacheKeyGenerator"></bean>
</beans>

在这里插入图片描述
会出现报错需要配置自定义Key生成器CacheKeyGenerator

CacheKeyGenerator:

package com.zking.ssm.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.util.ClassUtils;

import java.lang.reflect.Array;
import java.lang.reflect.Method;

@Slf4j
public class CacheKeyGenerator implements KeyGenerator {
    // custom cache key
    public static final int NO_PARAM_KEY = 0;
    public static final int NULL_PARAM_KEY = 53;

    @Override
    public Object generate(Object target, Method method, Object... params) {
        StringBuilder key = new StringBuilder();
        key.append(target.getClass().getSimpleName()).append(".").append(method.getName()).append(":");
        if (params.length == 0) {
            key.append(NO_PARAM_KEY);
        } else {
            int count = 0;
            for (Object param : params) {
                if (0 != count) {//参数之间用,进行分隔
                    key.append(',');
                }
                if (param == null) {
                    key.append(NULL_PARAM_KEY);
                } else if (ClassUtils.isPrimitiveArray(param.getClass())) {
                    int length = Array.getLength(param);
                    for (int i = 0; i < length; i++) {
                        key.append(Array.get(param, i));
                        key.append(',');
                    }
                } else if (ClassUtils.isPrimitiveOrWrapper(param.getClass()) || param instanceof String) {
                    key.append(param);
                } else {//Java一定要重写hashCode和eqauls
                    key.append(param.hashCode());
                }
                count++;
            }
        }

        String finalKey = key.toString();
        log.debug("using cache key={}", finalKey);
        return finalKey;
    }
}

在这里插入图片描述
redis.properties:

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:cache="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">

    <!-- redis的相关配置已经在applicationContext.xml导入了,因为spring只允许有一个context:property-placeholder -->
    <!-- 所以下面的配置会注释掉了 -->
    <!-- 1. 引入properties配置文件 -->
    <!--<context:property-placeholder ignore-unresolvable="true" location="classpath:redis.properties" />-->

    <!-- 2. 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}"/>
        <!--逐出连接的最小空闲时间 默认1800000毫秒(30分钟)-->
        <property name="minEvictableIdleTimeMillis" value="${redis.minEvictableIdleTimeMillis}"/>
        <!--每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3-->
        <property name="numTestsPerEvictionRun" value="${redis.numTestsPerEvictionRun}"/>
        <!--逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1-->
        <property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}"/>
        <!--是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个-->
        <property name="testOnBorrow" value="${redis.testOnBorrow}"/>
        <!--在空闲时检查有效性, 默认false  -->
        <property name="testWhileIdle" value="${redis.testWhileIdle}"/>
    </bean>

    <!-- 3. redis连接工厂 -->
    <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
          destroy-method="destroy">
        <property name="poolConfig" ref="poolConfig"/>
        <!--IP地址 -->
        <property name="hostName" value="${redis.hostName}"/>
        <!--端口号  -->
        <property name="port" value="${redis.port}"/>
        <!--如果Redis设置有密码  -->
        <property name="password" value="${redis.password}"/>
        <!--客户端超时时间单位是毫秒  -->
        <property name="timeout" value="${redis.timeout}"/>
    </bean>

    <!-- 4. redis操作模板,使用该对象可以操作redis  -->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="connectionFactory"/>
        <!--如果不配置Serializer,那么存储的时候缺省使用String,如果用User类型存储,那么会提示错误User can't cast to String!!  -->
        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
        </property>
        <property name="hashKeySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <property name="hashValueSerializer">
            <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
        </property>
        <!--开启事务  -->
        <property name="enableTransactionSupport" value="false"/>
    </bean>

    <!--静态注入中间类,解决RedisCache中RedisTemplate的静态注入,从而使MyBatis实现第三方缓存-->
    <!--<bean class="com.zking.zf.redis.RedisCacheTransfer">-->
    <!--<property name="redisTemplate" ref="redisTemplate"/>-->
    <!--</bean>-->

    <!--自定义redis工具类,在需要缓存的地方注入此类,建议使用注解配置  -->
    <!--<bean id="redisUtil" class="com.zking.zf.redis.RedisUtil">-->
    <!--<property name="redisTemplate" ref="redisTemplate"/>-->
    <!--</bean>-->

    <!--配置缓存管理器-->
    <bean id="redisCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
        <constructor-arg name="redisOperations" ref="redisTemplate" />
        <!--redis缓存数据过期时间单位秒-->
        <property name="defaultExpiration" value="${redis.expiration}" />

        <property name="usePrefix" value="true"/>

        <property name="cachePrefix">
            <bean class="org.springframework.data.redis.cache.DefaultRedisCachePrefix">
                <constructor-arg index="0" value="-cache-"/>
            </bean>
        </property>

    </bean>

    <!--启用缓存注解功能-->
    <cache:annotation-driven cache-manager="redisCacheManager" key-generator="cacheKeyGenerator"/>

    <!--配置自定义Key生成器CacheKeyGenerator-->
    <bean id="cacheKeyGenerator" class="com.zking.ssm.util.CacheKeyGenerator"></bean>
</beans>

在这里插入图片描述

配置完redis.properties需要添加jackson相关依赖的支持:
添加到Pom.xml文件中

  <!--8)jackson相关依赖-->
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>${jackson.version}</version>
  <exclusions>
    <exclusion>
      <artifactId>jackson-annotations</artifactId>
      <groupId>com.fasterxml.jackson.core</groupId>
    </exclusion>
  </exclusions>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-core</artifactId>
  <version>${jackson.version}</version>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-annotations</artifactId>
  <version>${jackson.version}</version>
</dependency>

@RunWith:运行器,指向Junit4来运行;
@ContextConfiguration Spring整合JUnit4测试时,使用注解引入配置文件;
BaseTestCase:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:spring.xml"})
public class BaseTestCase {

}

在这里插入图片描述

测试是否与Spring与Redis集合成功:
在这里插入图片描述

测试集合结果:
在这里插入图片描述在这里插入图片描述

三.spring注解式缓存使用步骤

  1. 前提:spring+redis集成已完成

  2. 配置缓存管理器

<bean id="redisCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
   <constructor-arg name="redisOperations" ref="redisTemplate" />
   <!--redis缓存数据过期时间单位秒-->
   <property name="defaultExpiration" value="${redis.expiration}" />
   
   <property name="usePrefix" value="true"/>

    <property name="cachePrefix">
        <bean class="org.springframework.data.redis.cache.DefaultRedisCachePrefix">
            <constructor-arg index="0" value="-cache-"/>
        </bean>
    </property>
 </bean>

在这里插入图片描述

  1. 配置自定义Key生成器CacheKeyGenerator
    在这里插入图片描述
    缓存的Java对象一定要重写hashCode和eqauls---->快捷键生成Ait+Insert
    在这里插入图片描述
    在这里插入图片描述

  2. 启用缓存注解功能

<cache:annotation-driven cache-manager="redisCacheManager"/>

在这里插入图片描述

  1. 在需要的地方进行注解缓存

四.开始缓存注解

2.1 @CacheConfig

1)@CacheConfig 是一个类级别的注解,允许共享缓存的名称、KeyGenerator、CacheManager 和CacheResolver,CacheNames。
该操作会被覆盖。

@Transactional
@CacheConfig(cacheNames = "bookCheable")
public interface IBookService {

   @Transactional(readOnly = true)
   @Cacheable(value = "bookCheable",key = "#bookId+''")
   Book selectByPrimaryKey(Integer bookId)....};

注意:不知道你们注意到一个问题没有,就是所有的@Cacheable()里面都有一个name=“xxx”的属性,这显然如果方法多了,写起来也是挺累的,如果可以一次性声明完 那就省事了,
所以,有了@CacheConfig这个配置,@CacheConfig is a class-level annotation that allows to share the cache names,不过不用担心,如果你在你的方法写别的名字,那么依然以方法的名字为准。

2.2 @Cacheable

1)配置在方法或类上,作用:本方法执行后,先去缓存看有没有数据,如果没有,从数据库中查找出来,给缓存中存一份,返回结果,下次本方法执行,在缓存未过期情况下,先在缓存中查找,有的话直接返回,没有的话从数据库查找

@Transactional
@CacheConfig(cacheNames = "bookCheable")
public interface IBookService {

    @Transactional(readOnly = true)
    @Cacheable(value = "bookCheable",key = "#bookId+''")
    Book selectByPrimaryKey(Integer bookId).....};
public class IBookServiceTest extends BaseTestCase {

    @Autowired
    private IBookService bookService;
    private Book book;
    
   @Before
    public void setUp() throws Exception {
        book=new Book();
    }
    
    @Test
    public void selectByPrimaryKey() {
            book.setBookId(9);
        System.out.println("yyyyyyyyyyyyyyy");
        Book b = bookService.selectByPrimaryKey(this.book.getBookId());
        System.out.println(b);
        System.out.println("xxxxxxxxxxxxxx");
        Book b2 = bookService.selectByPrimaryKey(this.book.getBookId());
        System.out.println(b2);
    }

	.....}

缓存就这样出现了:
在这里插入图片描述
在这里插入图片描述

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

注1:condition是在方法执行前评估, unless是在方法执行后评估.

2.3 @CachePut

1)类似于更新操作,即每次不管缓存中有没有结果,都从数据库查找结果,并将结果更新到缓存,并返回结果

@Transactional
public interface IBookService {
   
    @CachePut(value = "bookupdat",key = "#record.getBookId()+''")
    Book update(Book record)
    			.....}
public class IBookServiceTest extends BaseTestCase {

    @Autowired
    private IBookService bookService;
    private Book book;

    @Before
    public void setUp() throws Exception {
        book=new Book();
    }
    
    @Test
    public void update() {
        book.setBookId(9);
        book.setBookName("牛棚旅行记");
        book.setPrice(199l);
        bookService.update(book);
    }
					......}

在这里插入图片描述
在这里插入图片描述

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

1)用来清除用在本方法或者类上的缓存数据(用在哪里清除哪里)

@Transactional
	public interface IBookService {
	    @CacheEvict(value = "bookCheable",allEntries = true)
	    int del(Integer bookId);
	    ......}
public class IBookServiceTest extends BaseTestCase {

    @Autowired
    private IBookService bookService;
    private Book book;

    @Before
    public void setUp() throws Exception {
        book=new Book();
    }
    @Test
    public void del() {
        book.setBookId(6);
        bookService.del(book.getBookId());
    }

在这里插入图片描述
会根据bookCheable来删除缓存
在这里插入图片描述

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

五.spring注解式缓存中的巨坑

  1. 自定义Key生成器
    @Cacheable(value=“gomeo2oCache”, keyGenerator = “keyGenerator”)
    public ResultDTO method(User user);

    spring注解式缓存中的巨坑~~~~~~~
    没有指定key,默认情况下spirng会使用SimpleKeyGenerator生成key,
    而Spring默认的SimpleKeyGenerator是不会将函数名组合进key中的,举个例子:
    @Component
    public class CacheTestImpl implements CacheTest {
    @Cacheable(“databaseCache”)
    public Long test1()//无参是不会有返回值的默认都是1
    { return 1L; }

    @Cacheable(“databaseCache”)
    public Long test2()
    { return 2L; }

    @Cacheable(“databaseCache”)
    public Long test3()
    { return 3L; }

    @Cacheable(“databaseCache”)
    public String test4()
    { return “4”; }//注意返回的是字符串“4”
    }
    我们期望的输出是:
    1
    2
    3
    4
    而实际上的输出是:
    1
    1
    1
    ClassCastException: java.lang.Long cannot be cast to java.lang.String

    此外,原子类型的数组,直接作为key使用也是不会生效的,为了解决上述2个问题,只能通过自定义KeyGenerator解决

    自定义Key生成器CacheKeyGenerator:源码见资料“CacheKeyGenerator.java”,另外此类使用非加密哈希算法MurmurHash
    (源码46行: Hashing.murmur3_128().hashString),需要引入google guava项目,其pom如下:

<dependency>
	        <groupId>com.google.guava</groupId>
	        <artifactId>guava</artifactId>
	        <version>27.0.1-jre</version>
	      </dependency>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值