Skip to content

Commit a5b9a37

Browse files
committed
fixed thread-safe in CrowdinOTA
1 parent 05ac764 commit a5b9a37

File tree

1 file changed

+20
-5
lines changed
  • src/main/java/org/maxgamer/quickshop/localization/text/distributions/crowdin

1 file changed

+20
-5
lines changed

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@
4040
import java.nio.charset.StandardCharsets;
4141
import java.nio.file.Files;
4242
import java.util.*;
43+
import java.util.concurrent.locks.ReentrantLock;
4344

4445
public class CrowdinOTA implements Distribution {
4546
protected static final String CROWDIN_OTA_HOST = "https://distributions.crowdin.net/daf1a8db40f132ce157c457xrm4/";
4647
private final QuickShop plugin;
48+
private final OTACacheControl otaCacheControl = new OTACacheControl();
4749

4850
public CrowdinOTA(QuickShop plugin) {
4951
this.plugin = plugin;
@@ -149,7 +151,7 @@ public String getFile(String fileCrowdinPath, String crowdinLocale, boolean forc
149151
// }
150152
// Post path (replaced with locale code)
151153
String postProcessingPath = fileCrowdinPath.replace("%locale%", crowdinLocale);
152-
OTACacheControl otaCacheControl = new OTACacheControl();
154+
153155
// Validating the manifest
154156
long manifestTimestamp = getManifest().getTimestamp();
155157
if (otaCacheControl.readManifestTimestamp() == getManifest().getTimestamp() && !forceFlush) {
@@ -245,35 +247,46 @@ public static class CrowdinGetFileRequest {
245247
public static class OTACacheControl {
246248
private final File metadataFile = new File(Util.getCacheFolder(), "i18n.metadata");
247249
private final YamlConfiguration metadata;
250+
private final ReentrantLock LOCK = new ReentrantLock();
248251

249252
public OTACacheControl() {
250253
this.metadata = YamlConfiguration.loadConfiguration(this.metadataFile);
251254
}
252255

253256
@SneakyThrows
254257
private void save() {
258+
LOCK.lock();
255259
this.metadata.save(this.metadataFile);
260+
LOCK.unlock();
256261
}
257262

258263
private String hash(String str) {
259264
return DigestUtils.sha1Hex(str);
260265
}
261266

262267
public long readManifestTimestamp() {
263-
return this.metadata.getLong("manifest.timestamp", -1);
268+
LOCK.lock();
269+
long l = this.metadata.getLong("manifest.timestamp", -1);
270+
LOCK.unlock();
271+
return l;
264272
}
265273

266274
public void writeManifestTimestamp(long timestamp) {
275+
LOCK.lock();
267276
this.metadata.set("manifest.timestamp", timestamp);
277+
LOCK.unlock();
268278
save();
269279
}
270280

271281
public long readCachedObjectTimestamp(String path) {
272282
String cacheKey = hash(path);
273-
return this.metadata.getLong("objects." + cacheKey + ".time", -1);
283+
LOCK.lock();
284+
long l = this.metadata.getLong("objects." + cacheKey + ".time", -1);
285+
LOCK.unlock();
286+
return l;
274287
}
275288

276-
public synchronized boolean isCachedObjectOutdated(String path, long manifestTimestamp) {
289+
public boolean isCachedObjectOutdated(String path, long manifestTimestamp) {
277290
return readCachedObjectTimestamp(path) != manifestTimestamp;
278291
}
279292

@@ -282,10 +295,12 @@ public byte[] readObjectCache(String path) throws IOException {
282295
return Files.readAllBytes(new File(Util.getCacheFolder(), cacheKey).toPath());
283296
}
284297

285-
public synchronized void writeObjectCache(String path, byte[] data, long manifestTimestamp) throws IOException {
298+
public void writeObjectCache(String path, byte[] data, long manifestTimestamp) throws IOException {
286299
String cacheKey = hash(path);
287300
Files.write(new File(Util.getCacheFolder(), cacheKey).toPath(), data);
301+
LOCK.lock();
288302
this.metadata.set("objects." + cacheKey + ".time", manifestTimestamp);
303+
LOCK.unlock();
289304
save();
290305
}
291306

0 commit comments

Comments
 (0)