Browse Source

更换redis json序列化类

刘韬 5 years ago
parent
commit
7fbb0e9d91

+ 2 - 1
PacsOnline_Wechat_Doctor/src/main/java/com/zskk/common/ZskkConfig.java

@@ -25,6 +25,7 @@ import com.jfinal.wxaapp.WxaConfig;
 import com.jfinal.wxaapp.WxaConfigKit;
 import com.zskk.controller.WxaUserApiController;
 import com.zskk.model._MappingKit;
+import com.zskk.util.JsonSerializer;
 
 
 public class ZskkConfig extends JFinalConfig {
@@ -86,7 +87,7 @@ public class ZskkConfig extends JFinalConfig {
 		me.add(redisPlugin);
 		
 		RedisPlugin redisPluginPc = new RedisPlugin("pc", "10.46.159.65", 6379, 2000, "Zskk_2019", 0);
-		redisPluginPc.setSerializer(JdkSerializer.me); // 需要使用fst高性能序列化的用户请删除这一行(Fst jar依赖请查看WIKI)
+		redisPluginPc.setSerializer(new JsonSerializer()); // 需要使用fst高性能序列化的用户请删除这一行(Fst jar依赖请查看WIKI)
 		me.add(redisPluginPc);
 	}
 

+ 1 - 2
PacsOnline_Wechat_Doctor/src/main/java/com/zskk/controller/WxaUserApiController.java

@@ -285,9 +285,8 @@ public class WxaUserApiController extends WxaController {
 	
 	public void aaa() {
 		Doctors doctors = Doctors.dao.findFirst("select * from doctors where phone=?", "15203224322");
-		String tokenKey = "TOKEN_WXA_" + "test2";
+		String tokenKey = "TOKEN_WXA_" + "test3";
 		String doctorsStr =  JSON.toJSONString(doctors);
-
 		Redis.use("pc").setex("think" + tokenKey, 7200, doctorsStr);
 		
 	}

+ 74 - 0
PacsOnline_Wechat_Doctor/src/main/java/com/zskk/util/JsonSerializer.java

@@ -0,0 +1,74 @@
+package com.zskk.util;
+
+import java.io.UnsupportedEncodingException;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.serializer.SerializerFeature;
+import com.jfinal.plugin.redis.serializer.ISerializer;
+
+public class JsonSerializer implements ISerializer {
+
+	@Override
+	public byte[] keyToBytes(String key) {
+		return this.str2Byte(key);
+	}
+
+	@Override
+	public String keyFromBytes(byte[] bytes) {
+		if(bytes == null || bytes.length == 0){
+			return null;		
+		}
+		
+		return this.byte2Str(bytes);
+	}
+
+	@Override
+	public byte[] fieldToBytes(Object field) {
+		String json = JSON.toJSONString(field);
+		return this.str2Byte(json);
+	}
+
+	@Override
+	public Object fieldFromBytes(byte[] bytes) {
+		if(bytes == null || bytes.length == 0){
+			return null;		
+		}
+		
+		String json = this.byte2Str(bytes);
+		return JSON.parseObject(json);
+	}
+
+	@Override
+	public byte[] valueToBytes(Object value) {
+		String json = JSON.toJSONString(value, SerializerFeature.WriteClassName);
+		return this.str2Byte(json);
+	}
+
+	@Override
+	public Object valueFromBytes(byte[] bytes) {
+		if(bytes == null || bytes.length == 0){
+			return null;		
+		}
+		
+		String json = this.byte2Str(bytes);
+		return JSON.parse(json);
+	}
+	
+	private String byte2Str(byte[] bytes){
+		try {
+			return new String(bytes, "UTF-8");
+		} catch (UnsupportedEncodingException e) {
+			e.printStackTrace();
+		}
+		return null;		
+	}
+	
+	private byte[] str2Byte(String str){
+		try {
+			return str.getBytes("UTF-8");
+		} catch (UnsupportedEncodingException e) {
+			e.printStackTrace();
+		}
+		return null;	
+	}
+}