diff --git a/CHANGELOG.md b/CHANGELOG.md index bc13415..71ab16b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,10 @@ - 🎈新增: 新增 `Process_R_Douyu` 斗鱼录播处理器(类) - 🎈新增: 新增 `AbstractProcessor` 处理器抽象类(类) +### FileModule +- ❌移除: 新增 `FileCache的oldJsonFile变量`,不在用map来进行版本更替判断,取而代之的是判断写入字节是否为0的高效率方法 +- 🎈新增: 新增 `FileCache` 方法 `get,writekeys,append` 更加方便的缓存获取,更加方便的写入与内容追加 +- 🧪测试: 测试 `FileCache` 方法 `get,writekeys,append`,功能正常,可以使用 HelloWorld: ![QQ截图20230425201236](https://twj666.oss-cn-hangzhou.aliyuncs.com/img1/QQ%E6%88%AA%E5%9B%BE20230425201236.png) diff --git a/FileModule/src/main/java/org/example/cache/FileCache.java b/FileModule/src/main/java/org/example/cache/FileCache.java index 2e15099..7c20007 100644 --- a/FileModule/src/main/java/org/example/cache/FileCache.java +++ b/FileModule/src/main/java/org/example/cache/FileCache.java @@ -31,7 +31,6 @@ public class FileCache { private ConcurrentHashMap jsonFile; //文件内容缓存 - private ConcurrentHashMap oldJsonFileTemp; //保存上一个版本的文件内容,用于优化自动刷入 private static final int MAX_WRITE_BUFFER_LIMIT = 4096; //最大写入缓存上线 private AtomicInteger writeByte; //当前写入的字节数 @@ -86,7 +85,6 @@ public class FileCache { } this.jsonFile = new ConcurrentHashMap<>(stringObjectMap); - this.oldJsonFileTemp = new ConcurrentHashMap<>(stringObjectMap); return true; } @@ -106,11 +104,19 @@ public class FileCache { * @throws InterruptedException */ public int writeKeys(Object data,String...keys) throws InterruptedException, FileCacheException { + return writeKeys(false,data,keys); + } + + private int writeKeys(boolean isAppend,Object data,String...keys) throws FileCacheException, InterruptedException { + + if(Objects.isNull(data)){return 0;} + String jsonDataStr = JSON.toJSONString(data); int writeBytes = jsonDataStr.getBytes().length; + if(writeBytes==0){return 0;} - JSONObject jsonData = writeInData(data,keys); - jsonFile.put("data",jsonData); + Object jsonData = writeInData(isAppend,data,keys); + if(Objects.isNull(jsonData)){return 0;} ConcurrentHashMap temp = new ConcurrentHashMap<>(jsonFile); int newBytes = writeByte.updateAndGet(x -> x + writeBytes >= MAX_WRITE_BUFFER_LIMIT ? 0 : x + writeBytes); @@ -128,18 +134,36 @@ public class FileCache { return this.writeKeys(data,key); } - - private JSONObject writeInData(Object value,String...keys) throws FileCacheException { + private Object writeInData(boolean isAppend,Object value,String...keys) throws FileCacheException { String[] finds = Arrays.copyOf(keys, keys.length - 1); - JSONObject data = JSONObject.parseObject(this.get("data").toString()); - Object data1 = this.jsonFile.get("data"); + Object data = this.get("data"); Object temp = this.get(finds); - String tempStr = temp.toString(); - if(tempStr.startsWith("[")&&tempStr.endsWith("]")){ - (JSONArray.parseArray(tempStr)).add(Integer.parseInt(keys[keys.length-1]),value); + if(temp instanceof JSONArray){ + try{ + //元素添加 + int index = Integer.parseInt(keys[keys.length-1]); + if(index==-1){ + ((JSONArray) temp).add(index,value); + }else{ + String oldValue = ((JSONArray) temp).get(index).toString(); + value = isAppend?oldValue+value.toString():value; + if(oldValue.equals(value)){ + return null; + } + ((JSONArray) temp).add(index,value); + } + }catch (Exception e){ + return null; + } } else if(temp instanceof JSONObject){ - ((JSONObject) temp).put(keys[keys.length-1],value); + String key = keys[keys.length-1]; + String oldValue = ((JSONObject) temp).get(key).toString(); + value = isAppend?oldValue+value:value; + if(oldValue.equals(value)){ + return null; + } + ((JSONObject) temp).put(key,value); } else{ throw new FileCacheException("the keys is error!"); @@ -148,17 +172,14 @@ public class FileCache { } /** - * 追加内容 + * 追加内容,支持数组添加内容,添加内容,需要将最后一个key置为-1 * @param keys 要查找的key * @param append 追加内容 * @return * @throws InterruptedException */ public int append(Object append,String...keys) throws InterruptedException, FileCacheException { - Object data = get(keys); - StringBuffer buffer = new StringBuffer(JSON.toJSONString(data)); - String jsonStr = buffer.append(JSON.toJSONString(append)).toString(); - return writeKeys(jsonStr,keys); + return writeKeys(true,append,keys); } /** @@ -167,14 +188,13 @@ public class FileCache { * @return */ public Object get(String...keys){ - Object jsonObject = JSONObject.parse(this.get("data").toString()); + Object jsonObject = this.get("data"); for (String key : keys) { - String jsonStr = jsonObject.toString(); - if(jsonStr.startsWith("{")&&jsonStr.endsWith("}")){ + if(jsonObject instanceof JSONObject){ jsonObject = ((JSONObject) jsonObject).get(key); } - else if(jsonStr.startsWith("[")&&jsonStr.endsWith("]")){ - jsonObject = (JSONArray.parseArray(jsonObject.toString())).get(Integer.parseInt(key)); + else if(jsonObject instanceof JSONArray){ + jsonObject = ((JSONArray) jsonObject).get(Integer.parseInt(key)); }else{ return jsonObject; } @@ -205,13 +225,17 @@ public class FileCache { */ public boolean needAutoSync(){ long now = TimeUtil.getCurrentSecond(); - return now- TimeUtil.getSecond(configFile.getUpdateTime())>autoSyncTime; + return now - TimeUtil.getSecond(configFile.getUpdateTime())>autoSyncTime; } /** * 强制刷入磁盘 */ public void forceSync(){ + if(writeByte.get()==0){ + logger.debug("未发生版本变化"); + return; + } clearWriteBytes(); ConcurrentHashMap temp = new ConcurrentHashMap<>(jsonFile); try { @@ -228,13 +252,7 @@ public class FileCache { private boolean sync(ConcurrentHashMap take){ configFile.updateConfigTime(); //刷新配置文件刷入时间 String dir = getFileName(); - if(oldJsonFileTemp.get("data").toString().equals(take.get("data").toString())){ - logger.debug("未发生版本变化"); - return true; - } - configFile.onlyUpdateTime(take); - oldJsonFileTemp = new ConcurrentHashMap<>(take); File file = JsonFileUtil.writeJsonFile(dir, take); logger.debug("正在写入{}新版本",dir); return Objects.isNull(file); diff --git a/FileModule/src/test/java/org/example/cache/FileCacheTest.java b/FileModule/src/test/java/org/example/cache/FileCacheTest.java index 968e398..5f073f4 100644 --- a/FileModule/src/test/java/org/example/cache/FileCacheTest.java +++ b/FileModule/src/test/java/org/example/cache/FileCacheTest.java @@ -38,9 +38,6 @@ public class FileCacheTest { FileCache fileCache = new FileCache(moduleSrcConfigFile); FileCacheManager manager = new FileCacheManager(List.of(fileCache)); manager.start(); - for(int i=0;i<1;i++){ - fileCache.append("sectionwork",Integer.toString(i)); - } Thread.sleep(500000); } @@ -52,20 +49,61 @@ public class FileCacheTest { FileCacheManager manager = new FileCacheManager(List.of(fileCache)); manager.start(); - System.out.println(fileCache.get("sectionwork", "src")); - //fileCache.append(1,"sectionwork","src","0"); + System.out.println(fileCache.get("barrage","src")); + fileCache.append(1,"barrage","src"); + } + + @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(); + //追加数组的某个元素 + for(int i=0;i<10;i++){ + fileCache.append(i,"sectionwork","src","0"); + } + //追加数组 + for(int i=0;i<10;i++){ + fileCache.append(i,"sectionwork","src","-1"); + } + //追加类中的元素 + for(int i=0;i<10;i++){ + fileCache.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(); + //更改数组元素 + for(int i=0;i<10;i++){ + fileCache.writeKeys(i,"sectionwork","src","0"); + } + //追加数组 + for(int i=0;i<10;i++){ + fileCache.writeKeys(i,"sectionwork","src","-1"); + } + //更改类中的元素 + for(int i=0;i<10;i++){ + fileCache.writeKeys(i,"barrage","src"); + } + Thread.sleep(500000); } @Test public void TestJson(){ - String a = "{username:'[1,2,3,4]',age:{'hello':'world'}}"; - Object jsonObject = JSONObject.parse(a); + 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); - System.out.println(jsonObject.getClass()); - - jsonObject = JSONArray.parse( ((JSONObject) jsonObject).get("username").toString() ); - System.out.println(jsonObject); - System.out.println(jsonObject.getClass()); } }