Implement server group cache, standardize caching system
This commit is contained in:
parent
f4e8513343
commit
26c399fc8e
@ -14,7 +14,7 @@ import java.util.concurrent.TimeUnit;
|
||||
public final class Rank {
|
||||
|
||||
private static Map<String, Rank> rankCache = null;
|
||||
private static List<Rank> sortedRankCache = null;
|
||||
private static List<Rank> rankAltCache = null;
|
||||
private static long rankCacheUpdated = 0;
|
||||
|
||||
@Getter @Id private String id;
|
||||
@ -31,7 +31,7 @@ public final class Rank {
|
||||
|
||||
public static List<Rank> values() {
|
||||
updateCacheIfNeeded();
|
||||
return ImmutableList.copyOf(sortedRankCache);
|
||||
return ImmutableList.copyOf(rankAltCache);
|
||||
}
|
||||
|
||||
public Rank() {} // For Morphia
|
||||
@ -52,15 +52,15 @@ public final class Rank {
|
||||
private static void updateCacheIfNeeded() {
|
||||
if (rankCache == null || (System.currentTimeMillis() - rankCacheUpdated) > TimeUnit.MINUTES.toMillis(1)) {
|
||||
Map<String, Rank> working = new HashMap<>();
|
||||
List<Rank> workingSorted = new ArrayList<>();
|
||||
List<Rank> workingAlt = new ArrayList<>();
|
||||
|
||||
for (Rank rank : APIv3.getDatastore().createQuery(Rank.class).order("weight").asList()) {
|
||||
working.put(rank.getId(), rank);
|
||||
workingSorted.add(rank);
|
||||
workingAlt.add(rank);
|
||||
}
|
||||
|
||||
rankCache = working;
|
||||
sortedRankCache = workingSorted;
|
||||
rankAltCache = workingAlt;
|
||||
rankCacheUpdated = System.currentTimeMillis();
|
||||
}
|
||||
}
|
||||
|
@ -11,10 +11,15 @@ import org.mongodb.morphia.annotations.Id;
|
||||
import org.mongodb.morphia.annotations.Property;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Entity(value = "serverGroups", noClassnameStored = true)
|
||||
public final class ServerGroup {
|
||||
|
||||
private static Map<String, ServerGroup> serverGroupCache = null;
|
||||
private static List<ServerGroup> serverGroupAltCache = null;
|
||||
private static long serverGroupCacheUpdated = 0;
|
||||
|
||||
@Getter @Id private String id;
|
||||
@Getter private String image;
|
||||
// We rename this to public, we just can't name it that because it's a Java identifier.
|
||||
@ -25,11 +30,13 @@ public final class ServerGroup {
|
||||
@Getter @Setter @ExcludeFromReplies private Map<String, List<String>> permissions = new HashMap<>();
|
||||
|
||||
public static ServerGroup byId(String id) {
|
||||
return APIv3.getDatastore().createQuery(ServerGroup.class).field("id").equal(id).get();
|
||||
updateCacheIfNeeded();
|
||||
return serverGroupCache.get(id);
|
||||
}
|
||||
|
||||
public static List<ServerGroup> values() {
|
||||
return APIv3.getDatastore().createQuery(ServerGroup.class).asList();
|
||||
updateCacheIfNeeded();
|
||||
return serverGroupAltCache;
|
||||
}
|
||||
|
||||
public ServerGroup() {} // For Morphia
|
||||
@ -48,4 +55,20 @@ public final class ServerGroup {
|
||||
APIv3.getDatastore().delete(this);
|
||||
}
|
||||
|
||||
private static void updateCacheIfNeeded() {
|
||||
if (serverGroupCache == null || (System.currentTimeMillis() - serverGroupCacheUpdated) > TimeUnit.MINUTES.toMillis(1)) {
|
||||
Map<String, ServerGroup> working = new HashMap<>();
|
||||
List<ServerGroup> workingAlt = new ArrayList<>();
|
||||
|
||||
for (ServerGroup serverGroup : APIv3.getDatastore().createQuery(ServerGroup.class).asList()) {
|
||||
working.put(serverGroup.getId(), serverGroup);
|
||||
workingAlt.add(serverGroup);
|
||||
}
|
||||
|
||||
serverGroupCache = working;
|
||||
serverGroupAltCache = workingAlt;
|
||||
serverGroupCacheUpdated = System.currentTimeMillis();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user