springboot 接入redis作为缓存(Springboot accesses redis as cache)

1.pom

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-redis</artifactId>
            <version>1.4.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
            <version>2.1.6.RELEASE</version>
        </dependency>

2. 配置 

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;

/**
 * redis有三种模式-standalone,sentinel,cluster,
 *  @EnableCaching  开启缓存
 */
@Configuration
@EnableCaching
public class SpringRedisSessionConfig{

    private String redisHost = "127.0.0.1";
    private Integer port = 6379;

    @Bean("connectionFactory")
    public RedisConnectionFactory redisStandaloneMasterConnectionFactory() {
        RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration();
        redisConfig.setHostName(redisHost);
        redisConfig.setPort(port);
        redisConfig.setDatabase(0);
        RedisConnectionFactory lettuceConnectionFactory = new JedisConnectionFactory(redisConfig);
        return lettuceConnectionFactory;
    }

    @Bean
    public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
        RedisCacheConfiguration redisCacheConfiguration= RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofSeconds(1000));    //entryTtl 代表过期时间
        return RedisCacheManager.builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
                .cacheDefaults(redisCacheConfiguration).build();
    }


    // 对象序列化
    @Bean
    public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory connectionFactory){
        RedisTemplate<Object,Object> redisTemplate=new RedisTemplate<>();
        redisTemplate.setConnectionFactory(connectionFactory);
        //使用Jackson2JsonRedisSerializer替换默认的序列化规则
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper=new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);

        //设置value的序列化规则
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        //设置key的序列化规则
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

    /**
     * sentinel模式的通道提供
     * @return
     */
//    @Bean("redisSentinelctionFactory")
//    public RedisConnectionFactory redisSentinelctionFactory() {
//        RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration()
//                .master("mymaster")
//                .sentinel("10.70.33.238", 26379)
//                .sentinel("10.70.33.239", 26379)
//                .sentinel("10.70.33.246", 26379);
//        return new JedisConnectionFactory(redisSentinelConfiguration);
//    }

    /**
     * cluster模式的通道提供
     * @return
     */
//    @Bean("redisRedisClusterFactory")
//    public RedisConnectionFactory redisSentinelctionFactory() {
//        RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration()
//        RedisNode redisNode1 = new RedisNode("127.0.0.1", 6379);    // 有多少个节点就写多少个节点
//        RedisNode redisNode2 = new RedisNode("127.0.0.1", 6379);
//        redisClusterConfiguration.addClusterNode(redisNode1);
//        redisClusterConfiguration.addClusterNode(redisNode2);
//        return new JedisConnectionFactory(redisClusterConfiguration);
//    }

}

3.定义key 生成规则

import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.lang.reflect.Method;

/**
 * 自定义缓存的 key生成
 */

@Component
public class MyKeyGenerator implements KeyGenerator {
    @Override
    public Object generate(Object o, Method method, Object... objects) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(o.getClass().getSimpleName())
                .append("_")
                .append(method.getName())
                .append("_")
                .append(StringUtils.arrayToDelimitedString(objects, "_"));
        return stringBuffer.toString();
    }
}

 4. 使用


import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * @Cacheable  加上代表需要缓存的方法
 * @CacheEvict 注解代表需要清除缓存
 * cacheNames 为缓存的key
 *
 * 这样使用缺点是 缓存不会自动过期,只有调用 @CacheEvict 的方法才会过期
 *
 */

@RequestMapping
@RestController
public class Demo01Controller {

    @GetMapping("a")
    @Cacheable(cacheNames = "demo01",keyGenerator = "myKeyGenerator")
    public String test1() {
        System.out.println(LocalDateTime.now() + " 进入了方法 ");
        return "aaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    }

    @GetMapping("b")
    public String test2() {
        System.out.println(LocalDateTime.now() + " 进入了方法 ");
        return "bbbbbbbbbbbbbbbbbb";
    }

    @CacheEvict(cacheNames = "demo01", allEntries = true)
    @GetMapping("clear")
    public String clear() {
        return "清除成功";
    }
}
————————

1.pom

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-redis</artifactId>
            <version>1.4.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
            <version>2.1.6.RELEASE</version>
        </dependency>

2. Configuration

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;

/**
 * redis有三种模式-standalone,sentinel,cluster,
 *  @EnableCaching  开启缓存
 */
@Configuration
@EnableCaching
public class SpringRedisSessionConfig{

    private String redisHost = "127.0.0.1";
    private Integer port = 6379;

    @Bean("connectionFactory")
    public RedisConnectionFactory redisStandaloneMasterConnectionFactory() {
        RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration();
        redisConfig.setHostName(redisHost);
        redisConfig.setPort(port);
        redisConfig.setDatabase(0);
        RedisConnectionFactory lettuceConnectionFactory = new JedisConnectionFactory(redisConfig);
        return lettuceConnectionFactory;
    }

    @Bean
    public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
        RedisCacheConfiguration redisCacheConfiguration= RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofSeconds(1000));    //entryTtl 代表过期时间
        return RedisCacheManager.builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
                .cacheDefaults(redisCacheConfiguration).build();
    }


    // 对象序列化
    @Bean
    public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory connectionFactory){
        RedisTemplate<Object,Object> redisTemplate=new RedisTemplate<>();
        redisTemplate.setConnectionFactory(connectionFactory);
        //使用Jackson2JsonRedisSerializer替换默认的序列化规则
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper=new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);

        //设置value的序列化规则
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        //设置key的序列化规则
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

    /**
     * sentinel模式的通道提供
     * @return
     */
//    @Bean("redisSentinelctionFactory")
//    public RedisConnectionFactory redisSentinelctionFactory() {
//        RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration()
//                .master("mymaster")
//                .sentinel("10.70.33.238", 26379)
//                .sentinel("10.70.33.239", 26379)
//                .sentinel("10.70.33.246", 26379);
//        return new JedisConnectionFactory(redisSentinelConfiguration);
//    }

    /**
     * cluster模式的通道提供
     * @return
     */
//    @Bean("redisRedisClusterFactory")
//    public RedisConnectionFactory redisSentinelctionFactory() {
//        RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration()
//        RedisNode redisNode1 = new RedisNode("127.0.0.1", 6379);    // 有多少个节点就写多少个节点
//        RedisNode redisNode2 = new RedisNode("127.0.0.1", 6379);
//        redisClusterConfiguration.addClusterNode(redisNode1);
//        redisClusterConfiguration.addClusterNode(redisNode2);
//        return new JedisConnectionFactory(redisClusterConfiguration);
//    }

}

3. Define key} generation rules

import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.lang.reflect.Method;

/**
 * 自定义缓存的 key生成
 */

@Component
public class MyKeyGenerator implements KeyGenerator {
    @Override
    public Object generate(Object o, Method method, Object... objects) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(o.getClass().getSimpleName())
                .append("_")
                .append(method.getName())
                .append("_")
                .append(StringUtils.arrayToDelimitedString(objects, "_"));
        return stringBuffer.toString();
    }
}

4. Use


import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * @Cacheable  加上代表需要缓存的方法
 * @CacheEvict 注解代表需要清除缓存
 * cacheNames 为缓存的key
 *
 * 这样使用缺点是 缓存不会自动过期,只有调用 @CacheEvict 的方法才会过期
 *
 */

@RequestMapping
@RestController
public class Demo01Controller {

    @GetMapping("a")
    @Cacheable(cacheNames = "demo01",keyGenerator = "myKeyGenerator")
    public String test1() {
        System.out.println(LocalDateTime.now() + " 进入了方法 ");
        return "aaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    }

    @GetMapping("b")
    public String test2() {
        System.out.println(LocalDateTime.now() + " 进入了方法 ");
        return "bbbbbbbbbbbbbbbbbb";
    }

    @CacheEvict(cacheNames = "demo01", allEntries = true)
    @GetMapping("clear")
    public String clear() {
        return "清除成功";
    }
}