Spring整合Redis实现查询缓存机制

Pom.xml

<!-- redis start -->
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>
<!-- spring integrated with redis -->
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>1.8.11.RELEASE</version>
</dependency>
<!-- redis end -->

spring-redis.xml

配置Spring整合Redis相关的类RedisTemplateRedisCacheManager

<?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:cache="http://www.springframework.org/schema/cache"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"
    default-lazy-init="true">

    <!-- 启动注解,如@cacheable @cachePut等 -->
    <cache:annotation-driven/>

    <!-- redis config start -->
    <!-- 配置JedisPoolConfig实例 -->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${redis.maxIdle}" />
        <property name="maxTotal" value="${redis.maxActive}" />
        <property name="maxWaitMillis" value="${redis.maxWait}" />
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
    </bean>

    <!-- 配置JedisConnectionFactory -->
    <bean id="jedisConnectionFactory"
        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="${redis.host}" />
        <property name="port" value="${redis.port}" />
        <!-- <property name="password" value="${redis.pass}" /> -->
        <property name="database" value="${redis.dbIndex}" />
        <property name="poolConfig" ref="poolConfig" />
    </bean>

    <!-- 配置RedisTemplate -->
    <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="hashKeySerializer">  
           <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />  
       </property>  
       <property name="defaultSerializer">  
           <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"></bean>
       </property>
    </bean>

    <!-- 配置RedisCacheManager -->
    <bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
        <constructor-arg name="redisOperations" ref="redisTemplate" />
        <property name="defaultExpiration" value="${redis.expiration}" />
    </bean>

</beans>

RedisCacheConfig

配置自定义的KeyGenerator类。以下是KeyGenerator的通用写法

import java.lang.reflect.Method;

import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableCaching
public class RedisCacheConfig extends CachingConfigurerSupport {

    @Bean
    public KeyGenerator keyGenerator() {
        return new KeyGenerator() {
            public Object generate(Object target, Method method, Object... objects) {
                StringBuilder sb = new StringBuilder();
                sb.append(target.getClass().getName());
                sb.append(method.getName());
                for (Object obj : objects) {
                    sb.append(obj.toString());
                }
                return sb.toString();
            }
        };
    }
}

测试类

import java.util.List;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.linjw.business.beans.Emp;
import com.linjw.redis.service.EmployeeService;

import webmvc.TestSuperClass;

public class TestSpringRedis extends TestSuperClass {

    @Autowired
    EmployeeService service;

    @Test
    public void service() {

        List<Emp> list2 = service.findAllEmpCacheable("abc");
        System.out.println("find list2 first time");
        System.out.println(list2);

        System.out.println("find list2 2nd time");


        list2 = service.findAllEmpCacheable("abc");
        System.out.println(list2);
    }

}

猜你喜欢

转载自blog.csdn.net/u012557814/article/details/79933253