Cache both backup profiles and backup objects

This commit is contained in:
Alexander Söderberg 2020-05-10 15:12:17 +02:00
parent d0dbb495b0
commit 2bd30af361
No known key found for this signature in database
GPG Key ID: C0207FF7EA146678
2 changed files with 79 additions and 30 deletions

View File

@ -56,42 +56,53 @@ public class PlayerBackupProfile implements BackupProfile {
private final Plot plot; private final Plot plot;
private final BackupManager backupManager; private final BackupManager backupManager;
private volatile List<Backup> backupCache;
private final Object backupLock = new Object();
private static boolean isValidFile(@NotNull final Path path) { private static boolean isValidFile(@NotNull final Path path) {
final String name = path.getFileName().toString(); final String name = path.getFileName().toString();
return name.endsWith(".schem") || name.endsWith(".schematic"); return name.endsWith(".schem") || name.endsWith(".schematic");
} }
@Override @NotNull public CompletableFuture<List<Backup>> listBackups() { @Override @NotNull public CompletableFuture<List<Backup>> listBackups() {
return CompletableFuture.supplyAsync(() -> { synchronized (this.backupLock) {
final Path path = this.getBackupDirectory(); if (this.backupCache != null) {
if (!Files.exists(path)) { return CompletableFuture.completedFuture(backupCache);
try {
Files.createDirectories(path);
} catch (IOException e) {
e.printStackTrace();
return Collections.emptyList();
}
} }
final List<Backup> backups = new ArrayList<>(); return CompletableFuture.supplyAsync(() -> {
try { final Path path = this.getBackupDirectory();
Files.walk(path).filter(PlayerBackupProfile::isValidFile).forEach(file -> { if (!Files.exists(path)) {
try { try {
final BasicFileAttributes Files.createDirectories(path);
basicFileAttributes = Files.readAttributes(file, BasicFileAttributes.class);
backups.add(new Backup(this, basicFileAttributes.creationTime().toMillis(), file));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
return Collections.emptyList();
} }
}); }
} catch (IOException e) { final List<Backup> backups = new ArrayList<>();
e.printStackTrace(); try {
} Files.walk(path).filter(PlayerBackupProfile::isValidFile).forEach(file -> {
return backups; try {
}); final BasicFileAttributes basicFileAttributes =
Files.readAttributes(file, BasicFileAttributes.class);
backups.add(
new Backup(this, basicFileAttributes.creationTime().toMillis(), file));
} catch (IOException e) {
e.printStackTrace();
}
});
} catch (IOException e) {
e.printStackTrace();
}
return backups;
});
}
} }
@Override public void destroy() throws IOException { @Override public void destroy() throws IOException {
Files.delete(this.getBackupDirectory()); Files.delete(this.getBackupDirectory());
// Invalidate backup cache
this.backupCache = null;
} }
@NotNull public Path getBackupDirectory() { @NotNull public Path getBackupDirectory() {
@ -102,14 +113,17 @@ public class PlayerBackupProfile implements BackupProfile {
@Override @NotNull public CompletableFuture<Backup> createBackup() { @Override @NotNull public CompletableFuture<Backup> createBackup() {
final CompletableFuture<Backup> future = new CompletableFuture<>(); final CompletableFuture<Backup> future = new CompletableFuture<>();
this.listBackups().thenAcceptAsync(backups -> { this.listBackups().thenAcceptAsync(backups -> {
if (backups.size() == backupManager.getBackupLimit()) { synchronized (this.backupLock) {
backups.get(backups.size() - 1).delete(); if (backups.size() == backupManager.getBackupLimit()) {
} backups.get(backups.size() - 1).delete();
final List<Plot> plots = Collections.singletonList(plot); }
final boolean result = SchematicHandler.manager.exportAll(plots, null, null, () -> final List<Plot> plots = Collections.singletonList(plot);
future.complete(new Backup(this, System.currentTimeMillis(), null))); final boolean result = SchematicHandler.manager.exportAll(plots, null, null, () ->
if (!result) { future.complete(new Backup(this, System.currentTimeMillis(), null)));
future.completeExceptionally(new RuntimeException("Failed to complete the backup")); if (!result) {
future.completeExceptionally(new RuntimeException("Failed to complete the backup"));
}
this.backupCache = null;
} }
}); });
return future; return future;

View File

@ -25,9 +25,12 @@
*/ */
package com.plotsquared.core.backup; package com.plotsquared.core.backup;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.plotsquared.core.PlotSquared; import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.configuration.Settings;
import com.plotsquared.core.plot.Plot; import com.plotsquared.core.plot.Plot;
import lombok.AccessLevel;
import lombok.Getter; import lombok.Getter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -36,6 +39,8 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
/** /**
* {@inheritDoc} * {@inheritDoc}
@ -45,6 +50,8 @@ import java.util.concurrent.CompletableFuture;
@Getter private final Path backupPath; @Getter private final Path backupPath;
private final boolean automaticBackup; private final boolean automaticBackup;
@Getter private final int backupLimit; @Getter private final int backupLimit;
private final Cache<PlotCacheKey, BackupProfile> backupProfileCache = CacheBuilder.newBuilder()
.expireAfterAccess(3, TimeUnit.MINUTES).build();
public SimpleBackupManager() throws Exception { public SimpleBackupManager() throws Exception {
this.backupPath = Objects.requireNonNull(PlotSquared.imp()).getDirectory().toPath().resolve("backups"); this.backupPath = Objects.requireNonNull(PlotSquared.imp()).getDirectory().toPath().resolve("backups");
@ -57,7 +64,13 @@ import java.util.concurrent.CompletableFuture;
@Override @NotNull public BackupProfile getProfile(@NotNull final Plot plot) { @Override @NotNull public BackupProfile getProfile(@NotNull final Plot plot) {
if (plot.hasOwner() && !plot.isMerged()) { if (plot.hasOwner() && !plot.isMerged()) {
return new PlayerBackupProfile(plot.getOwnerAbs(), plot, this); try {
return backupProfileCache.get(new PlotCacheKey(plot), () -> new PlayerBackupProfile(plot.getOwnerAbs(), plot, this));
} catch (ExecutionException e) {
final BackupProfile profile = new PlayerBackupProfile(plot.getOwnerAbs(), plot, this);
this.backupProfileCache.put(new PlotCacheKey(plot), profile);
return profile;
}
} }
return new NullBackupProfile(); return new NullBackupProfile();
} }
@ -74,4 +87,26 @@ import java.util.concurrent.CompletableFuture;
return this.automaticBackup; return this.automaticBackup;
} }
@RequiredArgsConstructor(access = AccessLevel.PRIVATE) private static final class PlotCacheKey {
private final Plot plot;
@Override public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final PlotCacheKey that = (PlotCacheKey) o;
return com.google.common.base.Objects.equal(plot.getArea(), that.plot.getArea())
&& com.google.common.base.Objects.equal(plot.getId(), that.plot.getId())
&& com.google.common.base.Objects.equal(plot.getOwnerAbs(), that.plot.getOwnerAbs());
}
@Override public int hashCode() {
return com.google.common.base.Objects.hashCode(plot.getArea(), plot.getId(), plot.getOwnerAbs());
}
}
} }