【Spring Boot】2.2 RedisUtil

本文介绍了一个用于Spring Boot项目的Redis缓存工具类的设计与实现细节,包括如何处理不同类型的缓存数据、序列化配置及缓存管理操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

准备工作

  • 在SpringBoot 的 POM 中有引入 Redis starter
  • 在application.properties 中 有设置Redis的服务器和账号密码等信息

创建RedisUtil工具类

utils包中创建该类

import com.alibaba.druid.util.StringUtils;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.TimeUnit;

/***
 * Author: YL.Lou
 * Class: RedisUtil
 * Project: washes_base_backstage
 * Introduce:
 * DateTime: 2022-06-27 13:58
 ***/

@Slf4j
@Component
public class RedisUtil {

    @Autowired
    private StringRedisTemplate redisTemplate;

    private final String DEFAULT_KEY_PREFIX = "";
    private final int EXPIRE_TIME = 1;
    private final TimeUnit EXPIRE_TIME_TYPE = TimeUnit.DAYS;

    private ObjectMapper objectMapper = new ObjectMapper(new JsonFactory());


    /**
     * 数据缓存至redis
     *
     * @param key
     * @param value
     * @return
     */
    public <K, V> void add(K key, V value) {

        if (value != null) {
            if (BaseUtil.isBaseDefaultValue(value)) {
                log.info("基础数据,简单缓存。");

                try {
                    redisTemplate
                            .opsForValue()
                            .set(DEFAULT_KEY_PREFIX + key, value.toString());
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                    throw new RuntimeException("基础数据缓存至redis失败");
                }
            } else {
                log.info("高级数据,复杂缓存。");

                try {
                    // 注册时间转换插件
                    objectMapper.registerModule(new JavaTimeModule());
                    redisTemplate
                            .opsForValue()
                            .set(DEFAULT_KEY_PREFIX + key, objectMapper.writeValueAsString(value));
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                    throw new RuntimeException("高级数据缓存至redis失败");
                }
            }
        }
    }

    /**
     * 数据缓存至redis并设置过期时间
     *
     * @param key
     * @param value
     * @return
     */
    public <K, V> void add(K key, V value, long timeout, TimeUnit unit) {

        if (value != null) {
            if (BaseUtil.isBaseDefaultValue(value)) {
                try {
                    redisTemplate
                            .opsForValue()
                            .set(DEFAULT_KEY_PREFIX + key, value.toString(), timeout, unit);
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                    throw new RuntimeException("基础数据缓存至redis失败");
                }
            } else {
                try {
                    // 注册时间转换插件
                    objectMapper.registerModule(new JavaTimeModule());
                    redisTemplate
                            .opsForValue()
                            .set(DEFAULT_KEY_PREFIX + key, objectMapper.writeValueAsString(value), timeout, unit);

                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                    throw new RuntimeException("高级数据缓存至redis失败");
                }
            }
        }else{
            throw new RuntimeException("变量值不存在");
        }
    }

    /**
     * 写入 hash-set,已经是key-value的键值,不能再写入为hash-set
     *
     * @param key    must not be {@literal null}.
     * @param subKey must not be {@literal null}.
     * @param value  写入的值
     */
    public <K, SK, V> void addHashCache(K key, SK subKey, V value) {
        redisTemplate.opsForHash().put(DEFAULT_KEY_PREFIX + key, subKey, value);
    }

    /**
     * 写入 hash-set,并设置过期时间
     *
     * @param key    must not be {@literal null}.
     * @param subKey must not be {@literal null}.
     * @param value  写入的值
     */
    public <K, SK, V> void addHashCache(K key, SK subKey, V value, long timeout, TimeUnit unit) {
        redisTemplate.opsForHash().put(DEFAULT_KEY_PREFIX + key, subKey, value);
        redisTemplate.expire(DEFAULT_KEY_PREFIX + key, timeout, unit);
    }

    /**
     * 获取 hash-setvalue
     *
     * @param key    must not be {@literal null}.
     * @param subKey must not be {@literal null}.
     */
    public <K, SK> Object getHashCache(K key, SK subKey) {
        return redisTemplate.opsForHash().get(DEFAULT_KEY_PREFIX + key, subKey);
    }

    /**
     * 从redis中获取缓存数据,转成对象
     *
     * @param key   must not be {@literal null}.
     * @param clazz 对象类型
     * @return
     */
    public <K, V> V getObject(K key, Class<V> clazz) throws JsonProcessingException {
        String value = this.get(key);
        V result = null;
        if (!StringUtils.isEmpty(value)) {
            objectMapper.registerModule(new JavaTimeModule());
            result = objectMapper.readValue(value, clazz);
        }
        return result;
    }

    /**
     * 从redis中获取缓存数据,转成list
     *
     * @param key   must not be {@literal null}.
     * @param clazz 对象类型
     * @return
     */
    public <K, V> List<V> getList(K key, Class<V> clazz) throws JsonProcessingException {
        String value = this.get(key);
        List<V> result = Collections.emptyList();
        if (!StringUtils.isEmpty(value)) {
            objectMapper.registerModule(new JavaTimeModule());
            result = (List<V>) objectMapper.readValue(value, clazz);
        }
        return result;
    }

    /**
     * 功能描述:Get the value of {@code key}.
     *
     * @param key must not be {@literal null}.
     * @return java.lang.String
     * @date 2021/9/19
     **/
    public <K> String get(K key) {
        String value;
        try {
            value = redisTemplate.opsForValue().get(DEFAULT_KEY_PREFIX + key);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException("从redis缓存中获取缓存数据失败");
        }
        return value;
    }

    /**
     * 删除key
     */
    public void delete(String key) {
        redisTemplate.delete(key);
    }

    /**
     * 批量删除key
     */
    public void delete(Collection<String> keys) {
        redisTemplate.delete(keys);
    }

    /**
     * 获取全部Key值
     * @return
     */
    public Collection<String> getKeys(){
        Set<String> keys = redisTemplate.keys("*");
        return keys;
    }

    /**
     * 序列化key
     */
    public byte[] dump(String key) {
        return redisTemplate.dump(key);
    }

    /**
     * 是否存在key
     */
    public Boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }

    /**
     * 设置过期时间
     */
    public Boolean expire(String key, long timeout, TimeUnit unit) {
        return redisTemplate.expire(key, timeout, unit);
    }

    /**
     * 设置过期时间
     */
    public Boolean expireAt(String key, Date date) {
        return redisTemplate.expireAt(key, date);
    }

    /**
     * 移除 key 的过期时间,key 将持久保持
     */
    public Boolean persist(String key) {
        return redisTemplate.persist(key);
    }

    /**
     * 返回 key 的剩余的过期时间
     */
    public Long getExpire(String key, TimeUnit unit) {
        return redisTemplate.getExpire(key, unit);
    }

    /**
     * 返回 key 的剩余的过期时间
     */
    public Long getExpire(String key) {
        return redisTemplate.getExpire(key);
    }
}

补充

在POM中有引入一个JSR310就是给这个工具类用的
SpringBoot自带的Jackson的ObjectMapper不能处理LocalDateTime的序列化
上边代码中的在objectMapper.readValue或者是writeValue之前的
objectMapper.registerModul(new JavaTimeModule())
就是用来解决这个问你题的

这个工具类原本是从网上翻来的,但是没有对这一块有很好的处理 。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值