Commit c49b7b0e authored by yanzg's avatar yanzg

修复等待时间

parent 0ed3022a
......@@ -2,6 +2,8 @@ package com.yanzuoguang.cloud.aop;
import com.yanzuoguang.util.base.CollectionString;
import com.yanzuoguang.util.cache.MemoryCache;
import com.yanzuoguang.util.helper.DateHelper;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.thread.ThreadNext;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
......@@ -18,13 +20,38 @@ import java.util.List;
@Component
public class AspectLogTime implements ThreadNext.Next, InitializingBean {
private final MemoryCache<AspectUrlCountVo> memoryCache = new MemoryCache<>();
/**
* 今日缓存,超过时间会自动清空
*/
private final MemoryCache<AspectUrlCountVo> todayMemoryCache = new MemoryCache<>();
/**
* 今日日期
*/
private String today;
/**
* 今日时间
*/
private String todayTime;
@Override
public void afterPropertiesSet() {
ThreadNext.start(this, "aspectLogTime");
}
private void initToday() {
String today = DateHelper.getToday();
if (!StringHelper.compare(this.today, today)) {
synchronized (todayMemoryCache) {
if (!StringHelper.compare(this.today, today)) {
todayMemoryCache.clear();
this.today = today;
this.todayTime = DateHelper.getNow();
}
}
}
}
/**
* 获取缓存对象
*
......@@ -32,17 +59,21 @@ public class AspectLogTime implements ThreadNext.Next, InitializingBean {
* @return 缓存对象
*/
private AspectUrlCountVo getCount(String url) {
AspectUrlCountVo ret = memoryCache.get(url);
// 初始化日期
initToday();
// 缓存中获取对象
AspectUrlCountVo ret = todayMemoryCache.get(url);
if (ret != null) {
return ret;
}
synchronized (memoryCache) {
ret = memoryCache.get(url);
// 缓存中不存在则创建对象
synchronized (todayMemoryCache) {
ret = todayMemoryCache.get(url);
if (ret != null) {
return ret;
}
ret = new AspectUrlCountVo(url);
memoryCache.put(url, ret);
todayMemoryCache.put(url, ret);
return ret;
}
}
......@@ -71,12 +102,12 @@ public class AspectLogTime implements ThreadNext.Next, InitializingBean {
@Override
public boolean next() {
List<AspectUrlCountVo> rowList = new ArrayList<>(memoryCache.getValues());
List<AspectUrlCountVo> rowList = new ArrayList<>(todayMemoryCache.getValues());
if (rowList.isEmpty()) {
return true;
}
rowList.sort(Comparator.comparingInt(AspectUrlCountVo::getStartCount));
System.out.println(CollectionString.getCollectionString("接口执行次数:", rowList));
System.out.println(CollectionString.getCollectionString("接口从" + todayTime + "时执行次数:", rowList));
return true;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment