This repository has been archived by the owner on Feb 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
cubewhy
committed
Jan 18, 2024
1 parent
2b04278
commit cbbcce1
Showing
8 changed files
with
101 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
75 changes: 75 additions & 0 deletions
75
backend/src/main/java/org/cubewhy/api/files/InvokeCount.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,75 @@ | ||
package org.cubewhy.api.files; | ||
|
||
import com.alibaba.fastjson2.JSON; | ||
import com.alibaba.fastjson2.JSONObject; | ||
import lombok.Getter; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.io.*; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
@Slf4j | ||
public class InvokeCount { | ||
@Getter | ||
private JSONObject config; | ||
public final File file; | ||
|
||
public InvokeCount(File file) { | ||
this.file = file; | ||
this.load(); | ||
} | ||
|
||
public void addCount(String apiName) { | ||
if (!config.containsKey(apiName)) { | ||
config.put(apiName, 0); | ||
} | ||
config.put(apiName, config.getIntValue(apiName) + 1); | ||
save(); | ||
} | ||
|
||
public int getCount(String apiName) { | ||
return config.getIntValue(apiName, 0); | ||
} | ||
|
||
public InvokeCount save() { | ||
try { | ||
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(this.file)); | ||
bufferedWriter.write(config.toString()); | ||
bufferedWriter.flush(); | ||
bufferedWriter.close(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
return this; | ||
} | ||
|
||
public InvokeCount load() { | ||
FileReader fileReader; | ||
boolean successful = false; | ||
|
||
while (!successful) { | ||
try { | ||
fileReader = new FileReader(this.file, StandardCharsets.UTF_8); | ||
config = JSON.parseObject(fileReader); | ||
if (config == null) { | ||
config = new JSONObject(); | ||
} | ||
successful = true; | ||
fileReader.close(); | ||
} catch (FileNotFoundException e) { | ||
|
||
try { | ||
if (!this.file.getParentFile().exists()) { | ||
this.file.getParentFile().mkdirs(); | ||
} | ||
this.file.createNewFile(); | ||
} catch (IOException ex) { | ||
throw new RuntimeException(ex); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
return this; | ||
} | ||
} |
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