Update dump cache immediately upon startup

This commit is contained in:
Colin McDonald 2016-06-30 17:30:20 -04:00
parent c1017f9241
commit 043bc76cd3
2 changed files with 54 additions and 49 deletions

View File

@ -214,6 +214,7 @@ public final class APIv3 extends AbstractVerticle {
Server.updateCache();
ServerGroup.updateCache();
EmailUtils.updateBannedEmailDomains();
GETDumpsType.updateCache();
}
private ObjectMapper createMongoJacksonMapper() {
@ -244,7 +245,7 @@ public final class APIv3 extends AbstractVerticle {
Router http = Router.router(vertx);
http.route().handler(LoggerHandler.create(LoggerFormat.TINY));
http.route().handler(TimeoutHandler.create(TimeUnit.SECONDS.toMillis(5)));
http.route().handler(TimeoutHandler.create(TimeUnit.SECONDS.toMillis(7)));
http.route().method(HttpMethod.PUT).method(HttpMethod.POST).method(HttpMethod.DELETE).handler(BodyHandler.create());
http.route().handler(new ActorAttributeHandler());
http.route().handler(new MetricsHandler());

View File

@ -3,6 +3,7 @@ package net.frozenorb.apiv3.route;
import com.google.common.collect.ImmutableSet;
import io.vertx.core.Handler;
import io.vertx.ext.web.RoutingContext;
import lombok.extern.slf4j.Slf4j;
import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.model.Grant;
import net.frozenorb.apiv3.model.Punishment;
@ -11,67 +12,70 @@ import net.frozenorb.apiv3.util.ErrorUtils;
import java.util.*;
import java.util.concurrent.TimeUnit;
@Slf4j
public final class GETDumpsType implements Handler<RoutingContext> {
private List<UUID> banCache = new LinkedList<>();
private List<UUID> blacklistCache = new LinkedList<>();
private Map<String, List<UUID>> grantCache = new HashMap<>();
private static List<UUID> banCache = new LinkedList<>();
private static List<UUID> blacklistCache = new LinkedList<>();
private static Map<String, List<UUID>> grantCache = new HashMap<>();
public GETDumpsType() {
APIv3.getVertxInstance().setPeriodic(TimeUnit.MINUTES.toMillis(5), (id) -> {
Punishment.findByType(ImmutableSet.of(
Punishment.PunishmentType.BAN,
Punishment.PunishmentType.BLACKLIST
), (punishments, error) -> {
if (error != null) {
error.printStackTrace();
return;
static {
APIv3.getVertxInstance().setPeriodic(TimeUnit.MINUTES.toMillis(5), (id) -> updateCache());
}
public static void updateCache() {
Punishment.findByType(ImmutableSet.of(
Punishment.PunishmentType.BAN,
Punishment.PunishmentType.BLACKLIST
), (punishments, error) -> {
if (error != null) {
error.printStackTrace();
return;
}
List<UUID> banCache = new LinkedList<>();
List<UUID> blacklistCache = new LinkedList<>();
for (Punishment punishment : punishments) {
if (!punishment.isActive()) {
continue;
}
List<UUID> banCache = new LinkedList<>();
List<UUID> blacklistCache = new LinkedList<>();
if (punishment.getType() == Punishment.PunishmentType.BAN) {
banCache.add(punishment.getUser());
} else if (punishment.getType() == Punishment.PunishmentType.BLACKLIST) {
blacklistCache.add(punishment.getUser());
}
}
for (Punishment punishment : punishments) {
if (!punishment.isActive()) {
continue;
}
GETDumpsType.banCache = banCache;
GETDumpsType.blacklistCache = blacklistCache;
});
if (punishment.getType() == Punishment.PunishmentType.BAN) {
banCache.add(punishment.getUser());
} else if (punishment.getType() == Punishment.PunishmentType.BLACKLIST) {
blacklistCache.add(punishment.getUser());
}
Grant.findAll((grants, error) -> {
if (error != null) {
error.printStackTrace();
return;
}
Map<String, List<UUID>> grantCache = new HashMap<>();
for (Grant grant : grants) {
if (!grant.isActive()) {
continue;
}
GETDumpsType.this.banCache = banCache;
GETDumpsType.this.blacklistCache = blacklistCache;
});
List<UUID> users = grantCache.get(grant.getRank());
Grant.findAll((grants, error) -> {
if (error != null) {
error.printStackTrace();
return;
if (users == null) {
users = new LinkedList<>();
grantCache.put(grant.getRank(), users);
}
Map<String, List<UUID>> grantCache = new HashMap<>();
users.add(grant.getUser());
}
for (Grant grant : grants) {
if (!grant.isActive()) {
continue;
}
List<UUID> users = grantCache.get(grant.getRank());
if (users == null) {
users = new LinkedList<>();
grantCache.put(grant.getRank(), users);
}
users.add(grant.getUser());
}
GETDumpsType.this.grantCache = grantCache;
});
GETDumpsType.grantCache = grantCache;
});
}