CacheHelper.java
980 Bytes
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
39
40
41
42
43
44
45
package com.yohoufo.user.cache;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
public class CacheHelper {
/**
* 序列化value值
* @param value
* @return
*/
public static <T> String value2String(T value) {
String v = null;
if (value == null) {
return null;
}
if (value instanceof String) {
v = (String) value;
} else {
v = JSON.toJSONString(value);
}
return v;
}
/**
* 反序列化value值
*
* @param value
* @param clazz
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T string2Value(String value, Class<T> clazz) {
if (StringUtils.isEmpty(value)) {
return null;
}
if (clazz.getName().equalsIgnoreCase("java.lang.String")) {
return (T) value;
}
return (T) JSON.parseObject(value, clazz);
}
}