From 2ccc6a5480133777fc9cc133fd73c00c31aa220b Mon Sep 17 00:00:00 2001 From: dqcer Date: Sun, 19 May 2019 20:43:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=BC=93=E5=AD=98=E6=9C=BA?= =?UTF-8?q?=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../permission/model/ServiceListQuery.java | 15 ++ .../com/lyms/platform/common/utils/ExpiryMap.java | 171 +++++++++++++++++++++ .../operate/web/facade/AreaCountFacade.java | 21 ++- 3 files changed, 201 insertions(+), 6 deletions(-) create mode 100644 platform-common/src/main/java/com/lyms/platform/common/utils/ExpiryMap.java diff --git a/platform-biz-service/src/main/java/com/lyms/platform/permission/model/ServiceListQuery.java b/platform-biz-service/src/main/java/com/lyms/platform/permission/model/ServiceListQuery.java index 1134d8f..3dd526c 100644 --- a/platform-biz-service/src/main/java/com/lyms/platform/permission/model/ServiceListQuery.java +++ b/platform-biz-service/src/main/java/com/lyms/platform/permission/model/ServiceListQuery.java @@ -106,4 +106,19 @@ public class ServiceListQuery extends BaseQuery { public void setHospitalId(String hospitalId) { this.hospitalId = hospitalId; } + + @Override + public String toString() { + return "ServiceListQuery{" + + "startTime=" + startTime + + ", provinceId='" + provinceId + '\'' + + ", cityId='" + cityId + '\'' + + ", areaId='" + areaId + '\'' + + ", hospitalId='" + hospitalId.toString() + '\'' + + ", userId=" + userId + + ", endTime=" + endTime + + ", hospitalIds=" + hospitalIds + + ", serStatus='" + serStatus + '\'' + + '}'; + } } diff --git a/platform-common/src/main/java/com/lyms/platform/common/utils/ExpiryMap.java b/platform-common/src/main/java/com/lyms/platform/common/utils/ExpiryMap.java new file mode 100644 index 0000000..31daed9 --- /dev/null +++ b/platform-common/src/main/java/com/lyms/platform/common/utils/ExpiryMap.java @@ -0,0 +1,171 @@ +package com.lyms.platform.common.utils; + +import java.util.*; + +/** + * + * @Author dongqin + * @Description 继承至HashMap 重写了所有对外的方法,对每个key值都设置了有效期 + * @Date 15:49 2019/5/18 + **/ +public class ExpiryMap extends HashMap{ + + private static final long serialVersionUID = 1L; + + /** + * default expiry time 2m + */ + private long EXPIRY = 1000 * 60 * 2; + + private HashMap expiryMap = new HashMap<>(); + + public ExpiryMap(){ + super(); + } + public ExpiryMap(long defaultExpiryTime){ + this(1 << 4, defaultExpiryTime); + } + public ExpiryMap(int initialCapacity, long defaultExpiryTime){ + super(initialCapacity); + this.EXPIRY = defaultExpiryTime; + } + public V put(K key, V value) { + expiryMap.put(key, System.currentTimeMillis() + EXPIRY); + return super.put(key, value); + } + public boolean containsKey(Object key) { + return !checkExpiry(key, true) && super.containsKey(key); + } + /** + * @param key + * @param value + * @param expiryTime 键值对有效期 毫秒 + * @return + */ + public V put(K key, V value, long expiryTime) { + expiryMap.put(key, System.currentTimeMillis() + expiryTime); + return super.put(key, value); + } + public int size() { + return entrySet().size(); + } + public boolean isEmpty() { + return entrySet().size() == 0; + } + public boolean containsValue(Object value) { + if (value == null) return Boolean.FALSE; + Set> set = super.entrySet(); + Iterator> iterator = set.iterator(); + while (iterator.hasNext()) { + java.util.Map.Entry entry = iterator.next(); + if(value.equals(entry.getValue())){ + if(checkExpiry(entry.getKey(), false)) { + iterator.remove(); + return Boolean.FALSE; + }else return Boolean.TRUE; + } + } + return Boolean.FALSE; + } + public Collection values() { + + Collection values = super.values(); + + if(values == null || values.size() < 1) return values; + + Iterator iterator = values.iterator(); + + while (iterator.hasNext()) { + V next = iterator.next(); + if(!containsValue(next)) iterator.remove(); + } + return values; + } + public V get(Object key) { + if (key == null) + return null; + if(checkExpiry(key, true)) + return null; + return super.get(key); + } + /** + * + * @Description: 是否过期 + * @param key + * @return null:不存在或key为null -1:过期 存在且没过期返回value 因为过期的不是实时删除,所以稍微有点作用 + */ + public Object isInvalid(Object key) { + if (key == null) + return null; + if(!expiryMap.containsKey(key)){ + return null; + } + long expiryTime = expiryMap.get(key); + + boolean flag = System.currentTimeMillis() > expiryTime; + + if(flag){ + super.remove(key); + expiryMap.remove(key); + return -1; + } + return super.get(key); + } + public void putAll(Map m) { + for (Map.Entry e : m.entrySet()) + expiryMap.put(e.getKey(), System.currentTimeMillis() + EXPIRY); + super.putAll(m); + } + public Set> entrySet() { + Set> set = super.entrySet(); + Iterator> iterator = set.iterator(); + while (iterator.hasNext()) { + java.util.Map.Entry entry = iterator.next(); + if(checkExpiry(entry.getKey(), false)) iterator.remove(); + } + + return set; + } + /** + * + * @Description: 是否过期 + * @param key true 过期 + * @param isRemoveSuper true super删除 + * @return + */ + private boolean checkExpiry(Object key, boolean isRemoveSuper){ + + if(!expiryMap.containsKey(key)){ + return Boolean.FALSE; + } + long expiryTime = expiryMap.get(key); + + boolean flag = System.currentTimeMillis() > expiryTime; + + if(flag){ + if(isRemoveSuper) + super.remove(key); + expiryMap.remove(key); + } + return flag; + } + public static void main(String[] args) throws InterruptedException { + + ExpiryMap map = new ExpiryMap<>(10); + map.put("test", "ankang"); + map.put("test1", "ankang"); + map.put("test2", "ankang", 3000); + System.out.println("test1" + map.get("test")); + Thread.sleep(1000); + System.out.println("isInvalid:" + map.isInvalid("test")); + System.out.println("size:" + map.size()); + System.out.println("size:" + ((HashMap)map).size()); + for (Map.Entry m : map.entrySet()) { + System.out.println("isInvalid:" + map.isInvalid(m.getKey())); + map.containsKey(m.getKey()); + System.out.println("key:" + m.getKey() + " value:" + m.getValue()); + } + System.out.println("test1" + map.get("test")); + + } +} diff --git a/platform-operate-api/src/main/java/com/lyms/platform/operate/web/facade/AreaCountFacade.java b/platform-operate-api/src/main/java/com/lyms/platform/operate/web/facade/AreaCountFacade.java index 1aeb3f0..82c9403 100644 --- a/platform-operate-api/src/main/java/com/lyms/platform/operate/web/facade/AreaCountFacade.java +++ b/platform-operate-api/src/main/java/com/lyms/platform/operate/web/facade/AreaCountFacade.java @@ -96,6 +96,8 @@ public class AreaCountFacade { @Autowired private BabyBookbuildingService babyBookbuildingService; + public static ExpiryMap cacheGetServiceList = new ExpiryMap<>(1000 * 60 * 3); + /** * 获取当前用户拥有的医院权限列表并和查询条件取交集 * @@ -1037,6 +1039,11 @@ public class AreaCountFacade { */ public BaseResponse getServiceList(ServiceListQuery param) throws InterruptedException { BaseResponse baseResponse = new BaseResponse(); + String string = param.toString(); + BaseResponse baseResponseVal = cacheGetServiceList.get(string); + if (baseResponseVal != null){ + return baseResponseVal; + } setDefaultTime(param); HashMap objectHashMap = new HashMap<>(16); @@ -1080,9 +1087,9 @@ public class AreaCountFacade { CopyOnWriteArrayList statusValList = new CopyOnWriteArrayList(); param.setHospitalIds(hospitalIds); - CountDownLatch cdl = new CountDownLatch(4); - ExecutorService service = new ThreadPoolExecutor(5, 10, 3000, TimeUnit.MILLISECONDS, - new ArrayBlockingQueue(10), + + ExecutorService service = new ThreadPoolExecutor(4, 4, 9000, TimeUnit.MILLISECONDS, + new ArrayBlockingQueue(1), new RejectedExecutionHandler() { @Override public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { @@ -1091,11 +1098,11 @@ public class AreaCountFacade { } }); for (int i =1; i < 5; i ++){ + CountDownLatch cdl = new CountDownLatch(1); String serStatus = i + "" ; - service.execute(new ServiceListBySerStatusWorker(cdl, param, statusValList, serStatus, patientServiceService )); - Thread.sleep(300); + service.execute(new ServiceListBySerStatusWorker(cdl, param, statusValList, serStatus, patientServiceService)); + cdl.await(); } - cdl.await(); service.shutdown(); objectHashMap.put("statusNameList",statusNameList); @@ -1103,6 +1110,8 @@ public class AreaCountFacade { objectHashMap.put("serviceList", copyOnWriteArrayList); baseResponse.setObject(objectHashMap); + + cacheGetServiceList.put(string, baseResponse); return baseResponse; } -- 1.8.3.1