springboot整合redis---进阶篇(向缓存中存放List对象JSON格式、取出缓存中List对象、设置序列化、设置过期时间)

前提概要:

  学过redis的我觉得大家是否觉得@Cacheable、@CachePut、@CacheEvict、@Caching、@CacheConfig是好用呢,但是任何事物都是有利有弊呀。

  第一坑:@Cacheable来向redis缓存中存放List对象,我在想这么一个场景:从数据库查询出来所有员工信息,首先我不需要任何参数,我的目的知识查询出全部的信息,犯错点1:key不知道如何去写 查看笔记可知,我们没有参数的情况下我们还可以使用方法名作为key。springboot整合redis---进阶篇(向缓存中存放List对象JSON格式、取出缓存中List对象、设置序列化、设置过期时间)_第1张图片

第二坑:当然我们可以使用@Cacheable去放缓存,key也指定好了,运行结束,查看Redis Desktop Manager发现缓存确实已经存放进去了,当我们再次请求的时候原理上是可以查询到缓存的吧,但是坑又来了。取出来缓存是JSON形式的,你需要反序列化到对象里呀,这一点我查询了很多博客文档,没有解决。

    困惑点1:在哪里加上反序列化操作

   困惑点2:在什么时候去加(有老师知道的可以评论一下,我在研究,先说声感谢)

   当然我也做了一些尝试,不过失败了,但是可以分享一下,因为对象序列化,那么我建一个类,里面是个List形式,我给这个类序列化,我也配置了相应的RedisTemplate和RedisManager想着照猫画虎去写,结果是行不通的。

完美解决:既然基于注解完不成,我退后原点,另找出路,手动去添加缓存,首先我执行方法,先查询缓存,查询到了万事大吉,查询不到好吧我去执行我的sql,把查询到的内容再放回缓存中。springboot整合redis---进阶篇(向缓存中存放List对象JSON格式、取出缓存中List对象、设置序列化、设置过期时间)_第2张图片

序列化模块我直接贴出代码:

package com.atguigu.config;

import com.atguigu.bean.Department;
import com.atguigu.bean.Employee;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Chenaniah Cheng
 * @create 2019-04-24
 */
@Configuration
public class MyRedisConfig {


    /**
     * 目前这个通用说得过去
     * @param redisConnectionFactory
     * @return
     * @throws UnknownHostException
     */
/*    @Bean
    public RedisTemplate myRedisTemplate(
            RedisConnectionFactory redisConnectionFactory)
            throws UnknownHostException {
        RedisTemplate template = new RedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        Jackson2JsonRedisSerializer ser = new Jackson2JsonRedisSerializer(Object.class);
        template.setDefaultSerializer(ser);


        System.out.println("自己定义的RedisTemplate来存储对象");

        return template;
    }*/

    @Bean
    public RedisTemplateempRedisTemplate(
            RedisConnectionFactory redisConnectionFactory)
            throws UnknownHostException {
        RedisTemplate template = new RedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        Jackson2JsonRedisSerializer ser = new Jackson2JsonRedisSerializer(Employee.class);
        template.setDefaultSerializer(ser);


        System.out.println("Employee的RedisTemplate来存储对象");

        return template;
    }

    @Bean
    public RedisTemplate deptRedisTemplate(
            RedisConnectionFactory redisConnectionFactory)
            throws UnknownHostException {
        RedisTemplate template = new RedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        Jackson2JsonRedisSerializer ser = new Jackson2JsonRedisSerializer(Department.class);
        template.setDefaultSerializer(ser);


        System.out.println("Department的RedisTemplate来存储对象");

        return template;
    }

    @Primary
    @Bean
    public RedisCacheManager cacheManager(RedisTemplate redisTemplate) {
        RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
        cacheManager.setUsePrefix(true);

        return cacheManager;
    }


    @Bean
    public RedisCacheManager empCacheManager(RedisTemplate  empRedisTemplate) {
        RedisCacheManager cacheManager = new RedisCacheManager(empRedisTemplate);
        cacheManager.setUsePrefix(true);
        cacheManager.setDefaultExpiration(60);
      Map map=new HashMap();
        map.put("emp",36000L);
        cacheManager.setExpires(map);
        return cacheManager;
    }


    @Bean
    public RedisCacheManager deptCacheManager(RedisTemplate  deptRedisTemplate) {
        RedisCacheManager cacheManager = new RedisCacheManager(deptRedisTemplate);
        cacheManager.setUsePrefix(true);
        return cacheManager;
    }

}

总体来说就是一个RedisTemplate一个相对应的RedisManager。

设置过期时间:

就我这个方法而言,我没使用注解方式,因此我在service中自己添加过期时间:、

empCacheManager.expire("emp:getEmployee",600, TimeUnit.SECONDS);如果使用注解的形式可以在配置中设置过期时间,也可以在例如@Cacheable注解上添加过期时间,这些内容网上都有,可以参考即可。

谢谢您查阅这篇博文,疑问请加QQ515598988 希望我能帮助到您

你可能感兴趣的:(springboot整合redis---进阶篇(向缓存中存放List对象JSON格式、取出缓存中List对象、设置序列化、设置过期时间))