33import com .fasterxml .jackson .core .type .TypeReference ;
44import org .springframework .beans .factory .InitializingBean ;
55import org .springframework .beans .factory .annotation .Autowired ;
6- import org .springframework .data .redis .core .StringRedisTemplate ;
6+ import org .springframework .data .redis .core .RedisTemplate ;
77import org .springframework .stereotype .Component ;
8- import top .cadecode .uniboot .common .core .util .JacksonUtil ;
98
109import java .util .Objects ;
1110import java .util .concurrent .TimeUnit ;
1918@ Component
2019public class RedisUtil implements InitializingBean {
2120
22- public static StringRedisTemplate TEMPLATE ;
21+ private static RedisTemplate < String , Object > TEMPLATE ;
2322
24- private StringRedisTemplate stringRedisTemplate ;
23+ private RedisTemplate <String , Object > redisTemplate ;
24+
25+ public static RedisTemplate <String , Object > getTemplate () {
26+ return TEMPLATE ;
27+ }
2528
2629 /**
2730 * 自动注入 stringRedisTemplate
2831 */
2932 @ Autowired (required = false )
30- public void setStringRedisTemplate (StringRedisTemplate stringRedisTemplate ) {
31- this .stringRedisTemplate = stringRedisTemplate ;
33+ public void setStringRedisTemplate (RedisTemplate <String , Object > redisTemplate ) {
34+ this .redisTemplate = redisTemplate ;
35+ }
36+
37+ /**
38+ * 获取 value,String 类型
39+ */
40+ public static String get (String key ) {
41+ return get (key , String .class );
3242 }
3343
3444 /**
35- * 获取 value
45+ * 获取 value,指定类型
3646 */
47+ @ SuppressWarnings ("unchecked" )
3748 public static <T > T get (String key , Class <T > clazz ) {
38- String json = TEMPLATE .opsForValue ().get (key );
39- return JacksonUtil .toBean (json , clazz );
49+ return (T ) TEMPLATE .opsForValue ().get (key );
4050 }
4151
4252 /**
43- * 获取 value
53+ * 获取 value,指定类型
4454 */
55+ @ SuppressWarnings ("unchecked" )
4556 public static <T > T get (String key , TypeReference <T > typeReference ) {
46- String json = TEMPLATE .opsForValue ().get (key );
47- return JacksonUtil .toBean (json , typeReference );
57+ return (T ) TEMPLATE .opsForValue ().get (key );
4858 }
4959
5060 /**
5161 * 添加 key
5262 */
5363 public static void set (String key , Object o ) {
54- String json = JacksonUtil .toJson (o );
55- TEMPLATE .opsForValue ().set (key , json );
64+ TEMPLATE .opsForValue ().set (key , o );
5665 }
5766
5867 /**
5968 * 添加 key,如果不存在
6069 */
6170 public static Boolean setIfAbsent (String key , Object o ) {
62- String json = JacksonUtil .toJson (o );
63- return TEMPLATE .opsForValue ().setIfAbsent (key , json );
71+ return TEMPLATE .opsForValue ().setIfAbsent (key , o );
6472 }
6573
6674 /**
6775 * 添加 key,如果不存在
6876 */
6977 public static Boolean setIfPresent (String key , Object o ) {
70- String json = JacksonUtil .toJson (o );
71- return TEMPLATE .opsForValue ().setIfPresent (key , json );
78+ return TEMPLATE .opsForValue ().setIfPresent (key , o );
7279 }
7380
7481 /**
7582 * 添加 key 并设置有效期
7683 */
7784 public static void set (String key , Object o , long timeout , TimeUnit timeUnit ) {
78- String json = JacksonUtil .toJson (o );
79- TEMPLATE .opsForValue ().set (key , json , timeout , timeUnit );
85+ TEMPLATE .opsForValue ().set (key , o , timeout , timeUnit );
8086 }
8187
8288 /**
8389 * 添加 key 并设置有效期,如果不存在
8490 */
8591 public static Boolean setIfAbsent (String key , Object o , long timeout , TimeUnit timeUnit ) {
86- String json = JacksonUtil .toJson (o );
87- return TEMPLATE .opsForValue ().setIfAbsent (key , json , timeout , timeUnit );
92+ return TEMPLATE .opsForValue ().setIfAbsent (key , o , timeout , timeUnit );
8893 }
8994
9095 /**
9196 * 添加 key 并设置有效期,如果不存在
9297 */
9398 public static Boolean setIfPresent (String key , Object o , long timeout , TimeUnit timeUnit ) {
94- String json = JacksonUtil .toJson (o );
95- return TEMPLATE .opsForValue ().setIfPresent (key , json , timeout , timeUnit );
99+ return TEMPLATE .opsForValue ().setIfPresent (key , o , timeout , timeUnit );
96100 }
97101
98102 /**
@@ -118,7 +122,7 @@ public static Boolean expire(String key, long timeout, TimeUnit timeUnit) {
118122
119123 @ Override
120124 public void afterPropertiesSet () {
121- TEMPLATE = stringRedisTemplate ;
125+ TEMPLATE = redisTemplate ;
122126 if (Objects .isNull (TEMPLATE )) {
123127 throw new IllegalArgumentException ("Bean stringRedisTemplate not found" );
124128 }
0 commit comments