Skip to content

Commit 340bca4

Browse files
committed
fix CrowdinOTA cache system
1 parent 8451d16 commit 340bca4

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

src/main/java/org/maxgamer/quickshop/localization/text/distributions/crowdin/CrowdinOTA.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public CrowdinOTA(QuickShop plugin) {
5959

6060
@Nullable
6161
public Manifest getManifest() {
62-
return JsonUtil.getGson().fromJson(getManifestJson(), Manifest.class);
62+
return JsonUtil.regular().fromJson(getManifestJson(), Manifest.class);
6363
}
6464

6565
/**
@@ -144,9 +144,9 @@ public String getFile(String fileCrowdinPath, String crowdinLocale, boolean forc
144144
if (!manifest.getFiles().contains(fileCrowdinPath)) {
145145
throw new IllegalArgumentException("The file " + fileCrowdinPath + " not exists on Crowdin");
146146
}
147-
if (manifest.getCustomLanguages() != null && !manifest.getCustomLanguages().contains(crowdinLocale)) {
148-
throw new IllegalArgumentException("The locale " + crowdinLocale + " not exists on Crowdin");
149-
}
147+
// if (manifest.getCustom_languages() != null && !manifest.getCustom_languages().contains(crowdinLocale)) {
148+
// throw new IllegalArgumentException("The locale " + crowdinLocale + " not exists on Crowdin");
149+
// }
150150
// Post path (replaced with locale code)
151151
String postProcessingPath = fileCrowdinPath.replace("%locale%", crowdinLocale);
152152
OTACacheControl otaCacheControl = new OTACacheControl();
@@ -158,16 +158,18 @@ public String getFile(String fileCrowdinPath, String crowdinLocale, boolean forc
158158
// Check cache outdated
159159
if (!otaCacheControl.isCachedObjectOutdated(postProcessingPath, manifestTimestamp)) {
160160
// Return the caches
161+
Util.debugLog("Use local cache for " + postProcessingPath);
161162
return new String(otaCacheControl.readObjectCache(postProcessingPath), StandardCharsets.UTF_8);
163+
} else {
164+
Util.debugLog("Local cache outdated for " + postProcessingPath);
162165
}
163166
} catch (Exception exception) {
164167
MsgUtil.debugStackTrace(exception.getStackTrace());
165168
}
169+
} else {
170+
Util.debugLog("Manifest timestamp check failed " + postProcessingPath + " excepted:" + otaCacheControl.readManifestTimestamp() + " actual: " + getManifest().getTimestamp());
166171
}
167172
// Out of the cache
168-
if (manifestTimestamp == null) { // null-safe
169-
manifestTimestamp = System.currentTimeMillis();
170-
}
171173
String url = CROWDIN_OTA_HOST + "content" + fileCrowdinPath.replace("%locale%", crowdinLocale);
172174
plugin.getLogger().info("Updating translation " + crowdinLocale + " from: " + url);
173175
String data = HttpUtil.createGet(url);
@@ -177,6 +179,7 @@ public String getFile(String fileCrowdinPath, String crowdinLocale, boolean forc
177179
}
178180
// Successfully grab the data from the remote server
179181
otaCacheControl.writeObjectCache(postProcessingPath, data.getBytes(StandardCharsets.UTF_8), manifestTimestamp);
182+
otaCacheControl.writeManifestTimestamp(getManifest().getTimestamp());
180183
return data;
181184

182185
// String pathHash = DigestUtils.sha1Hex(postProcessingPath);
@@ -260,7 +263,7 @@ public long readManifestTimestamp() {
260263
}
261264

262265
public void writeManifestTimestamp(long timestamp) {
263-
this.metadata.set("manigest.timestamp", timestamp);
266+
this.metadata.set("manifest.timestamp", timestamp);
264267
save();
265268
}
266269

@@ -271,7 +274,7 @@ public long readCachedObjectTimestamp(String path) {
271274

272275
public boolean isCachedObjectOutdated(String path, long manifestTimestamp) {
273276
String cacheKey = hash(path);
274-
return this.metadata.getLong("objects." + cacheKey + ".timestamp") != manifestTimestamp;
277+
return readCachedObjectTimestamp(path) != manifestTimestamp;
275278
}
276279

277280
public byte[] readObjectCache(String path) throws IOException {

src/main/java/org/maxgamer/quickshop/localization/text/distributions/crowdin/bean/Manifest.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,19 @@
1919

2020
package org.maxgamer.quickshop.localization.text.distributions.crowdin.bean;
2121

22-
import com.fasterxml.jackson.annotation.JsonProperty;
22+
import lombok.AllArgsConstructor;
2323
import lombok.Data;
2424
import lombok.NoArgsConstructor;
2525

2626
import java.util.List;
2727

2828

29-
@NoArgsConstructor
29+
@AllArgsConstructor
3030
@Data
31+
@NoArgsConstructor
3132
public class Manifest {
32-
33-
@JsonProperty("files")
3433
private List<String> files;
35-
@JsonProperty("languages")
3634
private List<String> languages;
37-
@JsonProperty("custom_languages")
38-
private List<?> customLanguages;
39-
@JsonProperty("timestamp")
40-
private Long timestamp;
41-
35+
private List<?> custom_languages;
36+
private long timestamp;
4237
}

0 commit comments

Comments
 (0)