package cn.com.qmth.examcloud.web.cache; import java.util.Map; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.google.common.collect.Maps; import cn.com.qmth.examcloud.api.commons.CloudService; import cn.com.qmth.examcloud.api.commons.enums.BasicDataType; import cn.com.qmth.examcloud.commons.exception.ExamCloudRuntimeException; import cn.com.qmth.examcloud.commons.exception.StatusException; import cn.com.qmth.examcloud.commons.logging.ExamCloudLog; import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory; import cn.com.qmth.examcloud.commons.util.JsonUtil; import cn.com.qmth.examcloud.web.support.SpringContextHolder; /** * cache * * @author WANGWEI * @date 2018年8月23日 * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved. */ @RestController @RequestMapping("cache") public class CacheCloudServiceProvider implements CloudService { private static final long serialVersionUID = -5326807830421467943L; protected static final ExamCloudLog LOG = ExamCloudLogFactory .getLog(CacheCloudServiceProvider.class); private static Map> map = Maps.newConcurrentMap(); @RequestMapping(value = "refresh", method = RequestMethod.POST) public String refresh(@RequestBody RefreshCacheReq req) { String className = req.getClassName(); String[] keys = req.getKeys(); BasicDataType[] typeArray = req.getTypeArray(); Object[] expectedKeys = new Object[keys.length]; for (int i = 0; i < keys.length; i++) { String key = keys[i]; BasicDataType type = typeArray[i]; if (type.equals(BasicDataType.LONG)) { expectedKeys[i] = Long.parseLong(key); } else if (type.equals(BasicDataType.STRING)) { expectedKeys[i] = key; } else if (type.equals(BasicDataType.INTEGER)) { expectedKeys[i] = Integer.parseInt(key); } else { throw new ExamCloudRuntimeException("key type is not supported"); } } ObjectCache objectCache = map.get(className); if (null == objectCache) { try { Class c = Class.forName(className); objectCache = (ObjectCache) SpringContextHolder.getBean(c); map.put(className, objectCache); } catch (ClassNotFoundException e) { throw new StatusException("008001", "class not found"); } } objectCache.refresh(expectedKeys); Object object = objectCache.get(expectedKeys); return JsonUtil.toJson(object); } }