CacheHelper.java 980 Bytes
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);
    }


}