v1.0.3一些bug修改和一些模块重构优化

This commit is contained in:
userA
2023-04-26 00:52:14 +08:00
parent eba9c32c8f
commit 1233ca8a00
22 changed files with 389 additions and 106 deletions

View File

@@ -31,7 +31,7 @@ public class FileCache <T extends ConfigFile>{
private ConcurrentHashMap<String,Object> jsonFile; //文件内容缓存
private static final int MAX_WRITE_BUFFER_LIMIT = 4096; //最大写入缓存上线
private static int MAX_WRITE_BUFFER_LIMIT = 4096; //最大写入缓存上线
private AtomicInteger writeByte; //当前写入的字节数
@@ -46,7 +46,15 @@ public class FileCache <T extends ConfigFile>{
init(configFile,10);
}
public FileCache(T configFile,long autoSyncTime)throws FileCacheException {
/**
* 构造方法
* @param configFile 指定的配置文件
* @param autoSyncTime 自动刷新时间
* @param maxWriteBufferLimit 写入上限
* @throws FileCacheException
*/
public FileCache(T configFile,long autoSyncTime,int maxWriteBufferLimit)throws FileCacheException {
MAX_WRITE_BUFFER_LIMIT = maxWriteBufferLimit;
init(configFile,autoSyncTime);
}
@@ -143,7 +151,7 @@ public class FileCache <T extends ConfigFile>{
//元素添加
int index = Integer.parseInt(keys[keys.length-1]);
if(index==-1){
((JSONArray) temp).add(index,value);
((JSONArray) temp).add(value);
}else{
String oldValue = ((JSONArray) temp).get(index).toString();
value = isAppend?oldValue+value.toString():value;
@@ -211,7 +219,6 @@ public class FileCache <T extends ConfigFile>{
return jsonFile.get(key);
}
/**
* 清除已写入的字节数记录
*/
@@ -285,4 +292,20 @@ public class FileCache <T extends ConfigFile>{
}
}
@Override
public int hashCode() {
return Objects.hash(configFile);
}
@Override
public boolean equals(Object obj) {
if(obj instanceof FileCache){
if(((FileCache) obj).getFileName().equals(this.getFileName())){
return true;
}else if(obj.hashCode() == this.hashCode()){
return true;
}
}
return false;
}
}

View File

@@ -7,6 +7,7 @@ import org.slf4j.LoggerFactory;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
@@ -27,14 +28,16 @@ public class FileCacheManager {
private Logger logger = LoggerFactory.getLogger(FileCacheManager.class);
private final List<FileCache> fileCaches;
private long sleepTime; //睡眠时间
private AtomicLong sleepTime; //睡眠时间
private ExecutorService watchPool; //巡逻线程
private ExecutorService autoSyncer; //生产者线程
public FileCacheManager(List<FileCache> fileCaches){
this.fileCaches = fileCaches;
private volatile Watcher watcher;
protected FileCacheManager(List<FileCache> fileCaches){
this.fileCaches = new CopyOnWriteArrayList<>(fileCaches);
initSleepTime();
this.watchPool = Executors.newSingleThreadExecutor();
this.autoSyncer = Executors.newFixedThreadPool(fileCaches.size());
@@ -48,11 +51,28 @@ public class FileCacheManager {
fileCaches.forEach(item->{
minSleepTime.set(Long.min(minSleepTime.get(), item.getSyncTime()));
});
this.sleepTime = minSleepTime.get();
this.sleepTime = minSleepTime;
}
public void start(){
this.watchPool.submit(new Watcher());
if(!fileCaches.isEmpty()){
if(watcher==null){
synchronized (FileCacheManager.class){
if(watcher==null){
watcher = new Watcher();
this.watchPool.submit(watcher);
}
}
}
}
}
public boolean addFileCache(FileCache fileCache){
if (this.fileCaches.indexOf(fileCache)==-1) {
fileCaches.add(fileCache);
initSleepTime();
}
return false;
}
class Watcher implements Runnable{
@@ -71,9 +91,9 @@ public class FileCacheManager {
}
}
now -= TimeUtil.getCurrentSecond();
if(now<sleepTime){
if(now<sleepTime.get()){
try {
Thread.sleep((sleepTime-now)*1000);
Thread.sleep((sleepTime.get()-now)*1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}

View File

@@ -0,0 +1,30 @@
package org.example.cache;
import org.example.constpool.GlobalFileCache;
import java.util.ArrayList;
import java.util.List;
/**
* @author Genius
* @date 2023/04/25 22:14
**/
//FileCacheManager单例实体类
public class FileCacheManagerInstance {
//获取全局的一个fileCaches
private static List<FileCache> fileCaches = GlobalFileCache.fileCaches;
private static volatile FileCacheManager Instance;
public static FileCacheManager getInstance(){
if(Instance==null){
synchronized (FileCacheManagerInstance.class){
if(Instance==null){
Instance = new FileCacheManager(fileCaches);
}
}
}
return Instance;
}
}

View File

@@ -0,0 +1,31 @@
package org.example.constpool;
import org.example.cache.FileCache;
import org.example.exception.FileCacheException;
import org.example.pojo.configfile.ModuleSrcConfigFile;
import java.util.List;
/**
* @author Genius
* @date 2023/04/25 23:03
**/
/**
* 全局文件缓存池,用于存放全局文件缓存,便于跨模块调用
*/
public class GlobalFileCache {
public static FileCache ModuleSrcConfigFile;
static {
try {
ModuleSrcConfigFile = new FileCache(new ModuleSrcConfigFile());
} catch (FileCacheException e) {
throw new RuntimeException(e);
}
}
public static List<FileCache> fileCaches
= List.of(ModuleSrcConfigFile);
}

View File

@@ -1,6 +1,6 @@
package org.example.init;
import org.example.bean.ModuleSrcConfigFile;
import org.example.pojo.configfile.ModuleSrcConfigFile;
import org.example.util.FileUtil;
import org.example.util.JsonFileUtil;
import org.slf4j.Logger;

View File

@@ -0,0 +1,14 @@
package org.example.pojo;
import org.example.common.ConfigFile;
/**
* @author Genius
* @date 2023/04/26 00:12
**/
public class CommonConfigFile extends ConfigFile {
public CommonConfigFile(String filePath, String fileName, Object data) {
super(filePath, fileName, data);
}
}

View File

@@ -1,4 +1,4 @@
package org.example.bean;
package org.example.pojo.configfile;
import org.example.common.ConfigFile;
import org.example.constpool.ConstPool;

View File

@@ -1,10 +1,65 @@
{
"name": "ChopperBot",
"description": "A bot for the ChopperMC server",
"version": "1.0.0",
"module": {
"type": ["Account","Creeper","File","Hot","Publish","Section","SectionWork","VideoSection"],
"main": "console",
"ui": "console-ui"
}
}
"data":[
{
"age":0,
"major":"major0",
"name":"0",
"school":"jsu0"
},
{
"age":1,
"major":"major1",
"name":"1",
"school":"jsu1"
},
{
"age":2,
"major":"major2",
"name":"2",
"school":"jsu2"
},
{
"age":3,
"major":"major3",
"name":"3",
"school":"jsu3"
},
{
"age":4,
"major":"major4",
"name":"4",
"school":"jsu4"
},
{
"age":5,
"major":"major5",
"name":"5",
"school":"jsu5"
},
{
"age":6,
"major":"major6",
"name":"6",
"school":"jsu6"
},
{
"age":7,
"major":"major7",
"name":"7",
"school":"jsu7"
},
{
"age":8,
"major":"major8",
"name":"8",
"school":"jsu8"
},
{
"age":9,
"major":"major9",
"name":"9",
"school":"jsu9"
}
],
"updateTime":"2023-04-26 00:24:47"
}

View File

@@ -1,19 +1,16 @@
package org.example.cache;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPObject;
import org.example.bean.ModuleSrcConfigFile;
import org.example.bean.Student;
import org.example.cache.FileCache;
import org.example.common.ConfigFile;
import org.example.constpool.GlobalFileCache;
import org.example.pojo.CommonConfigFile;
import org.example.pojo.Student;
import org.example.pojo.configfile.ModuleSrcConfigFile;
import org.example.exception.FileCacheException;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import static org.apache.logging.log4j.message.MapMessage.MapFormat.JSON;
import java.util.concurrent.ConcurrentLinkedQueue;
/**
* @author Genius
@@ -21,6 +18,23 @@ import static org.apache.logging.log4j.message.MapMessage.MapFormat.JSON;
**/
public class FileCacheTest {
static FileCache cache;
static{
try {
cache = new FileCache<>(
new CommonConfigFile(
"E:\\Project\\ChopperBot\\FileModule\\src\\main\\resources\\",
"test.json",
new ConcurrentLinkedQueue<Student>()
)
);
FileCacheManagerInstance.getInstance().addFileCache(cache);
} catch (FileCacheException e) {
throw new RuntimeException(e);
}
}
@Test
public void TestFileCache() throws FileCacheException, InterruptedException {
@@ -55,55 +69,59 @@ public class FileCacheTest {
@Test
public void TestAppend() throws FileCacheException, InterruptedException {
ModuleSrcConfigFile moduleSrcConfigFile = new ModuleSrcConfigFile();
FileCache fileCache = new FileCache(moduleSrcConfigFile);
FileCacheManager manager = new FileCacheManager(List.of(fileCache));
manager.start();
FileCacheManagerInstance.getInstance().start();
//追加数组的某个元素
for(int i=0;i<10;i++){
fileCache.append(i,"sectionwork","src","0");
GlobalFileCache.ModuleSrcConfigFile.append(i,"sectionwork","src","0");
}
//追加数组
for(int i=0;i<10;i++){
fileCache.append(i,"sectionwork","src","-1");
GlobalFileCache.ModuleSrcConfigFile.append(i,"sectionwork","src","-1");
}
//追加类中的元素
for(int i=0;i<10;i++){
fileCache.append(i,"barrage","src");
GlobalFileCache.ModuleSrcConfigFile.append(i,"barrage","src");
}
Thread.sleep(500000);
}
@Test
public void TestWrite() throws FileCacheException,InterruptedException{
ModuleSrcConfigFile moduleSrcConfigFile = new ModuleSrcConfigFile();
FileCache fileCache = new FileCache(moduleSrcConfigFile);
FileCacheManager manager = new FileCacheManager(List.of(fileCache));
manager.start();
FileCacheManagerInstance.getInstance().start();
//更改数组元素
for(int i=0;i<10;i++){
fileCache.writeKeys(i,"sectionwork","src","0");
GlobalFileCache.ModuleSrcConfigFile.writeKeys(i,"sectionwork","src","0");
}
//追加数组
for(int i=0;i<10;i++){
fileCache.writeKeys(i,"sectionwork","src","-1");
GlobalFileCache.ModuleSrcConfigFile.writeKeys(i,"sectionwork","src","-1");
}
//更改类中的元素
for(int i=0;i<10;i++){
fileCache.writeKeys(i,"barrage","src");
GlobalFileCache.ModuleSrcConfigFile.writeKeys(i,"barrage","src");
}
Thread.sleep(500000);
}
@Test
public void TestJson(){
Student student = new Student("Genius",18,"qinghua","SC",List.of("1","2","3"),null);
String s = JSONObject.toJSONString("");
JSONObject jsonObject = JSONObject.parseObject(s);
jsonObject.put("hobby",List.of("1","2","3","4").toString());
System.out.println(jsonObject);
public void TestArrayAdd() throws InterruptedException, FileCacheException {
ModuleSrcConfigFile moduleSrcConfigFile = new ModuleSrcConfigFile();
FileCache fileCache = new FileCache(moduleSrcConfigFile);
FileCacheManager manager = new FileCacheManager(List.of(fileCache));
manager.start();
System.out.println(manager.addFileCache(fileCache));
}
@Test
public void TestAddCache() throws FileCacheException, InterruptedException {
FileCacheManagerInstance.getInstance().start();
for(int i=0;i<10;i++){
Student student = new Student(Integer.toString(i),i,"jsu"+i,"major"+i,null,null);
cache.writeKeys(student,"-1");
}
Thread.sleep(500000);
}
}

View File

@@ -1,4 +1,4 @@
package org.example.bean;
package org.example.pojo;
import lombok.Data;

View File

@@ -1,15 +1,9 @@
package org.example.util;
import com.alibaba.fastjson.JSONReader;
import com.alibaba.fastjson.JSONWriter;
import org.example.bean.Student;
import org.example.pojo.Student;
import org.junit.jupiter.api.Test;
import org.junit.platform.commons.util.PackageUtils;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

View File

@@ -1,10 +1,4 @@
{
"name": "ChopperBot",
"description": "A bot for the ChopperMC server",
"version": "1.0.0",
"module": {
"type": ["Account","Creeper","File","Hot","Publish","Section","SectionWork","VideoSection"],
"main": "console",
"ui": "console-ui"
}
"data":[],
"updateTime":"2023-04-25 23:46:35"
}