Parcourir la source

Ajout d'un cacheReloader pour mettre à jour le cache

Martin Passard il y a 7 ans
Parent
commit
e25e4a4004
2 fichiers modifiés avec 49 ajouts et 1 suppressions
  1. 47 0
      src/main/CacheReloader.java
  2. 2 1
      src/main/Main.java

+ 47 - 0
src/main/CacheReloader.java

@@ -0,0 +1,47 @@
+package main;
+
+import java.util.Date;
+
+public class CacheReloader implements Runnable {
+		private Thread thread;
+		private String threadName;
+		private long timeout;
+		boolean end = false;
+
+	public CacheReloader(long timeout) {
+		this.threadName="Cache reloader";
+		this.timeout=timeout;
+	}
+
+	@Override
+	public void run() {
+		try {
+			do {
+			Cache c = Cache.getInstance();
+			long lastCacheUpdate = c.getLastCacheUpdate().getTime();
+			long now = new Date().getTime();
+			if(lastCacheUpdate+timeout < now) {
+				System.out.println("MISE A JOUR DE DB!!");
+				c.reload();
+			}
+			Thread.sleep(timeout*1000);
+			}while(!end);
+			
+		}catch(InterruptedException ie) {
+			System.err.println("Le thread de mise à jour du cache a été interompu");
+			
+		}
+
+	}
+	
+	
+	public void start() {
+		System.out.println("Démarage du cache Reloader. Mise à jour toute les "+timeout+" secondes.");
+		if(thread == null) {
+			thread = new Thread(this, this.threadName);
+			thread.start();
+		}
+		
+	}
+
+}

+ 2 - 1
src/main/Main.java

@@ -31,7 +31,8 @@ public class Main {
         for(int i = 0; i< CHANNELS.length; i++) {
         bot.joinChannel(CHANNELS[i]);
         }
-        
+        CacheReloader cacheReloader = new CacheReloader(3600); // Met à jour la base toute les heures.
+        cacheReloader.start();
 		}catch(ConnectException ce) {
 			failures++;
 			System.err.println("Erreur numéro "+failures);