Commit 69912848 authored by yanzg's avatar yanzg

将源码打包进jar包

parent a54e2871
...@@ -90,11 +90,7 @@ public class MemoryCache<T> { ...@@ -90,11 +90,7 @@ public class MemoryCache<T> {
if (this.isAutoClear && this.clearSecond < 1) { if (this.isAutoClear && this.clearSecond < 1) {
return data; return data;
} }
MemoryCacheItem<T> item = cache.containsKey(key) ? cache.get(key) : null; MemoryCacheItem<T> item = cache.computeIfAbsent(key, k -> new MemoryCacheItem<>());
if (item == null) {
item = new MemoryCacheItem<T>();
cache.put(key, item);
}
item.setData(data); item.setData(data);
if (StringHelper.isEmpty(item.getName())) { if (StringHelper.isEmpty(item.getName())) {
item.setName(key); item.setName(key);
...@@ -125,20 +121,20 @@ public class MemoryCache<T> { ...@@ -125,20 +121,20 @@ public class MemoryCache<T> {
* *
* @param key 关键字 * @param key 关键字
* @param defData 默认数据 * @param defData 默认数据
* @return * @return 读取到的数据
*/ */
public synchronized T get(String key, T defData) { public synchronized T get(String key, T defData) {
if (this.isAutoClear && this.clearSecond < 1) { if (this.isAutoClear && this.clearSecond < 1) {
return defData; return defData;
} }
MemoryCacheItem<T> item = cache.getOrDefault(key, new MemoryCacheItem<T>()); MemoryCacheItem<T> item = cache.getOrDefault(key, new MemoryCacheItem<>());
if (StringHelper.isEmpty(item.getName())) { if (StringHelper.isEmpty(item.getName())) {
item.setName(key); item.setName(key);
item.setData(defData); item.setData(defData);
cache.put(key, item); cache.put(key, item);
} else { } else {
if (defData instanceof MemoryCache) { if (defData instanceof MemoryCache<?>) {
((MemoryCache) defData).close(); ((MemoryCache<?>) defData).close();
} }
} }
this.getActive(item); this.getActive(item);
...@@ -147,6 +143,8 @@ public class MemoryCache<T> { ...@@ -147,6 +143,8 @@ public class MemoryCache<T> {
/** /**
* 在获取时重新激活数据 * 在获取时重新激活数据
*
* @param item 当前数据
*/ */
private void getActive(MemoryCacheItem<T> item) { private void getActive(MemoryCacheItem<T> item) {
if (isGetActive) { if (isGetActive) {
...@@ -165,8 +163,8 @@ public class MemoryCache<T> { ...@@ -165,8 +163,8 @@ public class MemoryCache<T> {
return null; return null;
} }
T data = item.getData(); T data = item.getData();
if (data instanceof MemoryCache) { if (data instanceof MemoryCache<?>) {
((MemoryCache) data).close(); ((MemoryCache<?>) data).close();
} }
return data; return data;
} }
...@@ -201,7 +199,7 @@ public class MemoryCache<T> { ...@@ -201,7 +199,7 @@ public class MemoryCache<T> {
continue; continue;
} }
// 判断是否过期 // 判断是否过期
if (now.getTime() - itemValue.getDate().getTime() > this.clearSecond * 1000) { if (now.getTime() - itemValue.getDate().getTime() > this.clearSecond * 1000L) {
this.remove(key); this.remove(key);
} }
} }
...@@ -218,11 +216,11 @@ public class MemoryCache<T> { ...@@ -218,11 +216,11 @@ public class MemoryCache<T> {
/** /**
* 获取所有的关键字 * 获取所有的关键字
* *
* @return * @return 获取值集合
*/ */
public synchronized Collection<T> getValues() { public synchronized Collection<T> getValues() {
List<T> list = new ArrayList<>(); List<T> list = new ArrayList<>();
for(MemoryCacheItem<T> item:cache.values()){ for (MemoryCacheItem<T> item : cache.values()) {
list.add(item.getData()); list.add(item.getData());
} }
return list; return list;
...@@ -231,7 +229,7 @@ public class MemoryCache<T> { ...@@ -231,7 +229,7 @@ public class MemoryCache<T> {
/** /**
* 获取所有的关键字 * 获取所有的关键字
* *
* @return * @return 获取键集合
*/ */
public synchronized String[] getKeys() { public synchronized String[] getKeys() {
String[] items = new String[this.cache.size()]; String[] items = new String[this.cache.size()];
...@@ -240,8 +238,6 @@ public class MemoryCache<T> { ...@@ -240,8 +238,6 @@ public class MemoryCache<T> {
/** /**
* 析构函数 * 析构函数
*
* @throws Throwable
*/ */
@Override @Override
protected void finalize() { protected void finalize() {
......
...@@ -65,7 +65,8 @@ public class Timeout<T extends Object> { ...@@ -65,7 +65,8 @@ public class Timeout<T extends Object> {
* 是否达到最大时间 * 是否达到最大时间
* *
* @param maxTime 最大时间 * @param maxTime 最大时间
* @return * @param prevMaxTime 上次最大时间
* @return 是否达到最大时间
*/ */
public boolean isMaxTime(long maxTime, long prevMaxTime) { public boolean isMaxTime(long maxTime, long prevMaxTime) {
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
......
...@@ -2,6 +2,7 @@ package com.yanzuoguang.cloud.aop.log; ...@@ -2,6 +2,7 @@ package com.yanzuoguang.cloud.aop.log;
import com.yanzuoguang.cloud.CloudConfig; import com.yanzuoguang.cloud.CloudConfig;
import com.yanzuoguang.cloud.aop.Timeout; import com.yanzuoguang.cloud.aop.Timeout;
import com.yanzuoguang.util.cache.MemoryCache;
import com.yanzuoguang.util.helper.DateHelper; import com.yanzuoguang.util.helper.DateHelper;
import com.yanzuoguang.util.helper.StringHelper; import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.thread.ThreadNext; import com.yanzuoguang.util.thread.ThreadNext;
...@@ -10,7 +11,6 @@ import org.springframework.beans.factory.InitializingBean; ...@@ -10,7 +11,6 @@ import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.*; import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
/** /**
* 当前线程日志编写 * 当前线程日志编写
...@@ -19,6 +19,11 @@ import java.util.concurrent.ConcurrentHashMap; ...@@ -19,6 +19,11 @@ import java.util.concurrent.ConcurrentHashMap;
*/ */
@Component @Component
public class LogLocal implements ThreadNext.Next, InitializingBean { public class LogLocal implements ThreadNext.Next, InitializingBean {
/**
* 一个请求最多保留15分钟
*/
public static final int CLEAR_CACHE = 15 * 60;
/** /**
* 日志基础 * 日志基础
*/ */
...@@ -33,7 +38,7 @@ public class LogLocal implements ThreadNext.Next, InitializingBean { ...@@ -33,7 +38,7 @@ public class LogLocal implements ThreadNext.Next, InitializingBean {
/** /**
* 缓存队列 * 缓存队列
*/ */
protected volatile Map<String, Timeout<LogVo>> cache = new ConcurrentHashMap<>(); protected volatile MemoryCache<Timeout<LogVo>> cache = new MemoryCache<>(CLEAR_CACHE);
public LogLocal(LogBase logBase, CloudConfig cloudConfig, List<LogFilter> logFilters) { public LogLocal(LogBase logBase, CloudConfig cloudConfig, List<LogFilter> logFilters) {
this.logBase = logBase; this.logBase = logBase;
...@@ -94,30 +99,37 @@ public class LogLocal implements ThreadNext.Next, InitializingBean { ...@@ -94,30 +99,37 @@ public class LogLocal implements ThreadNext.Next, InitializingBean {
if (isLog) { if (isLog) {
this.remove(log); this.remove(log);
} else { } else {
Timeout<LogVo> timeout = cache.get(log.getLogId()); this.result(log, status, result, true);
result(timeout, log, status, result, true);
} }
} }
/** /**
* 写入状态 * 写入状态
* *
* @param timeout 超时
* @param log 日志对象 * @param log 日志对象
* @param status 状态 * @param status 状态
* @param result 结果 * @param result 结果
* @param write 写入 * @param write 写入
*/ */
private void result(Timeout<LogVo> timeout, LogVo log, String status, String result, boolean write) { private void result(LogVo log, String status, String result, boolean write) {
if (log == null || StringHelper.isEmpty(log.getLogId())) {
return;
}
Timeout<LogVo> timeout;
// 判断是否延时结果
boolean isMaxTime = StringHelper.compare(status, MAX_TIME);
if (isMaxTime) {
timeout = cache.get(log.getLogId());
} else {
timeout = cache.remove(log.getLogId());
}
if (timeout != null) { if (timeout != null) {
long useTime = System.currentTimeMillis() - timeout.getStart(); long useTime = System.currentTimeMillis() - timeout.getStart();
log.setUseTime((int) useTime); log.setUseTime((int) useTime);
} }
log.setStatus(status); log.setStatus(status);
log.setResult(result); log.setResult(result);
if (!StringHelper.compare(status, MAX_TIME)) {
cache.remove(log.getLogId());
}
if (write) { if (write) {
logBase.addLog(log); logBase.addLog(log);
} }
...@@ -129,24 +141,23 @@ public class LogLocal implements ThreadNext.Next, InitializingBean { ...@@ -129,24 +141,23 @@ public class LogLocal implements ThreadNext.Next, InitializingBean {
* @param log 删除日志 * @param log 删除日志
*/ */
private void remove(LogVo log) { private void remove(LogVo log) {
Timeout<LogVo> timeout = cache.get(log.getLogId()); this.result(log, StringHelper.EMPTY, StringHelper.EMPTY, false);
result(timeout, log, StringHelper.EMPTY, StringHelper.EMPTY, false);
} }
/** /**
* 记录超时 * 记录超时
* *
* @return 超时对象 * @param timeout 超时对象
*/ */
private void writeTimeout(Timeout<LogVo> timeout) { private void writeTimeout(Timeout<LogVo> timeout) {
result(timeout, timeout.getData(), MAX_TIME, MAX_TIME_NAME, true); this.result(timeout.getData(), MAX_TIME, MAX_TIME_NAME, true);
} }
/** /**
* 是否属于日志服务 * 是否属于日志服务
* *
* @param keys * @param keys 是否需要写入
* @return * @return 是否属于日志服务
*/ */
public boolean isLog(String... keys) { public boolean isLog(String... keys) {
List<String> list = new ArrayList<>(); List<String> list = new ArrayList<>();
...@@ -169,19 +180,19 @@ public class LogLocal implements ThreadNext.Next, InitializingBean { ...@@ -169,19 +180,19 @@ public class LogLocal implements ThreadNext.Next, InitializingBean {
* 执行下一个函数,出现异常会继续执行 * 执行下一个函数,出现异常会继续执行
* *
* @return 是否继续执行 * @return 是否继续执行
* @throws Exception 异常信息
*/ */
@Override @Override
public boolean next() throws Exception { public boolean next() {
List<String> keys = new ArrayList<>(); Collection<Timeout<LogVo>> values = cache.getValues();
keys.addAll(cache.keySet()); for (Timeout<LogVo> timeout : values) {
for (String key : keys) { if (timeout == null) {
Timeout<LogVo> timeout = cache.get(key);
if (timeout == null || !timeout.isMaxTime(this.cloudConfig.getLogTimeMax(), this.cloudConfig.getLogTimeSplit())) {
continue; continue;
} }
// 判断是否达到超时时间
if (timeout.isMaxTime(this.cloudConfig.getLogTimeMax(), this.cloudConfig.getLogTimeSplit())) {
writeTimeout(timeout); writeTimeout(timeout);
} }
}
return true; 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