kongdeqiang
2024-04-07 8e51195319ea210e7ba06aabdabc40d64df14b08
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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;
 
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 {
 
    private static StringRedisTemplate stringRedisTemplate;
 
    public static <T> void set(String key, T value) throws IOException {
        String jsonValue = JSON.toJSONString(value);
        stringRedisTemplate.opsForValue().set(key, jsonValue);
    }
 
    public static <T> T get(String key, Class<T> type) throws IOException {
        String jsonValue = stringRedisTemplate.opsForValue().get(key);
        return jsonValue == null ? null : JSON.parseObject(jsonValue, type);
    }
    public static void del(String key)  {
        stringRedisTemplate.delete(key);
    }
 
}