CacheCloudServiceProvider.java 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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.commons.exception.StatusException;
  10. import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
  11. import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
  12. import cn.com.qmth.examcloud.commons.util.JsonUtil;
  13. import cn.com.qmth.examcloud.web.support.SpringContextHolder;
  14. /**
  15. * cache
  16. *
  17. * @author WANGWEI
  18. * @date 2018年8月23日
  19. * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
  20. */
  21. @RestController
  22. @RequestMapping("cache")
  23. public class CacheCloudServiceProvider implements CloudService {
  24. private static final long serialVersionUID = -5326807830421467943L;
  25. protected static final ExamCloudLog LOG = ExamCloudLogFactory
  26. .getLog(CacheCloudServiceProvider.class);
  27. private static Map<String, ObjectCache<?>> map = Maps.newConcurrentMap();
  28. @RequestMapping(value = "refresh", method = RequestMethod.POST)
  29. public String refresh(@RequestBody RefreshCacheReq req) {
  30. String className = req.getClassName();
  31. Object[] keys = req.getKeys();
  32. ObjectCache<?> objectCache = map.get(className);
  33. if (null == objectCache) {
  34. try {
  35. Class<?> c = Class.forName(className);
  36. objectCache = (ObjectCache<?>) SpringContextHolder.getBean(c);
  37. map.put(className, objectCache);
  38. } catch (ClassNotFoundException e) {
  39. throw new StatusException("008001", "class not found");
  40. }
  41. }
  42. objectCache.refresh(keys);
  43. Object object = objectCache.get(keys);
  44. return JsonUtil.toJson(object);
  45. }
  46. }