| | |
| | | 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; |
| | | |
| | |
| | | */ |
| | | @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); |
| | | } |
| | | |
| | | } |