-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: file monitor update (#764)
* feat: update file listening create file bug * refactor: update file monitor
- Loading branch information
Showing
12 changed files
with
122 additions
and
30 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
.../docean/src/test/java/com/xiaomi/youpin/docean/test/demo/mydemo/DefaultDemoInterface.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.xiaomi.youpin.docean.test.demo.mydemo; | ||
|
||
import com.xiaomi.youpin.docean.anno.Component; | ||
|
||
/** | ||
* @author wtt | ||
* @version 1.0 | ||
* @description | ||
* @date 2023/11/20 10:19 | ||
*/ | ||
@Component | ||
public class DefaultDemoInterface implements DemoInterface { | ||
@Override | ||
public String hi() { | ||
return "test"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/demo/mydemo/DemoInterface.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.xiaomi.youpin.docean.test.demo.mydemo; | ||
|
||
/** | ||
* @author [email protected] | ||
* @date 2023/11/18 14:50 | ||
*/ | ||
public interface DemoInterface { | ||
|
||
String hi(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,4 +34,7 @@ default void setPointer(Object obj) { | |
|
||
} | ||
|
||
default void saveProgress() { | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,6 @@ public class FileInfo { | |
|
||
private long pointer; | ||
|
||
private String fileName; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,35 +3,40 @@ | |
import com.google.common.cache.Cache; | ||
import com.google.common.cache.CacheBuilder; | ||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import com.google.gson.reflect.TypeToken; | ||
import lombok.Setter; | ||
import lombok.SneakyThrows; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.lang.reflect.Type; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentMap; | ||
|
||
/** | ||
* @author [email protected] | ||
* @date 2023/9/26 11:50 | ||
*/ | ||
@Slf4j | ||
public class FileInfoCache { | ||
|
||
private Cache<String, FileInfo> cache = CacheBuilder.newBuilder().maximumSize(50000).build(); | ||
|
||
private Cache<String, FileInfo> cache = CacheBuilder.newBuilder().maximumSize(10000).build(); | ||
|
||
private Gson gson = new Gson(); | ||
private Gson gson = new GsonBuilder().setLenient().create(); | ||
|
||
@Setter | ||
private String filePath = "/tmp/.ozhera_pointer"; | ||
|
||
private volatile boolean loaded = false; | ||
|
||
private static final class LazyHolder { | ||
private static final FileInfoCache ins = new FileInfoCache(); | ||
} | ||
|
||
|
||
public static final FileInfoCache ins() { | ||
return LazyHolder.ins; | ||
} | ||
|
@@ -45,15 +50,26 @@ public FileInfo get(String key) { | |
return cache.getIfPresent(key); | ||
} | ||
|
||
public void remove(String key) { | ||
cache.invalidate(key); | ||
} | ||
|
||
public ConcurrentMap<String, FileInfo> caches() { | ||
return cache.asMap(); | ||
} | ||
|
||
@SneakyThrows | ||
public void shutdown() { | ||
String str = gson.toJson(cache.asMap()); | ||
Files.write(Paths.get(filePath), str.getBytes()); | ||
FileWriter writer = new FileWriter(filePath, false); | ||
writer.append(str); | ||
writer.flush(); | ||
} | ||
|
||
@SneakyThrows | ||
public void load() { | ||
if (new File(filePath).exists()) { | ||
if (!loaded && new File(filePath).exists()) { | ||
loaded = true; | ||
String str = new String(Files.readAllBytes(Paths.get(filePath))); | ||
Type typeOfT = new TypeToken<Map<String, FileInfo>>() { | ||
}.getType(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.