Skip to content

Commit

Permalink
small improvements to TPSCache
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Dec 27, 2023
1 parent 881df12 commit fdc1121
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,21 @@ public interface TPSCache {
final class Default implements TPSCache {

private final Server server;
private final Cache<Boolean, Double> cached_tps;
private final Cache<Object, Double> cached_tps;
private final Object TPS_KEY;

Default(JavaPlugin plugin, long checkDelayMillis) {
this.server = plugin.getServer();
this.cached_tps = Caffeine.newBuilder().expireAfterWrite(Duration.ofMillis(checkDelayMillis)).build();
this.TPS_KEY = new Object(); // Dummy value to associate with tps in the backing Cache
}

@Override
public double getGlobalTPS() {
Double tps = this.cached_tps.getIfPresent(true);
Double tps = this.cached_tps.getIfPresent(this.TPS_KEY);
if (tps == null) {
tps = this.server.getTPS()[0];
this.cached_tps.put(true, tps);
this.cached_tps.put(this.TPS_KEY, tps);
}
return tps;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,24 @@
public final class TPSCache {

private final Server server;
private final Cache<Boolean, Double> cached_tps;
private final Cache<Object, Double> cached_tps;
private final Object TPS_KEY;

TPSCache(Server server, long checkDelayMillis) {
this.server = server;
this.cached_tps = Caffeine.newBuilder().expireAfterWrite(Duration.ofMillis(checkDelayMillis)).build();
this.TPS_KEY = new Object(); // Dummy value to associate with tps in the backing Cache
}

public static TPSCache create(long checkDelayMillis) {
return new TPSCache(AnarchyExploitFixes.getInstance().getServer(), checkDelayMillis);
}

public double getTPS() {
Double tps = this.cached_tps.getIfPresent(true);
Double tps = this.cached_tps.getIfPresent(this.TPS_KEY);
if (tps == null) {
tps = this.server.getTPS()[0];
this.cached_tps.put(true, tps);
this.cached_tps.put(this.TPS_KEY, tps);
}
return tps;
}
Expand Down

0 comments on commit fdc1121

Please sign in to comment.