Ver código fonte

。。。。

WANG 6 anos atrás
pai
commit
4c25015fd2

+ 92 - 0
examcloud-core-basic-service/src/main/java/cn/com/qmth/examcloud/core/basic/service/cache/SystemPropertyCache.java

@@ -0,0 +1,92 @@
+package cn.com.qmth.examcloud.core.basic.service.cache;
+
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import cn.com.qmth.examcloud.api.commons.enums.BasicDataType;
+import cn.com.qmth.examcloud.commons.exception.StatusException;
+import cn.com.qmth.examcloud.commons.util.DateUtil;
+import cn.com.qmth.examcloud.commons.util.DateUtil.DatePatterns;
+import cn.com.qmth.examcloud.core.basic.dao.SystemPropertyRepo;
+import cn.com.qmth.examcloud.core.basic.dao.entity.SystemPropertyEntity;
+import cn.com.qmth.examcloud.core.basic.service.SystemPropertyService;
+import cn.com.qmth.examcloud.support.cache.bean.SysPropertyCacheBean;
+import cn.com.qmth.examcloud.web.cache.RandomObjectRedisCache;
+
+@Service
+public class SystemPropertyCache extends RandomObjectRedisCache<SysPropertyCacheBean> {
+
+	@Autowired
+	SystemPropertyRepo systemPropertyRepo;
+
+	@Autowired
+	SystemPropertyService systemPropertyService;
+
+	@Override
+	public SysPropertyCacheBean loadFromResource(Object... keys) {
+		String propKey = (String) keys[0];
+
+		SystemPropertyEntity sysConf = systemPropertyRepo.getOne(propKey);
+		if (null == sysConf) {
+			throw new StatusException("350002", "key is wrong");
+		}
+
+		String value = sysConf.getPropValue();
+		BasicDataType propValueType = sysConf.getPropValueType();
+
+		Object propValue = null;
+
+		if (StringUtils.isBlank(value)) {
+			propValue = null;
+		} else if (propValueType.equals(BasicDataType.STRING)) {
+			propValue = value;
+		} else if (propValueType.equals(BasicDataType.LONG)) {
+			try {
+				propValue = Long.parseLong(value);
+			} catch (NumberFormatException e) {
+				throw new StatusException("350002", "value is wrong");
+			}
+		} else if (propValueType.equals(BasicDataType.INTEGER)) {
+			try {
+				propValue = Integer.parseInt(value);
+			} catch (NumberFormatException e) {
+				throw new StatusException("350002", "value is wrong");
+			}
+		} else if (propValueType.equals(BasicDataType.DATE)) {
+			try {
+				propValue = DateUtil.parse(value, DatePatterns.CHINA_DEFAULT);
+			} catch (Exception e) {
+				throw new StatusException("350002", "value is wrong");
+			}
+		} else if (propValueType.equals(BasicDataType.BOOLEAN)) {
+			if ("true".equals(value)) {
+				propValue = true;
+			} else if ("false".equals(value)) {
+				propValue = false;
+			} else {
+				throw new StatusException("350002", "value is wrong. value=" + value);
+			}
+		} else {
+			throw new StatusException("350003", "data type is undefined");
+		}
+
+		SysPropertyCacheBean b = new SysPropertyCacheBean();
+		b.setValueType(propValueType);
+		b.setValue(propValue);
+
+		return b;
+	}
+
+	@Override
+	protected String getKeyPrefix() {
+		return "$_SYS_PROP:";
+	}
+
+	@Override
+	protected int getTimeout() {
+		// 2分钟
+		return 60 * 2;
+	}
+
+}

+ 7 - 45
examcloud-core-basic-service/src/main/java/cn/com/qmth/examcloud/core/basic/service/impl/SystemPropertyServiceImpl.java

@@ -11,6 +11,8 @@ import cn.com.qmth.examcloud.commons.util.DateUtil.DatePatterns;
 import cn.com.qmth.examcloud.core.basic.dao.SystemPropertyRepo;
 import cn.com.qmth.examcloud.core.basic.dao.entity.SystemPropertyEntity;
 import cn.com.qmth.examcloud.core.basic.service.SystemPropertyService;
+import cn.com.qmth.examcloud.core.basic.service.cache.SystemPropertyCache;
+import cn.com.qmth.examcloud.support.cache.bean.SysPropertyCacheBean;
 
 /**
  * 系统配置服务
@@ -25,6 +27,9 @@ public class SystemPropertyServiceImpl implements SystemPropertyService {
 	@Autowired
 	SystemPropertyRepo systemPropertyRepo;
 
+	@Autowired
+	SystemPropertyCache systemPropertyCache;
+
 	@Override
 	public void set(String key, String value) {
 		if (StringUtils.isBlank(key)) {
@@ -75,51 +80,8 @@ public class SystemPropertyServiceImpl implements SystemPropertyService {
 
 	@Override
 	public Object get(String key) {
-		if (StringUtils.isBlank(key)) {
-			throw new StatusException("350001", "key is blank");
-		}
-
-		SystemPropertyEntity sysConf = systemPropertyRepo.getOne(key);
-		if (null == sysConf) {
-			throw new StatusException("350002", "key is wrong");
-		}
-
-		String value = sysConf.getPropValue();
-		BasicDataType dataType = sysConf.getPropValueType();
-
-		if (StringUtils.isBlank(value)) {
-			return null;
-		} else if (dataType.equals(BasicDataType.STRING)) {
-			return value;
-		} else if (dataType.equals(BasicDataType.LONG)) {
-			try {
-				return Long.parseLong(value);
-			} catch (NumberFormatException e) {
-				throw new StatusException("350002", "value is wrong");
-			}
-		} else if (dataType.equals(BasicDataType.INTEGER)) {
-			try {
-				return Integer.parseInt(value);
-			} catch (NumberFormatException e) {
-				throw new StatusException("350002", "value is wrong");
-			}
-		} else if (dataType.equals(BasicDataType.DATE)) {
-			try {
-				return DateUtil.parse(value, DatePatterns.CHINA_DEFAULT);
-			} catch (Exception e) {
-				throw new StatusException("350002", "value is wrong");
-			}
-		} else if (dataType.equals(BasicDataType.BOOLEAN)) {
-			if ("true".equals(value)) {
-				return true;
-			} else if ("false".equals(value)) {
-				return false;
-			} else {
-				throw new StatusException("350002", "value is wrong. value=" + value);
-			}
-		} else {
-			throw new StatusException("350003", "dataType is undefined");
-		}
+		SysPropertyCacheBean sysPropertyCacheBean = systemPropertyCache.get(key);
+		return sysPropertyCacheBean.getValue();
 	}
 
 }