Skip to content

Commit 9161afe

Browse files
committed
add cache for lang configuration
1 parent e31d885 commit 9161afe

1 file changed

Lines changed: 24 additions & 3 deletions

File tree

src/main/java/fr/mrcoq/lib/multilanguageapi/MultiLanguageAPI.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
import org.bukkit.plugin.Plugin;
1010

1111
import java.io.*;
12-
import java.util.Arrays;
13-
import java.util.List;
14-
import java.util.Optional;
12+
import java.util.*;
1513
import java.util.function.Consumer;
1614
import java.util.stream.Collectors;
1715

1816
public final class MultiLanguageAPI {
1917

18+
private static Map<Lang, FileConfiguration> cache = new HashMap<>();
19+
2020
private static File globalLangFolder = new File("./plugins/MultiLanguageAPI/");
2121
private static File globalLangFile = new File(globalLangFolder, "config.yml");
2222

@@ -101,7 +101,25 @@ public String getString(Player player, String path) throws LangFileNotFoundExcep
101101
return getString(lang, path);
102102
}
103103

104+
/**
105+
* Clear configurations in cache.
106+
*/
107+
public void clearCache() {
108+
cache.clear();
109+
}
110+
111+
/**
112+
* Get config of {@link fr.mrcoq.lib.multilanguageapi.Lang}.
113+
* @param lang
114+
* @return
115+
* @throws LangFileNotFoundException
116+
*/
104117
public FileConfiguration getConfig(Lang lang) throws LangFileNotFoundException {
118+
119+
if(cache.containsKey(lang)) {
120+
return cache.get(lang);
121+
}
122+
105123
Optional<File> optionalFile = Arrays.stream(langFolder.listFiles()).filter((file) -> lang.getLocals().stream().anyMatch(local -> file.getName().startsWith(local))).findFirst();
106124

107125
FileConfiguration config = null;
@@ -123,6 +141,8 @@ public FileConfiguration getConfig(Lang lang) throws LangFileNotFoundException {
123141
throw new LangFileNotFoundException(lang);
124142
}
125143

144+
cache.put(lang, config);
145+
126146
return config;
127147
}
128148

@@ -199,6 +219,7 @@ public static Lang getDefaultLang() {
199219
}
200220

201221
FileConfiguration config = YamlConfiguration.loadConfiguration(globalLangFile);
222+
202223
String defaultLang = config.getString("lang.default");
203224

204225
return Lang.getFromLocal(defaultLang);

0 commit comments

Comments
 (0)