mirror of
https://github.com/IntellectualSites/PlotSquared.git
synced 2024-11-26 07:06:44 +01:00
Cache both backup profiles and backup objects
This commit is contained in:
parent
d0dbb495b0
commit
2bd30af361
@ -56,12 +56,19 @@ 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() {
|
||||||
|
synchronized (this.backupLock) {
|
||||||
|
if (this.backupCache != null) {
|
||||||
|
return CompletableFuture.completedFuture(backupCache);
|
||||||
|
}
|
||||||
return CompletableFuture.supplyAsync(() -> {
|
return CompletableFuture.supplyAsync(() -> {
|
||||||
final Path path = this.getBackupDirectory();
|
final Path path = this.getBackupDirectory();
|
||||||
if (!Files.exists(path)) {
|
if (!Files.exists(path)) {
|
||||||
@ -76,9 +83,10 @@ public class PlayerBackupProfile implements BackupProfile {
|
|||||||
try {
|
try {
|
||||||
Files.walk(path).filter(PlayerBackupProfile::isValidFile).forEach(file -> {
|
Files.walk(path).filter(PlayerBackupProfile::isValidFile).forEach(file -> {
|
||||||
try {
|
try {
|
||||||
final BasicFileAttributes
|
final BasicFileAttributes basicFileAttributes =
|
||||||
basicFileAttributes = Files.readAttributes(file, BasicFileAttributes.class);
|
Files.readAttributes(file, BasicFileAttributes.class);
|
||||||
backups.add(new Backup(this, basicFileAttributes.creationTime().toMillis(), file));
|
backups.add(
|
||||||
|
new Backup(this, basicFileAttributes.creationTime().toMillis(), file));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@ -89,9 +97,12 @@ public class PlayerBackupProfile implements BackupProfile {
|
|||||||
return backups;
|
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,6 +113,7 @@ 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 -> {
|
||||||
|
synchronized (this.backupLock) {
|
||||||
if (backups.size() == backupManager.getBackupLimit()) {
|
if (backups.size() == backupManager.getBackupLimit()) {
|
||||||
backups.get(backups.size() - 1).delete();
|
backups.get(backups.size() - 1).delete();
|
||||||
}
|
}
|
||||||
@ -111,6 +123,8 @@ public class PlayerBackupProfile implements BackupProfile {
|
|||||||
if (!result) {
|
if (!result) {
|
||||||
future.completeExceptionally(new RuntimeException("Failed to complete the backup"));
|
future.completeExceptionally(new RuntimeException("Failed to complete the backup"));
|
||||||
}
|
}
|
||||||
|
this.backupCache = null;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
return future;
|
return future;
|
||||||
}
|
}
|
||||||
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user