JSONCache invalidation to some events

This commit is contained in:
Rsl1122 2019-08-31 10:51:14 +03:00
parent 06d4d2fef2
commit fbdf6dbb45
7 changed files with 214 additions and 76 deletions

View File

@ -17,6 +17,8 @@
package com.djrapitops.plan.gathering.listeners.bukkit; package com.djrapitops.plan.gathering.listeners.bukkit;
import com.djrapitops.plan.delivery.domain.Nickname; import com.djrapitops.plan.delivery.domain.Nickname;
import com.djrapitops.plan.delivery.webserver.cache.DataID;
import com.djrapitops.plan.delivery.webserver.cache.JSONCache;
import com.djrapitops.plan.extension.CallEvents; import com.djrapitops.plan.extension.CallEvents;
import com.djrapitops.plan.extension.ExtensionServiceImplementation; import com.djrapitops.plan.extension.ExtensionServiceImplementation;
import com.djrapitops.plan.gathering.cache.GeolocationCache; import com.djrapitops.plan.gathering.cache.GeolocationCache;
@ -147,6 +149,8 @@ public class PlayerOnlineListener implements Listener {
UUID playerUUID = player.getUniqueId(); UUID playerUUID = player.getUniqueId();
UUID serverUUID = serverInfo.getServerUUID(); UUID serverUUID = serverInfo.getServerUUID();
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
JSONCache.invalidate(DataID.SERVER_OVERVIEW, serverUUID);
JSONCache.invalidate(DataID.GRAPH_PERFORMANCE, serverUUID);
BukkitAFKListener.AFK_TRACKER.performedAction(playerUUID, time); BukkitAFKListener.AFK_TRACKER.performedAction(playerUUID, time);
@ -202,6 +206,9 @@ public class PlayerOnlineListener implements Listener {
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
Player player = event.getPlayer(); Player player = event.getPlayer();
UUID playerUUID = player.getUniqueId(); UUID playerUUID = player.getUniqueId();
UUID serverUUID = serverInfo.getServerUUID();
JSONCache.invalidate(DataID.SERVER_OVERVIEW, serverUUID);
JSONCache.invalidate(DataID.GRAPH_PERFORMANCE, serverUUID);
BukkitAFKListener.AFK_TRACKER.loggedOut(playerUUID, time); BukkitAFKListener.AFK_TRACKER.loggedOut(playerUUID, time);

View File

@ -17,6 +17,8 @@
package com.djrapitops.plan.gathering.listeners.bungee; package com.djrapitops.plan.gathering.listeners.bungee;
import com.djrapitops.plan.delivery.domain.keys.SessionKeys; import com.djrapitops.plan.delivery.domain.keys.SessionKeys;
import com.djrapitops.plan.delivery.webserver.cache.DataID;
import com.djrapitops.plan.delivery.webserver.cache.JSONCache;
import com.djrapitops.plan.delivery.webserver.cache.PageId; import com.djrapitops.plan.delivery.webserver.cache.PageId;
import com.djrapitops.plan.delivery.webserver.cache.ResponseCache; import com.djrapitops.plan.delivery.webserver.cache.ResponseCache;
import com.djrapitops.plan.extension.CallEvents; import com.djrapitops.plan.extension.CallEvents;
@ -90,33 +92,41 @@ public class PlayerOnlineListener implements Listener {
@EventHandler(priority = EventPriority.HIGHEST) @EventHandler(priority = EventPriority.HIGHEST)
public void onPostLogin(PostLoginEvent event) { public void onPostLogin(PostLoginEvent event) {
try { try {
ProxiedPlayer player = event.getPlayer(); actOnLogin(event);
UUID playerUUID = player.getUniqueId();
String playerName = player.getName();
InetAddress address = player.getAddress().getAddress();
long time = System.currentTimeMillis();
Session session = new Session(playerUUID, serverInfo.getServerUUID(), time, null, null);
session.putRawData(SessionKeys.SERVER_NAME, "Proxy Server");
sessionCache.cacheSession(playerUUID, session);
Database database = dbSystem.getDatabase();
boolean gatheringGeolocations = config.isTrue(DataGatheringSettings.GEOLOCATIONS);
if (gatheringGeolocations) {
database.executeTransaction(
new GeoInfoStoreTransaction(playerUUID, address, time, geolocationCache::getCountry)
);
}
database.executeTransaction(new PlayerRegisterTransaction(playerUUID, () -> time, playerName));
processing.submit(processors.info().playerPageUpdateProcessor(playerUUID));
processing.submitNonCritical(() -> extensionService.updatePlayerValues(playerUUID, playerName, CallEvents.PLAYER_JOIN));
ResponseCache.clearResponse(PageId.SERVER.of(serverInfo.getServerUUID())); // TODO Swap to clearing data after creating JSON cache.
} catch (Exception e) { } catch (Exception e) {
errorHandler.log(L.WARN, this.getClass(), e); errorHandler.log(L.WARN, this.getClass(), e);
} }
} }
private void actOnLogin(PostLoginEvent event) {
ProxiedPlayer player = event.getPlayer();
UUID playerUUID = player.getUniqueId();
String playerName = player.getName();
InetAddress address = player.getAddress().getAddress();
long time = System.currentTimeMillis();
Session session = new Session(playerUUID, serverInfo.getServerUUID(), time, null, null);
session.putRawData(SessionKeys.SERVER_NAME, "Proxy Server");
sessionCache.cacheSession(playerUUID, session);
Database database = dbSystem.getDatabase();
boolean gatheringGeolocations = config.isTrue(DataGatheringSettings.GEOLOCATIONS);
if (gatheringGeolocations) {
database.executeTransaction(
new GeoInfoStoreTransaction(playerUUID, address, time, geolocationCache::getCountry)
);
}
database.executeTransaction(new PlayerRegisterTransaction(playerUUID, () -> time, playerName));
processing.submit(processors.info().playerPageUpdateProcessor(playerUUID));
processing.submitNonCritical(() -> extensionService.updatePlayerValues(playerUUID, playerName, CallEvents.PLAYER_JOIN));
UUID serverUUID = serverInfo.getServerUUID();
JSONCache.invalidateMatching(DataID.SERVER_OVERVIEW);
JSONCache.invalidate(DataID.GRAPH_ONLINE, serverUUID);
JSONCache.invalidate(DataID.SERVERS);
}
@EventHandler(priority = EventPriority.NORMAL) @EventHandler(priority = EventPriority.NORMAL)
public void beforeLogout(PlayerDisconnectEvent event) { public void beforeLogout(PlayerDisconnectEvent event) {
ProxiedPlayer player = event.getPlayer(); ProxiedPlayer player = event.getPlayer();
@ -128,31 +138,59 @@ public class PlayerOnlineListener implements Listener {
@EventHandler(priority = EventPriority.HIGHEST) @EventHandler(priority = EventPriority.HIGHEST)
public void onLogout(PlayerDisconnectEvent event) { public void onLogout(PlayerDisconnectEvent event) {
try { try {
ProxiedPlayer player = event.getPlayer(); actOnLogout(event);
UUID playerUUID = player.getUniqueId();
sessionCache.endSession(playerUUID, System.currentTimeMillis());
processing.submit(processors.info().playerPageUpdateProcessor(playerUUID));
ResponseCache.clearResponse(PageId.SERVER.of(serverInfo.getServerUUID())); // TODO Swap to clearing data after creating JSON cache.
} catch (Exception e) { } catch (Exception e) {
errorHandler.log(L.WARN, this.getClass(), e); errorHandler.log(L.WARN, this.getClass(), e);
} }
} }
private void actOnLogout(PlayerDisconnectEvent event) {
ProxiedPlayer player = event.getPlayer();
UUID playerUUID = player.getUniqueId();
sessionCache.endSession(playerUUID, System.currentTimeMillis());
processing.submit(processors.info().playerPageUpdateProcessor(playerUUID));
ResponseCache.clearResponse(PageId.SERVER.of(serverInfo.getServerUUID())); // TODO Swap to clearing data after creating JSON cache.
processing.submit(() -> {
JSONCache.invalidateMatching(
DataID.SERVER_OVERVIEW,
DataID.SESSIONS,
DataID.GRAPH_WORLD_PIE,
DataID.GRAPH_PUNCHCARD,
DataID.KILLS,
DataID.ONLINE_OVERVIEW,
DataID.SESSIONS_OVERVIEW,
DataID.PVP_PVE,
DataID.GRAPH_UNIQUE_NEW,
DataID.GRAPH_CALENDAR
);
UUID serverUUID = serverInfo.getServerUUID();
JSONCache.invalidate(DataID.GRAPH_ONLINE, serverUUID);
JSONCache.invalidate(DataID.SERVERS);
});
}
@EventHandler(priority = EventPriority.HIGHEST) @EventHandler(priority = EventPriority.HIGHEST)
public void onServerSwitch(ServerSwitchEvent event) { public void onServerSwitch(ServerSwitchEvent event) {
try { try {
ProxiedPlayer player = event.getPlayer(); actOnServerSwitch(event);
UUID playerUUID = player.getUniqueId();
long time = System.currentTimeMillis();
// Replaces the current session in the cache.
Session session = new Session(playerUUID, serverInfo.getServerUUID(), time, null, null);
session.putRawData(SessionKeys.SERVER_NAME, "Proxy Server");
sessionCache.cacheSession(playerUUID, session);
processing.submit(processors.info().playerPageUpdateProcessor(playerUUID));
} catch (Exception e) { } catch (Exception e) {
errorHandler.log(L.WARN, this.getClass(), e); errorHandler.log(L.WARN, this.getClass(), e);
} }
} }
private void actOnServerSwitch(ServerSwitchEvent event) {
ProxiedPlayer player = event.getPlayer();
UUID playerUUID = player.getUniqueId();
long time = System.currentTimeMillis();
// Replaces the current session in the cache.
Session session = new Session(playerUUID, serverInfo.getServerUUID(), time, null, null);
session.putRawData(SessionKeys.SERVER_NAME, "Proxy Server");
sessionCache.cacheSession(playerUUID, session);
processing.submit(processors.info().playerPageUpdateProcessor(playerUUID));
JSONCache.invalidate(DataID.SERVERS);
}
} }

View File

@ -22,9 +22,12 @@ import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine; import com.github.benmanes.caffeine.cache.Caffeine;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import java.util.Arrays;
import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.stream.Collectors;
/** /**
* Cache for any JSON data sent via {@link com.djrapitops.plan.delivery.webserver.pages.json.RootJSONHandler}. * Cache for any JSON data sent via {@link com.djrapitops.plan.delivery.webserver.pages.json.RootJSONHandler}.
@ -59,8 +62,31 @@ public class JSONCache {
cache.invalidate(identifier); cache.invalidate(identifier);
} }
public static void invalidate(DataID dataID) {
invalidate(dataID.name());
}
public static void invalidate(UUID serverUUID, DataID... dataIDs) {
for (DataID dataID : dataIDs) {
invalidate(dataID.of(serverUUID));
}
}
public static void invalidate(DataID dataID, UUID serverUUID) { public static void invalidate(DataID dataID, UUID serverUUID) {
cache.invalidate(dataID.of(serverUUID)); invalidate(dataID.of(serverUUID));
}
public static void invalidateMatching(DataID... dataIDs) {
Set<String> toInvalidate = Arrays.stream(dataIDs)
.map(DataID::name)
.collect(Collectors.toSet());
for (String identifier : cache.asMap().keySet()) {
for (String identifierToInvalidate : toInvalidate) {
if (StringUtils.startsWith(identifier, identifierToInvalidate)) {
invalidate(identifier);
}
}
}
} }
public static void invalidateMatching(DataID dataID) { public static void invalidateMatching(DataID dataID) {

View File

@ -16,6 +16,8 @@
*/ */
package com.djrapitops.plan.storage.database.transactions.events; package com.djrapitops.plan.storage.database.transactions.events;
import com.djrapitops.plan.delivery.webserver.cache.DataID;
import com.djrapitops.plan.delivery.webserver.cache.JSONCache;
import com.djrapitops.plan.gathering.cache.SessionCache; import com.djrapitops.plan.gathering.cache.SessionCache;
import com.djrapitops.plan.storage.database.queries.DataStoreQueries; import com.djrapitops.plan.storage.database.queries.DataStoreQueries;
import com.djrapitops.plan.storage.database.queries.PlayerFetchQueries; import com.djrapitops.plan.storage.database.queries.PlayerFetchQueries;
@ -54,5 +56,7 @@ public class PlayerRegisterTransaction extends Transaction {
SessionCache.getCachedSession(playerUUID).ifPresent(session -> session.setAsFirstSessionIfMatches(registerDate)); SessionCache.getCachedSession(playerUUID).ifPresent(session -> session.setAsFirstSessionIfMatches(registerDate));
} }
execute(DataStoreQueries.updatePlayerName(playerUUID, playerName)); execute(DataStoreQueries.updatePlayerName(playerUUID, playerName));
JSONCache.invalidateMatching(DataID.PLAYERS);
} }
} }

View File

@ -16,6 +16,9 @@
*/ */
package com.djrapitops.plan.storage.database.transactions.events; package com.djrapitops.plan.storage.database.transactions.events;
import com.djrapitops.plan.delivery.domain.keys.SessionKeys;
import com.djrapitops.plan.delivery.webserver.cache.DataID;
import com.djrapitops.plan.delivery.webserver.cache.JSONCache;
import com.djrapitops.plan.gathering.domain.Session; import com.djrapitops.plan.gathering.domain.Session;
import com.djrapitops.plan.storage.database.queries.DataStoreQueries; import com.djrapitops.plan.storage.database.queries.DataStoreQueries;
import com.djrapitops.plan.storage.database.transactions.Transaction; import com.djrapitops.plan.storage.database.transactions.Transaction;
@ -36,5 +39,19 @@ public class SessionEndTransaction extends Transaction {
@Override @Override
protected void performOperations() { protected void performOperations() {
execute(DataStoreQueries.storeSession(session)); execute(DataStoreQueries.storeSession(session));
session.getValue(SessionKeys.SERVER_UUID)
.ifPresent(serverUUID -> JSONCache.invalidate(
serverUUID,
DataID.SESSIONS,
DataID.GRAPH_WORLD_PIE,
DataID.GRAPH_PUNCHCARD,
DataID.KILLS,
DataID.ONLINE_OVERVIEW,
DataID.SESSIONS_OVERVIEW,
DataID.PVP_PVE,
DataID.GRAPH_UNIQUE_NEW,
DataID.GRAPH_CALENDAR
));
} }
} }

View File

@ -17,6 +17,8 @@
package com.djrapitops.plan.gathering.listeners.sponge; package com.djrapitops.plan.gathering.listeners.sponge;
import com.djrapitops.plan.delivery.domain.Nickname; import com.djrapitops.plan.delivery.domain.Nickname;
import com.djrapitops.plan.delivery.webserver.cache.DataID;
import com.djrapitops.plan.delivery.webserver.cache.JSONCache;
import com.djrapitops.plan.extension.CallEvents; import com.djrapitops.plan.extension.CallEvents;
import com.djrapitops.plan.extension.ExtensionServiceImplementation; import com.djrapitops.plan.extension.ExtensionServiceImplementation;
import com.djrapitops.plan.gathering.cache.GeolocationCache; import com.djrapitops.plan.gathering.cache.GeolocationCache;
@ -150,6 +152,8 @@ public class PlayerOnlineListener {
UUID playerUUID = player.getUniqueId(); UUID playerUUID = player.getUniqueId();
UUID serverUUID = serverInfo.getServerUUID(); UUID serverUUID = serverInfo.getServerUUID();
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
JSONCache.invalidate(DataID.SERVER_OVERVIEW, serverUUID);
JSONCache.invalidate(DataID.GRAPH_PERFORMANCE, serverUUID);
SpongeAFKListener.AFK_TRACKER.performedAction(playerUUID, time); SpongeAFKListener.AFK_TRACKER.performedAction(playerUUID, time);
@ -206,6 +210,9 @@ public class PlayerOnlineListener {
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
Player player = event.getTargetEntity(); Player player = event.getTargetEntity();
UUID playerUUID = player.getUniqueId(); UUID playerUUID = player.getUniqueId();
UUID serverUUID = serverInfo.getServerUUID();
JSONCache.invalidate(DataID.SERVER_OVERVIEW, serverUUID);
JSONCache.invalidate(DataID.GRAPH_PERFORMANCE, serverUUID);
SpongeAFKListener.AFK_TRACKER.loggedOut(playerUUID, time); SpongeAFKListener.AFK_TRACKER.loggedOut(playerUUID, time);

View File

@ -16,6 +16,8 @@
*/ */
package com.djrapitops.plan.gathering.listeners.velocity; package com.djrapitops.plan.gathering.listeners.velocity;
import com.djrapitops.plan.delivery.webserver.cache.DataID;
import com.djrapitops.plan.delivery.webserver.cache.JSONCache;
import com.djrapitops.plan.delivery.webserver.cache.PageId; import com.djrapitops.plan.delivery.webserver.cache.PageId;
import com.djrapitops.plan.delivery.webserver.cache.ResponseCache; import com.djrapitops.plan.delivery.webserver.cache.ResponseCache;
import com.djrapitops.plan.extension.CallEvents; import com.djrapitops.plan.extension.CallEvents;
@ -92,32 +94,41 @@ public class PlayerOnlineListener {
@Subscribe(order = PostOrder.LAST) @Subscribe(order = PostOrder.LAST)
public void onPostLogin(PostLoginEvent event) { public void onPostLogin(PostLoginEvent event) {
try { try {
Player player = event.getPlayer(); actOnLogin(event);
UUID playerUUID = player.getUniqueId();
String playerName = player.getUsername();
InetAddress address = player.getRemoteAddress().getAddress();
long time = System.currentTimeMillis();
sessionCache.cacheSession(playerUUID, new Session(playerUUID, serverInfo.getServerUUID(), time, null, null));
Database database = dbSystem.getDatabase();
boolean gatheringGeolocations = config.isTrue(DataGatheringSettings.GEOLOCATIONS);
if (gatheringGeolocations) {
database.executeTransaction(
new GeoInfoStoreTransaction(playerUUID, address, time, geolocationCache::getCountry)
);
}
database.executeTransaction(new PlayerRegisterTransaction(playerUUID, () -> time, playerName));
processing.submit(processors.info().playerPageUpdateProcessor(playerUUID));
processing.submitNonCritical(() -> extensionService.updatePlayerValues(playerUUID, playerName, CallEvents.PLAYER_JOIN));
ResponseCache.clearResponse(PageId.SERVER.of(serverInfo.getServerUUID()));
} catch (Exception e) { } catch (Exception e) {
errorHandler.log(L.WARN, this.getClass(), e); errorHandler.log(L.WARN, this.getClass(), e);
} }
} }
public void actOnLogin(PostLoginEvent event) {
Player player = event.getPlayer();
UUID playerUUID = player.getUniqueId();
String playerName = player.getUsername();
InetAddress address = player.getRemoteAddress().getAddress();
long time = System.currentTimeMillis();
sessionCache.cacheSession(playerUUID, new Session(playerUUID, serverInfo.getServerUUID(), time, null, null));
Database database = dbSystem.getDatabase();
boolean gatheringGeolocations = config.isTrue(DataGatheringSettings.GEOLOCATIONS);
if (gatheringGeolocations) {
database.executeTransaction(
new GeoInfoStoreTransaction(playerUUID, address, time, geolocationCache::getCountry)
);
}
database.executeTransaction(new PlayerRegisterTransaction(playerUUID, () -> time, playerName));
processing.submit(processors.info().playerPageUpdateProcessor(playerUUID));
processing.submitNonCritical(() -> extensionService.updatePlayerValues(playerUUID, playerName, CallEvents.PLAYER_JOIN));
ResponseCache.clearResponse(PageId.SERVER.of(serverInfo.getServerUUID()));
UUID serverUUID = serverInfo.getServerUUID();
JSONCache.invalidateMatching(DataID.SERVER_OVERVIEW);
JSONCache.invalidate(DataID.GRAPH_ONLINE, serverUUID);
JSONCache.invalidate(DataID.SERVERS);
}
@Subscribe(order = PostOrder.NORMAL) @Subscribe(order = PostOrder.NORMAL)
public void beforeLogout(DisconnectEvent event) { public void beforeLogout(DisconnectEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
@ -129,30 +140,58 @@ public class PlayerOnlineListener {
@Subscribe(order = PostOrder.LAST) @Subscribe(order = PostOrder.LAST)
public void onLogout(DisconnectEvent event) { public void onLogout(DisconnectEvent event) {
try { try {
Player player = event.getPlayer(); actOnLogout(event);
UUID playerUUID = player.getUniqueId();
sessionCache.endSession(playerUUID, System.currentTimeMillis());
processing.submit(processors.info().playerPageUpdateProcessor(playerUUID));
ResponseCache.clearResponse(PageId.SERVER.of(serverInfo.getServerUUID()));
} catch (Exception e) { } catch (Exception e) {
errorHandler.log(L.WARN, this.getClass(), e); errorHandler.log(L.WARN, this.getClass(), e);
} }
} }
public void actOnLogout(DisconnectEvent event) {
Player player = event.getPlayer();
UUID playerUUID = player.getUniqueId();
sessionCache.endSession(playerUUID, System.currentTimeMillis());
processing.submit(processors.info().playerPageUpdateProcessor(playerUUID));
ResponseCache.clearResponse(PageId.SERVER.of(serverInfo.getServerUUID()));
processing.submit(() -> {
JSONCache.invalidateMatching(
DataID.SERVER_OVERVIEW,
DataID.SESSIONS,
DataID.GRAPH_WORLD_PIE,
DataID.GRAPH_PUNCHCARD,
DataID.KILLS,
DataID.ONLINE_OVERVIEW,
DataID.SESSIONS_OVERVIEW,
DataID.PVP_PVE,
DataID.GRAPH_UNIQUE_NEW,
DataID.GRAPH_CALENDAR
);
UUID serverUUID = serverInfo.getServerUUID();
JSONCache.invalidate(DataID.GRAPH_ONLINE, serverUUID);
JSONCache.invalidate(DataID.SERVERS);
});
}
@Subscribe(order = PostOrder.LAST) @Subscribe(order = PostOrder.LAST)
public void onServerSwitch(ServerConnectedEvent event) { public void onServerSwitch(ServerConnectedEvent event) {
try { try {
Player player = event.getPlayer(); actOnServerSwitch(event);
UUID playerUUID = player.getUniqueId();
long time = System.currentTimeMillis();
// Replaces the current session in the cache.
sessionCache.cacheSession(playerUUID, new Session(playerUUID, serverInfo.getServerUUID(), time, null, null));
processing.submit(processors.info().playerPageUpdateProcessor(playerUUID));
} catch (Exception e) { } catch (Exception e) {
errorHandler.log(L.WARN, this.getClass(), e); errorHandler.log(L.WARN, this.getClass(), e);
} }
} }
public void actOnServerSwitch(ServerConnectedEvent event) {
Player player = event.getPlayer();
UUID playerUUID = player.getUniqueId();
long time = System.currentTimeMillis();
// Replaces the current session in the cache.
sessionCache.cacheSession(playerUUID, new Session(playerUUID, serverInfo.getServerUUID(), time, null, null));
processing.submit(processors.info().playerPageUpdateProcessor(playerUUID));
JSONCache.invalidate(DataID.SERVERS);
}
} }