kongdeqiang
2024-09-26 1d42efe4cbd6b1028a28ff7f3ef2b3d721051c2f
src/main/java/com/boying/util/RedisJsonUtil.java
@@ -1,7 +1,9 @@
package com.boying.util;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
@@ -17,21 +19,20 @@
 */
@Component
public class RedisJsonUtil {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    private final ObjectMapper objectMapper = new ObjectMapper();
    private static StringRedisTemplate stringRedisTemplate;
    public <T> void set(String key, T value) throws IOException {
        String jsonValue = objectMapper.writeValueAsString(value);
    public static <T> void set(String key, T value) throws IOException {
        String jsonValue = JSON.toJSONString(value);
        stringRedisTemplate.opsForValue().set(key, jsonValue);
    }
    public <T> T get(String key, Class<T> type) throws IOException {
    public static <T> T get(String key, Class<T> type) throws IOException {
        String jsonValue = stringRedisTemplate.opsForValue().get(key);
        return jsonValue == null ? null : objectMapper.readValue(jsonValue, type);
        return jsonValue == null ? null : JSON.parseObject(jsonValue, type);
    }
    public void del(String key)  {
    public static void del(String key)  {
        stringRedisTemplate.delete(key);
    }
}