mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-12-26 18:47:37 +01:00
#806 Create period cleanup task and implement cleanup for session records
This commit is contained in:
parent
eef314b965
commit
f2d7fe908e
@ -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) {
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
14
src/main/java/fr/xephi/authme/initialization/HasCleanup.java
Normal file
14
src/main/java/fr/xephi/authme/initialization/HasCleanup.java
Normal 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();
|
||||
|
||||
}
|
26
src/main/java/fr/xephi/authme/task/CleanupTask.java
Normal file
26
src/main/java/fr/xephi/authme/task/CleanupTask.java
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user