Commit 2ccc6a5480133777fc9cc133fd73c00c31aa220b

Authored by dqcer
1 parent e547aa8cd5

添加缓存机制

Showing 3 changed files with 201 additions and 6 deletions

platform-biz-service/src/main/java/com/lyms/platform/permission/model/ServiceListQuery.java View file @ 2ccc6a5
... ... @@ -106,5 +106,20 @@
106 106 public void setHospitalId(String hospitalId) {
107 107 this.hospitalId = hospitalId;
108 108 }
  109 +
  110 + @Override
  111 + public String toString() {
  112 + return "ServiceListQuery{" +
  113 + "startTime=" + startTime +
  114 + ", provinceId='" + provinceId + '\'' +
  115 + ", cityId='" + cityId + '\'' +
  116 + ", areaId='" + areaId + '\'' +
  117 + ", hospitalId='" + hospitalId.toString() + '\'' +
  118 + ", userId=" + userId +
  119 + ", endTime=" + endTime +
  120 + ", hospitalIds=" + hospitalIds +
  121 + ", serStatus='" + serStatus + '\'' +
  122 + '}';
  123 + }
109 124 }
platform-common/src/main/java/com/lyms/platform/common/utils/ExpiryMap.java View file @ 2ccc6a5
  1 +package com.lyms.platform.common.utils;
  2 +
  3 +import java.util.*;
  4 +
  5 +/**
  6 + *
  7 + * @Author dongqin
  8 + * @Description 继承至HashMap 重写了所有对外的方法,对每个key值都设置了有效期
  9 + * @Date 15:49 2019/5/18
  10 + **/
  11 +public class ExpiryMap<K, V> extends HashMap<K, V>{
  12 +
  13 + private static final long serialVersionUID = 1L;
  14 +
  15 + /**
  16 + * default expiry time 2m
  17 + */
  18 + private long EXPIRY = 1000 * 60 * 2;
  19 +
  20 + private HashMap<K, Long> expiryMap = new HashMap<>();
  21 +
  22 + public ExpiryMap(){
  23 + super();
  24 + }
  25 + public ExpiryMap(long defaultExpiryTime){
  26 + this(1 << 4, defaultExpiryTime);
  27 + }
  28 + public ExpiryMap(int initialCapacity, long defaultExpiryTime){
  29 + super(initialCapacity);
  30 + this.EXPIRY = defaultExpiryTime;
  31 + }
  32 + public V put(K key, V value) {
  33 + expiryMap.put(key, System.currentTimeMillis() + EXPIRY);
  34 + return super.put(key, value);
  35 + }
  36 + public boolean containsKey(Object key) {
  37 + return !checkExpiry(key, true) && super.containsKey(key);
  38 + }
  39 + /**
  40 + * @param key
  41 + * @param value
  42 + * @param expiryTime 键值对有效期 毫秒
  43 + * @return
  44 + */
  45 + public V put(K key, V value, long expiryTime) {
  46 + expiryMap.put(key, System.currentTimeMillis() + expiryTime);
  47 + return super.put(key, value);
  48 + }
  49 + public int size() {
  50 + return entrySet().size();
  51 + }
  52 + public boolean isEmpty() {
  53 + return entrySet().size() == 0;
  54 + }
  55 + public boolean containsValue(Object value) {
  56 + if (value == null) return Boolean.FALSE;
  57 + Set<java.util.Map.Entry<K, V>> set = super.entrySet();
  58 + Iterator<java.util.Map.Entry<K, V>> iterator = set.iterator();
  59 + while (iterator.hasNext()) {
  60 + java.util.Map.Entry<K, V> entry = iterator.next();
  61 + if(value.equals(entry.getValue())){
  62 + if(checkExpiry(entry.getKey(), false)) {
  63 + iterator.remove();
  64 + return Boolean.FALSE;
  65 + }else return Boolean.TRUE;
  66 + }
  67 + }
  68 + return Boolean.FALSE;
  69 + }
  70 + public Collection<V> values() {
  71 +
  72 + Collection<V> values = super.values();
  73 +
  74 + if(values == null || values.size() < 1) return values;
  75 +
  76 + Iterator<V> iterator = values.iterator();
  77 +
  78 + while (iterator.hasNext()) {
  79 + V next = iterator.next();
  80 + if(!containsValue(next)) iterator.remove();
  81 + }
  82 + return values;
  83 + }
  84 + public V get(Object key) {
  85 + if (key == null)
  86 + return null;
  87 + if(checkExpiry(key, true))
  88 + return null;
  89 + return super.get(key);
  90 + }
  91 + /**
  92 + *
  93 + * @Description: 是否过期
  94 + * @param key
  95 + * @return null:不存在或key为null -1:过期 存在且没过期返回value 因为过期的不是实时删除,所以稍微有点作用
  96 + */
  97 + public Object isInvalid(Object key) {
  98 + if (key == null)
  99 + return null;
  100 + if(!expiryMap.containsKey(key)){
  101 + return null;
  102 + }
  103 + long expiryTime = expiryMap.get(key);
  104 +
  105 + boolean flag = System.currentTimeMillis() > expiryTime;
  106 +
  107 + if(flag){
  108 + super.remove(key);
  109 + expiryMap.remove(key);
  110 + return -1;
  111 + }
  112 + return super.get(key);
  113 + }
  114 + public void putAll(Map<? extends K, ? extends V> m) {
  115 + for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
  116 + expiryMap.put(e.getKey(), System.currentTimeMillis() + EXPIRY);
  117 + super.putAll(m);
  118 + }
  119 + public Set<Entry<K,V>> entrySet() {
  120 + Set<java.util.Map.Entry<K, V>> set = super.entrySet();
  121 + Iterator<Entry<K, V>> iterator = set.iterator();
  122 + while (iterator.hasNext()) {
  123 + java.util.Map.Entry<K, V> entry = iterator.next();
  124 + if(checkExpiry(entry.getKey(), false)) iterator.remove();
  125 + }
  126 +
  127 + return set;
  128 + }
  129 + /**
  130 + *
  131 + * @Description: 是否过期
  132 + * @param key true 过期
  133 + * @param isRemoveSuper true super删除
  134 + * @return
  135 + */
  136 + private boolean checkExpiry(Object key, boolean isRemoveSuper){
  137 +
  138 + if(!expiryMap.containsKey(key)){
  139 + return Boolean.FALSE;
  140 + }
  141 + long expiryTime = expiryMap.get(key);
  142 +
  143 + boolean flag = System.currentTimeMillis() > expiryTime;
  144 +
  145 + if(flag){
  146 + if(isRemoveSuper)
  147 + super.remove(key);
  148 + expiryMap.remove(key);
  149 + }
  150 + return flag;
  151 + }
  152 + public static void main(String[] args) throws InterruptedException {
  153 +
  154 + ExpiryMap<String, String> map = new ExpiryMap<>(10);
  155 + map.put("test", "ankang");
  156 + map.put("test1", "ankang");
  157 + map.put("test2", "ankang", 3000);
  158 + System.out.println("test1" + map.get("test"));
  159 + Thread.sleep(1000);
  160 + System.out.println("isInvalid:" + map.isInvalid("test"));
  161 + System.out.println("size:" + map.size());
  162 + System.out.println("size:" + ((HashMap<String, String>)map).size());
  163 + for (Map.Entry<String, String> m : map.entrySet()) {
  164 + System.out.println("isInvalid:" + map.isInvalid(m.getKey()));
  165 + map.containsKey(m.getKey());
  166 + System.out.println("key:" + m.getKey() + " value:" + m.getValue());
  167 + }
  168 + System.out.println("test1" + map.get("test"));
  169 +
  170 + }
  171 +}
platform-operate-api/src/main/java/com/lyms/platform/operate/web/facade/AreaCountFacade.java View file @ 2ccc6a5
... ... @@ -96,6 +96,8 @@
96 96 @Autowired
97 97 private BabyBookbuildingService babyBookbuildingService;
98 98  
  99 + public static ExpiryMap<String, BaseResponse> cacheGetServiceList = new ExpiryMap<>(1000 * 60 * 3);
  100 +
99 101 /**
100 102 * 获取当前用户拥有的医院权限列表并和查询条件取交集
101 103 *
... ... @@ -1037,6 +1039,11 @@
1037 1039 */
1038 1040 public BaseResponse getServiceList(ServiceListQuery param) throws InterruptedException {
1039 1041 BaseResponse baseResponse = new BaseResponse();
  1042 + String string = param.toString();
  1043 + BaseResponse baseResponseVal = cacheGetServiceList.get(string);
  1044 + if (baseResponseVal != null){
  1045 + return baseResponseVal;
  1046 + }
1040 1047 setDefaultTime(param);
1041 1048  
1042 1049 HashMap<String, Object> objectHashMap = new HashMap<>(16);
... ... @@ -1080,9 +1087,9 @@
1080 1087  
1081 1088 CopyOnWriteArrayList statusValList = new CopyOnWriteArrayList();
1082 1089 param.setHospitalIds(hospitalIds);
1083   - CountDownLatch cdl = new CountDownLatch(4);
1084   - ExecutorService service = new ThreadPoolExecutor(5, 10, 3000, TimeUnit.MILLISECONDS,
1085   - new ArrayBlockingQueue<Runnable>(10),
  1090 +
  1091 + ExecutorService service = new ThreadPoolExecutor(4, 4, 9000, TimeUnit.MILLISECONDS,
  1092 + new ArrayBlockingQueue<Runnable>(1),
1086 1093 new RejectedExecutionHandler() {
1087 1094 @Override
1088 1095 public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
1089 1096  
1090 1097  
... ... @@ -1091,11 +1098,11 @@
1091 1098 }
1092 1099 });
1093 1100 for (int i =1; i < 5; i ++){
  1101 + CountDownLatch cdl = new CountDownLatch(1);
1094 1102 String serStatus = i + "" ;
1095   - service.execute(new ServiceListBySerStatusWorker(cdl, param, statusValList, serStatus, patientServiceService ));
1096   - Thread.sleep(300);
  1103 + service.execute(new ServiceListBySerStatusWorker(cdl, param, statusValList, serStatus, patientServiceService));
  1104 + cdl.await();
1097 1105 }
1098   - cdl.await();
1099 1106 service.shutdown();
1100 1107  
1101 1108 objectHashMap.put("statusNameList",statusNameList);
... ... @@ -1103,6 +1110,8 @@
1103 1110 objectHashMap.put("serviceList", copyOnWriteArrayList);
1104 1111 baseResponse.setObject(objectHashMap);
1105 1112  
  1113 +
  1114 + cacheGetServiceList.put(string, baseResponse);
1106 1115 return baseResponse;
1107 1116 }
1108 1117