Skip to content

Commit

Permalink
fixed language progress map loading too often
Browse files Browse the repository at this point in the history
  • Loading branch information
albogdano committed May 13, 2018
1 parent 66b90c4 commit 2a5c535
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions para-server/src/main/java/com/erudika/para/i18n/LanguageUtils.java
Expand Up @@ -69,6 +69,8 @@ public class LanguageUtils {
private static final Map<String, Map<String, String>> LANG_CACHE =
new ConcurrentHashMap<String, Map<String, String>>(ALL_LOCALES.size());

private static Sysprop langProgressCache = new Sysprop();

private String deflangCode;
private final String keyPrefix = "language".concat(Config.SEPARATOR);
private final String progressKey = keyPrefix.concat("progress");
Expand Down Expand Up @@ -258,7 +260,16 @@ public Map<String, Integer> getTranslationProgressMap(String appid) {
if (dao == null) {
return Collections.emptyMap();
}
Sysprop progress = dao.read(appid, progressKey);
Sysprop progress;
if (langProgressCache.getProperties().isEmpty()) {
progress = dao.read(appid, progressKey);
if (progress != null) {
langProgressCache = progress;
}
} else {
progress = langProgressCache;
}

Map<String, Integer> progressMap = new HashMap<>(ALL_LOCALES.size());
boolean isMissing = progress == null;
if (isMissing) {
Expand All @@ -275,6 +286,7 @@ public Map<String, Integer> getTranslationProgressMap(String appid) {
}
if (isMissing) {
dao.create(appid, progress);
langProgressCache = progress;
}
return progressMap;
}
Expand Down Expand Up @@ -376,12 +388,13 @@ private void updateTranslationProgressMap(String appid, String langCode, int val
} else {
progress.put(langCode, (int) ((approved / defsize) * 100));
}
Sysprop updatedProgress = new Sysprop(progressKey);
for (Map.Entry<String, Integer> entry : progress.entrySet()) {
updatedProgress.addProperty(entry.getKey(), entry.getValue());
}
langProgressCache = updatedProgress;
if (percent < 100 && !percent.equals(progress.get(langCode))) {
Sysprop s = new Sysprop(progressKey);
for (Map.Entry<String, Integer> entry : progress.entrySet()) {
s.addProperty(entry.getKey(), entry.getValue());
}
dao.create(appid, s);
dao.create(appid, updatedProgress);
}
}

Expand Down

0 comments on commit 2a5c535

Please sign in to comment.