package com.boying.util; import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.ibatis.jdbc.Null; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.io.IOException; /** * @author kdq * @version 1.0.0 * @ClassName RedisJsonUtil.java * @Description TODO * @createTime 2024年03月18日 15:20:00 */ @Component public class RedisJsonUtil { @Resource private StringRedisTemplate stringRedisTemplate; private final ObjectMapper objectMapper = new ObjectMapper(); public void set(String key, T value) throws IOException { String jsonValue = objectMapper.writeValueAsString(value); stringRedisTemplate.opsForValue().set(key, jsonValue); } public T get(String key, Class type) throws IOException { String jsonValue = stringRedisTemplate.opsForValue().get(key); return jsonValue == null ? null : objectMapper.readValue(jsonValue, type); } public void del(String key) { stringRedisTemplate.delete(key); } }