Commit 69912848 authored by yanzg's avatar yanzg

将源码打包进jar包

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