CacheCloudServiceProvider.java 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package cn.com.qmth.examcloud.web.cache;
  2. import java.util.Map;
  3. import org.springframework.web.bind.annotation.RequestBody;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RequestMethod;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import com.google.common.collect.Maps;
  8. import cn.com.qmth.examcloud.api.commons.CloudService;
  9. import cn.com.qmth.examcloud.api.commons.enums.BasicDataType;
  10. import cn.com.qmth.examcloud.commons.exception.ExamCloudRuntimeException;
  11. import cn.com.qmth.examcloud.commons.exception.StatusException;
  12. import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
  13. import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
  14. import cn.com.qmth.examcloud.commons.util.JsonUtil;
  15. import cn.com.qmth.examcloud.web.support.SpringContextHolder;
  16. /**
  17. * cache
  18. *
  19. * @author WANGWEI
  20. * @date 2018年8月23日
  21. * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
  22. */
  23. @RestController
  24. @RequestMapping("cache")
  25. public class CacheCloudServiceProvider implements CloudService {
  26. private static final long serialVersionUID = -5326807830421467943L;
  27. protected static final ExamCloudLog LOG = ExamCloudLogFactory
  28. .getLog(CacheCloudServiceProvider.class);
  29. private static Map<String, ObjectCache<?>> map = Maps.newConcurrentMap();
  30. @RequestMapping(value = "refresh", method = RequestMethod.POST)
  31. public String refresh(@RequestBody RefreshCacheReq req) {
  32. String className = req.getClassName();
  33. String[] keys = req.getKeys();
  34. BasicDataType[] typeArray = req.getTypeArray();
  35. Object[] expectedKeys = new Object[keys.length];
  36. for (int i = 0; i < keys.length; i++) {
  37. String key = keys[i];
  38. BasicDataType type = typeArray[i];
  39. if (type.equals(BasicDataType.LONG)) {
  40. expectedKeys[i] = Long.parseLong(key);
  41. } else if (type.equals(BasicDataType.STRING)) {
  42. expectedKeys[i] = key;
  43. } else if (type.equals(BasicDataType.INTEGER)) {
  44. expectedKeys[i] = Integer.parseInt(key);
  45. } else {
  46. throw new ExamCloudRuntimeException("key type is not supported");
  47. }
  48. }
  49. ObjectCache<?> objectCache = map.get(className);
  50. if (null == objectCache) {
  51. try {
  52. Class<?> c = Class.forName(className);
  53. objectCache = (ObjectCache<?>) SpringContextHolder.getBean(c);
  54. map.put(className, objectCache);
  55. } catch (ClassNotFoundException e) {
  56. throw new StatusException("008001", "class not found");
  57. }
  58. }
  59. objectCache.refresh(expectedKeys);
  60. Object object = objectCache.get(expectedKeys);
  61. return JsonUtil.toJson(object);
  62. }
  63. }