#806 Create period cleanup task and implement cleanup for session records

This commit is contained in:
ljacqu 2016-08-06 23:20:30 +02:00
parent eef314b965
commit f2d7fe908e
4 changed files with 62 additions and 1 deletions

View File

@ -45,6 +45,7 @@ import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.settings.properties.SettingsFieldRetriever;
import fr.xephi.authme.settings.propertymap.PropertyMap;
import fr.xephi.authme.task.CleanupTask;
import fr.xephi.authme.task.purge.PurgeService;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.util.FileUtils;
@ -80,6 +81,7 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import static fr.xephi.authme.settings.properties.EmailSettings.RECALL_PLAYERS;
import static fr.xephi.authme.util.BukkitService.TICKS_PER_MINUTE;
/**
* The AuthMe main class.
@ -271,6 +273,11 @@ public class AuthMe extends JavaPlugin {
// Purge on start if enabled
PurgeService purgeService = injector.getSingleton(PurgeService.class);
purgeService.runAutoPurge();
// Schedule clean up task
final int cleanupInterval = 5 * TICKS_PER_MINUTE;
CleanupTask cleanupTask = injector.getSingleton(CleanupTask.class);
cleanupTask.runTaskTimerAsynchronously(this, cleanupInterval, cleanupInterval);
}
protected void instantiateServices(Injector injector) {

View File

@ -1,10 +1,12 @@
package fr.xephi.authme.cache;
import fr.xephi.authme.initialization.HasCleanup;
import fr.xephi.authme.initialization.SettingsDependent;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.PluginSettings;
import javax.inject.Inject;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@ -12,7 +14,7 @@ import java.util.concurrent.ConcurrentHashMap;
* Manages sessions, allowing players to be automatically logged in if they join again
* within a configurable amount of time.
*/
public class SessionManager implements SettingsDependent {
public class SessionManager implements SettingsDependent, HasCleanup {
private static final int MINUTE_IN_MILLIS = 60_000;
// Player -> expiration of session in milliseconds
@ -74,4 +76,16 @@ public class SessionManager implements SettingsDependent {
sessions.clear();
}
}
@Override
public void performCleanup() {
final long currentTime = System.currentTimeMillis();
Iterator<Map.Entry<String, Long>> iterator = sessions.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, Long> entry = iterator.next();
if (entry.getValue() < currentTime) {
iterator.remove();
}
}
}
}

View File

@ -0,0 +1,14 @@
package fr.xephi.authme.initialization;
/**
* Common interface for types which have data that becomes outdated
* and that can be cleaned up periodically.
*/
public interface HasCleanup {
/**
* Performs the cleanup action.
*/
void performCleanup();
}

View File

@ -0,0 +1,26 @@
package fr.xephi.authme.task;
import ch.jalu.injector.Injector;
import fr.xephi.authme.initialization.HasCleanup;
import org.bukkit.scheduler.BukkitRunnable;
import javax.inject.Inject;
/**
* Task run periodically to invoke the cleanup task on services.
*/
public class CleanupTask extends BukkitRunnable {
@Inject
private Injector injector;
CleanupTask() {
}
@Override
public void run() {
for (HasCleanup service : injector.retrieveAllOfType(HasCleanup.class)) {
service.performCleanup();
}
}
}