123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- 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<String, ObjectCache<?>> 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);
- }
- }
|