| @@ -11,8 +11,7 @@ import org.springframework.data.redis.core.ValueOperations; | |||
| import com.alibaba.fastjson.JSON; | |||
| import com.alibaba.fastjson.JSONObject; | |||
| import com.alibaba.fastjson.TypeReference; | |||
| import com.iformall.domain.po.WxCUser; | |||
| import com.iformall.domain.po.base.BaseCUserEntity; | |||
| import com.iformall.domain.po.WxLevelConfig; | |||
| public class RedisCacheUtils { | |||
| @@ -21,11 +20,11 @@ public class RedisCacheUtils { | |||
| } | |||
| private static <T> List<T> parseListJson(String json,Class<T> clazz) { | |||
| return JSON.parseObject(json,new TypeReference<List<T>>() {}); | |||
| return JSON.parseObject(json,new TypeReference<List<T>>(clazz) {}); | |||
| } | |||
| private static <T> Set<T> parseSetJson(String json,Class<T> clazz) { | |||
| return JSON.parseObject(json,new TypeReference<Set<T>>() {}); | |||
| return JSON.parseObject(json,new TypeReference<Set<T>>(clazz) {}); | |||
| } | |||
| public static void cache(RedisTemplate<String, Object> template,String key, Object value,long seconds) { | |||
| @@ -41,7 +40,12 @@ public class RedisCacheUtils { | |||
| if (template.hasKey(key)) { | |||
| ValueOperations<String, Object> operations = template.opsForValue(); | |||
| Object o = operations.get(key); | |||
| return parseJson(JSON.toJSONString(o), clazz); | |||
| if (o instanceof String) { | |||
| return parseJson((String)o, clazz); | |||
| }else { | |||
| return parseJson(JSON.toJSONString(o), clazz); | |||
| } | |||
| } | |||
| return null; | |||
| } | |||
| @@ -50,7 +54,11 @@ public class RedisCacheUtils { | |||
| if (template.hasKey(key)) { | |||
| ValueOperations<String, Object> operations = template.opsForValue(); | |||
| Object o = operations.get(key); | |||
| return parseListJson(JSON.toJSONString(o), clazz); | |||
| if (o instanceof String) { | |||
| return parseListJson((String)o, clazz); | |||
| }else { | |||
| return parseListJson(JSON.toJSONString(o), clazz); | |||
| } | |||
| } | |||
| return null; | |||
| } | |||
| @@ -59,7 +67,11 @@ public class RedisCacheUtils { | |||
| if (template.hasKey(key)) { | |||
| ValueOperations<String, Object> operations = template.opsForValue(); | |||
| Object o = operations.get(key); | |||
| return parseSetJson(JSON.toJSONString(o), clazz); | |||
| if (o instanceof String) { | |||
| return parseSetJson((String)o, clazz); | |||
| }else { | |||
| return parseSetJson(JSON.toJSONString(o), clazz); | |||
| } | |||
| } | |||
| return null; | |||
| } | |||
| @@ -72,6 +84,9 @@ public class RedisCacheUtils { | |||
| if (template.hasKey(key)) { | |||
| ValueOperations<String, Object> operations = template.opsForValue(); | |||
| Object o = operations.get(key); | |||
| if( o instanceof String) { | |||
| return (String) o; | |||
| } | |||
| return JSON.toJSONString(o); | |||
| } | |||
| return null; | |||
| @@ -81,6 +96,9 @@ public class RedisCacheUtils { | |||
| if (template.hasKey(key)) { | |||
| ValueOperations<String, Object> operations = template.opsForValue(); | |||
| Object o = operations.get(key); | |||
| if (o instanceof String) { | |||
| Long.parseLong((String)o); | |||
| } | |||
| return Long.parseLong(o.toString()); | |||
| } | |||
| return null; | |||
| @@ -90,6 +108,9 @@ public class RedisCacheUtils { | |||
| if (template.hasKey(key)) { | |||
| ValueOperations<String, Object> operations = template.opsForValue(); | |||
| Object o = operations.get(key); | |||
| if (o instanceof String) { | |||
| return new Integer((String)o); | |||
| } | |||
| return new Integer(o.toString()); | |||
| } | |||
| return null; | |||
| @@ -100,32 +121,41 @@ public class RedisCacheUtils { | |||
| JSONObject jsonObject = JSON.parseObject(json); | |||
| Object p = jsonObject.get(property); | |||
| if (null != p ) { | |||
| if (p instanceof String) { | |||
| return parseJson((String)p, clazz); | |||
| } | |||
| return parseJson(JSON.toJSONString(p), clazz); | |||
| } | |||
| return null; | |||
| } | |||
| public static <T> List<T> getListFieldObject(Object o,String property,Class<T> clazz) { | |||
| public static <T> List<T> getFieldListObject(Object o,String property,Class<T> clazz) { | |||
| String json = JSON.toJSONString(o); | |||
| JSONObject jsonObject = JSON.parseObject(json); | |||
| Object p = jsonObject.get(property); | |||
| if (null != p ) { | |||
| if (p instanceof String) { | |||
| return parseListJson((String)p, clazz); | |||
| } | |||
| return parseListJson(JSON.toJSONString(p), clazz); | |||
| } | |||
| return null; | |||
| } | |||
| public static <T> Set<T> getSetFieldObject(Object o,String property,Class<T> clazz) { | |||
| public static <T> Set<T> getFieldSetObject(Object o,String property,Class<T> clazz) { | |||
| String json = JSON.toJSONString(o); | |||
| JSONObject jsonObject = JSON.parseObject(json); | |||
| Object p = jsonObject.get(property); | |||
| if (null != p ) { | |||
| if (p instanceof String) { | |||
| return parseSetJson((String)p, clazz); | |||
| } | |||
| return parseSetJson(JSON.toJSONString(p), clazz); | |||
| } | |||
| return null; | |||
| } | |||
| public static Map getMapFieldObject(Object o,String property) { | |||
| public static Map getFieldMapObject(Object o,String property) { | |||
| return getFieldObject(o,property,Map.class); | |||
| } | |||