看了好多篇文章,再根据自己的理解进行的配置。

一、Mevan相关配置

1.我的SpringBoot版本

<parent>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-parent</artifactId>    <version>2.1.7.RELEASE</version>    <relativePath/> <!-- lookup parent from repository --></parent>

2.引入spring-redis的依赖

<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-data-redis</artifactId></dependency>

二、application.yml中加入redis依赖

spring:  datasource:    url: jdbc:mysql://localhost:3306/spring_cache?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT    driver-class-name: com.mysql.cj.jdbc.Driver    username: root    password: 123456  redis:    host: 39.107.250.174    port: 6379    database: 0    timeout: 1000s  # 数据库连接超时时间,2.0 中该参数的类型为Duration,这里在配置的时候需要指明单位    # 连接池配置,2.0中直接使用jedis或者lettuce配置连接池    jedis:      pool:        # 最大空闲连接数        max-idle: 500        # 最小空闲连接数        min-idle: 50        # 等待可用连接的最大时间,负数为不限制        max-wait: -1        # 最大活跃连接数,负数为不限制        max-active: -1  cache:    redis:      time-to-live: -1 #毫秒      #以下可忽略mybatis:  configuration:    #开启驼峰命名    map-underscore-to-camel-case: truelogging:  level:    com.scitc.cache.mapper : debug

三、配置编写redis配置类

package com.scitc.cache.config;import com.fasterxml.jackson.annotation.JsonAutoDetect;import com.fasterxml.jackson.annotation.PropertyAccessor;import com.fasterxml.jackson.databind.ObjectMapper;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.cache.CacheManager;import org.springframework.cache.annotation.CachingConfigurerSupport;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.connection.RedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;import org.springframework.data.redis.serializer.RedisSerializationContext;import org.springframework.data.redis.serializer.RedisSerializer;import org.springframework.data.redis.serializer.StringRedisSerializer;import java.time.Duration;@Slf4j@Configuration@EnableCaching//启用缓存,这个注解很重要;//继承CachingConfigurerSupport,为了自定义生成KEY的策略。可以不继承。public class RedisConfig extends CachingConfigurerSupport {    @Value("${spring.cache.redis.time-to-live}")    private Duration timeToLive = Duration.ZERO;        @Bean(name = "redisTemplate")    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {        RedisTemplate<String, Object> template = new RedisTemplate<>();        template.setConnectionFactory(redisConnectionFactory);        //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值        Jackson2JsonRedisSerializer<Object> redisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);        ObjectMapper mapper = new ObjectMapper();        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);        mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);        redisSerializer.setObjectMapper(mapper);        template.setValueSerializer(redisSerializer);        //使用StringRedisSerializer来序列化和反序列化redis的key值        template.setKeySerializer(new StringRedisSerializer());        template.afterPropertiesSet();        return template;    }    @Bean    public CacheManager cacheManager(RedisConnectionFactory factory) {        RedisSerializer<String> redisSerializer = new StringRedisSerializer();        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);        //解决查询缓存转换异常的问题        ObjectMapper om = new ObjectMapper();        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);        jackson2JsonRedisSerializer.setObjectMapper(om);        // 配置序列化(解决乱码的问题)        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()                .entryTtl(timeToLive)                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))                .disableCachingNullValues();        RedisCacheManager cacheManager = RedisCacheManager.builder(factory)                .cacheDefaults(config)                .build();        return cacheManager;    }}

然后我发现有好几篇文章配置 CacheManager 采用的是如下配置:

    @Bean    public CacheManager cacheManager(RedisConnectionFactory factory) {        // 生成一个默认配置,通过config对象即可对缓存进行自定义配置        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();        // 设置缓存的默认过期时间,也是使用Duration设置        config = config.entryTtl(Duration.ofMinutes(1))                .disableCachingNullValues();     // 不缓存空值        // 设置一个初始化的缓存空间set集合        Set<String> cacheNames = new HashSet<>();        cacheNames.add("my-redis-cache1");        cacheNames.add("my-redis-cache2");        // 对每个缓存空间应用不同的配置        Map<String, RedisCacheConfiguration> configMap = new HashMap<>();        configMap.put("my-redis-cache1", config);        configMap.put("my-redis-cache2", config.entryTtl(Duration.ofSeconds(120)));        // 使用自定义的缓存配置初始化一个cacheManager        RedisCacheManager cacheManager = RedisCacheManager.builder(factory)                .initialCacheNames(cacheNames)  // 注意这两句的调用顺序,一定要先调用该方法设置初始化的缓存名,再初始化相关的配置                .withInitialCacheConfigurations(configMap)                .build();        return cacheManager;    }

但经过我使用测试发现,他在redis中保存的数据是这样的,并不是json

后面又找到一篇文章,重新配置 CacheManager (就是我直接贴出来的完整配置代码)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/caojida...

经过测试访问保存的数据格式为 json(下方有图片)

到此配置完成,正常运行

RedisUtils工具类

作者:zeng1994
出处:http://www.cnblogs.com/zeng1994/
本文版权归作者和博客园共有,欢迎转载!但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接!

package com.scitc.cache.utils;import java.util.List;import java.util.Map;import java.util.Set;import java.util.concurrent.TimeUnit;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.stereotype.Component;import org.springframework.util.CollectionUtils;@Componentpublic class RedisUtil {    @Autowired    private RedisTemplate<String, Object> redisTemplate;    /**     * 指定缓存失效时间     * @param key 键     * @param time 时间(秒)     * @return     */    public boolean expire(String key,long time){        try {            if(time>0){                redisTemplate.expire(key, time, TimeUnit.SECONDS);            }            return true;        } catch (Exception e) {            e.printStackTrace();            return false;        }    }    /**     * 根据key 获取过期时间     * @param key 键 不能为null     * @return 时间(秒) 返回0代表为永久有效     */    public long getExpire(String key){        return redisTemplate.getExpire(key,TimeUnit.SECONDS);    }    /**     * 判断key是否存在     * @param key 键     * @return true 存在 false不存在     */    public boolean hasKey(String key){        try {            return redisTemplate.hasKey(key);        } catch (Exception e) {            e.printStackTrace();            return false;        }    }    /**     * 删除缓存     * @param key 可以传一个值 或多个     */    @SuppressWarnings("unchecked")    public void del(String ... key){        if(key!=null&&key.length>0){            if(key.length==1){                redisTemplate.delete(key[0]);            }else{                redisTemplate.delete(CollectionUtils.arrayToList(key));            }        }    }    //============================String=============================    /**     * 普通缓存获取     * @param key 键     * @return 值     */    public Object get(String key){        return key==null?null:redisTemplate.opsForValue().get(key);    }    /**     * 普通缓存放入     * @param key 键     * @param value 值     * @return true成功 false失败     */    public boolean set(String key,Object value) {        try {            redisTemplate.opsForValue().set(key, value);            return true;        } catch (Exception e) {            e.printStackTrace();            return false;        }    }    /**     * 普通缓存放入并设置时间     * @param key 键     * @param value 值     * @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期     * @return true成功 false 失败     */    public boolean set(String key,Object value,long time){        try {            if(time>0){                redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);            }else{                set(key, value);            }            return true;        } catch (Exception e) {            e.printStackTrace();            return false;        }    }    /**     * 递增     * @param key 键     * @param by 要增加几(大于0)     * @return     */    public long incr(String key, long delta){        if(delta<0){            throw new RuntimeException("递增因子必须大于0");        }        return redisTemplate.opsForValue().increment(key, delta);    }    /**     * 递减     * @param key 键     * @param by 要减少几(小于0)     * @return     */    public long decr(String key, long delta){        if(delta<0){            throw new RuntimeException("递减因子必须大于0");        }        return redisTemplate.opsForValue().increment(key, -delta);    }    //================================Map=================================    /**     * HashGet     * @param key 键 不能为null     * @param item 项 不能为null     * @return 值     */    public Object hget(String key,String item){        return redisTemplate.opsForHash().get(key, item);    }    /**     * 获取hashKey对应的所有键值     * @param key 键     * @return 对应的多个键值     */    public Map<Object,Object> hmget(String key){        return redisTemplate.opsForHash().entries(key);    }    /**     * HashSet     * @param key 键     * @param map 对应多个键值     * @return true 成功 false 失败     */    public boolean hmset(String key, Map<String,Object> map){        try {            redisTemplate.opsForHash().putAll(key, map);            return true;        } catch (Exception e) {            e.printStackTrace();            return false;        }    }    /**     * HashSet 并设置时间     * @param key 键     * @param map 对应多个键值     * @param time 时间(秒)     * @return true成功 false失败     */    public boolean hmset(String key, Map<String,Object> map, long time){        try {            redisTemplate.opsForHash().putAll(key, map);            if(time>0){                expire(key, time);            }            return true;        } catch (Exception e) {            e.printStackTrace();            return false;        }    }    /**     * 向一张hash表中放入数据,如果不存在将创建     * @param key 键     * @param item 项     * @param value 值     * @return true 成功 false失败     */    public boolean hset(String key,String item,Object value) {        try {            redisTemplate.opsForHash().put(key, item, value);            return true;        } catch (Exception e) {            e.printStackTrace();            return false;        }    }    /**     * 向一张hash表中放入数据,如果不存在将创建     * @param key 键     * @param item 项     * @param value 值     * @param time 时间(秒)  注意:如果已存在的hash表有时间,这里将会替换原有的时间     * @return true 成功 false失败     */    public boolean hset(String key,String item,Object value,long time) {        try {            redisTemplate.opsForHash().put(key, item, value);            if(time>0){                expire(key, time);            }            return true;        } catch (Exception e) {            e.printStackTrace();            return false;        }    }    /**     * 删除hash表中的值     * @param key 键 不能为null     * @param item 项 可以使多个 不能为null     */    public void hdel(String key, Object... item){        redisTemplate.opsForHash().delete(key,item);    }    /**     * 判断hash表中是否有该项的值     * @param key 键 不能为null     * @param item 项 不能为null     * @return true 存在 false不存在     */    public boolean hHasKey(String key, String item){        return redisTemplate.opsForHash().hasKey(key, item);    }    /**     * hash递增 如果不存在,就会创建一个 并把新增后的值返回     * @param key 键     * @param item 项     * @param by 要增加几(大于0)     * @return     */    public double hincr(String key, String item,double by){        return redisTemplate.opsForHash().increment(key, item, by);    }    /**     * hash递减     * @param key 键     * @param item 项     * @param by 要减少记(小于0)     * @return     */    public double hdecr(String key, String item,double by){        return redisTemplate.opsForHash().increment(key, item,-by);    }    //============================set=============================    /**     * 根据key获取Set中的所有值     * @param key 键     * @return     */    public Set<Object> sGet(String key){        try {            return redisTemplate.opsForSet().members(key);        } catch (Exception e) {            e.printStackTrace();            return null;        }    }    /**     * 根据value从一个set中查询,是否存在     * @param key 键     * @param value 值     * @return true 存在 false不存在     */    public boolean sHasKey(String key,Object value){        try {            return redisTemplate.opsForSet().isMember(key, value);        } catch (Exception e) {            e.printStackTrace();            return false;        }    }    /**     * 将数据放入set缓存     * @param key 键     * @param values 值 可以是多个     * @return 成功个数     */    public long sSet(String key, Object...values) {        try {            return redisTemplate.opsForSet().add(key, values);        } catch (Exception e) {            e.printStackTrace();            return 0;        }    }    /**     * 将set数据放入缓存     * @param key 键     * @param time 时间(秒)     * @param values 值 可以是多个     * @return 成功个数     */    public long sSetAndTime(String key,long time,Object...values) {        try {            Long count = redisTemplate.opsForSet().add(key, values);            if(time>0) expire(key, time);            return count;        } catch (Exception e) {            e.printStackTrace();            return 0;        }    }    /**     * 获取set缓存的长度     * @param key 键     * @return     */    public long sGetSetSize(String key){        try {            return redisTemplate.opsForSet().size(key);        } catch (Exception e) {            e.printStackTrace();            return 0;        }    }    /**     * 移除值为value的     * @param key 键     * @param values 值 可以是多个     * @return 移除的个数     */    public long setRemove(String key, Object ...values) {        try {            Long count = redisTemplate.opsForSet().remove(key, values);            return count;        } catch (Exception e) {            e.printStackTrace();            return 0;        }    }    //===============================list=================================    /**     * 获取list缓存的内容     * @param key 键     * @param start 开始     * @param end 结束  0 到 -1代表所有值     * @return     */    public List<Object> lGet(String key,long start, long end){        try {            return redisTemplate.opsForList().range(key, start, end);        } catch (Exception e) {            e.printStackTrace();            return null;        }    }    /**     * 获取list缓存的长度     * @param key 键     * @return     */    public long lGetListSize(String key){        try {            return redisTemplate.opsForList().size(key);        } catch (Exception e) {            e.printStackTrace();            return 0;        }    }    /**     * 通过索引 获取list中的值     * @param key 键     * @param index 索引  index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推     * @return     */    public Object lGetIndex(String key,long index){        try {            return redisTemplate.opsForList().index(key, index);        } catch (Exception e) {            e.printStackTrace();            return null;        }    }    /**     * 将list放入缓存     * @param key 键     * @param value 值     * @param time 时间(秒)     * @return     */    public boolean lSet(String key, Object value) {        try {            redisTemplate.opsForList().rightPush(key, value);            return true;        } catch (Exception e) {            e.printStackTrace();            return false;        }    }    /**     * 将list放入缓存     * @param key 键     * @param value 值     * @param time 时间(秒)     * @return     */    public boolean lSet(String key, Object value, long time) {        try {            redisTemplate.opsForList().rightPush(key, value);            if (time > 0) expire(key, time);            return true;        } catch (Exception e) {            e.printStackTrace();            return false;        }    }    /**     * 将list放入缓存     * @param key 键     * @param value 值     * @param time 时间(秒)     * @return     */    public boolean lSet(String key, List<Object> value) {        try {            redisTemplate.opsForList().rightPushAll(key, value);            return true;        } catch (Exception e) {            e.printStackTrace();            return false;        }    }    /**     * 将list放入缓存     * @param key 键     * @param value 值     * @param time 时间(秒)     * @return     */    public boolean lSet(String key, List<Object> value, long time) {        try {            redisTemplate.opsForList().rightPushAll(key, value);            if (time > 0) expire(key, time);            return true;        } catch (Exception e) {            e.printStackTrace();            return false;        }    }    /**     * 根据索引修改list中的某条数据     * @param key 键     * @param index 索引     * @param value 值     * @return     */    public boolean lUpdateIndex(String key, long index,Object value) {        try {            redisTemplate.opsForList().set(key, index, value);            return true;        } catch (Exception e) {            e.printStackTrace();            return false;        }    }    /**     * 移除N个值为value     * @param key 键     * @param count 移除多少个     * @param value 值     * @return 移除的个数     */    public long lRemove(String key,long count,Object value) {        try {            Long remove = redisTemplate.opsForList().remove(key, count, value);            return remove;        } catch (Exception e) {            e.printStackTrace();            return 0;        }    }}

自己遇到的问题

发现了个问题(不是配置文件的问题),我的对象实体类是这样的

@Datapublic class Employee implements Serializable {        private Integer id;    private String lastName;    private String email;    private Integer gender; //性别 1男  0女//    private Integer userId;    private Integer dId;}

在json数据中多了一个did,

但获取然后打印出来并没有多数据

emp:Employee(id=2, lastName=李四, email=132456798@qq.com, gender=0, dId=1)

问了大佬说是命名问题
当把Employee中的 dId改成 userId时(以及修改数据库字段)

这次userId居然不重复,后面吧dId换成了uId,依旧重复。emmm玄学。。。

最开始不知道集成redis怎么在业务中使用,根据自己理解发现,当你配置了redis,SpringBoot会自动把缓存加到redis中,并不使用默认的

    @Cacheable(value = {"emp"},key = "#id")    public Employee getEmp(Integer id){        log.info("查询" + id + "号员工");        Employee employee = employeeMapper.getEmpById(id);        return employee;    }

因为配置了redis所以,使用redis缓存不使用默认的