Forráskód Böngészése

Possibilité d'activer/desactiver le cache de la bd

Arthur Brandao 5 éve
szülő
commit
e0ebd9169e
3 módosított fájl, 32 hozzáadás és 13 törlés
  1. 4 1
      src/database.properties
  2. 5 0
      src/db/DatabaseProperties.java
  3. 23 12
      src/db/DatabaseTable.java

+ 4 - 1
src/database.properties

@@ -5,4 +5,7 @@ url=jdbc:derby:microfolie-db;create=true
 manager=microfolie.db.MicrofolieDatabase
 
 # Valeur par défaut de @DbLink
-dblink=microfolie.db.table
+dblink=microfolie.db.table
+
+# Active ou non le cache des données récupèré en base
+cache=true

+ 5 - 0
src/db/DatabaseProperties.java

@@ -17,5 +17,10 @@ public class DatabaseProperties {
 	public static String get(String key) {
 		return properties.getProperty(key);
 	}
+	
+	public static boolean getBool(String key) {
+		String prop = properties.getProperty(key);
+		return "true".equals(prop.toLowerCase());
+	}
 
 }

+ 23 - 12
src/db/DatabaseTable.java

@@ -18,6 +18,14 @@ public abstract class DatabaseTable<T extends Persistable> {
 	
 	private Map<Long, T> cacheMap = new HashMap<>();
 	
+	public T get(T obj) {
+		return getById(obj.getId());
+	}
+	
+	public Optional<T> find(T obj) {
+		return findById(obj.getId());
+	}
+	
 	public T getById(long id) {
 		if (cacheMap.containsKey(id)) {
 			return cacheMap.get(id);
@@ -63,25 +71,23 @@ public abstract class DatabaseTable<T extends Persistable> {
 		if(!optObj.isPresent()) {
 			throw new IllegalStateException("Unable to find entity in the database");
 		}
-		T obj = optObj.get();
-		cache(obj);
-		return obj;
+		return cache(optObj.get());
 	}
 	
 	public T refresh(T obj) {
 		return refresh(obj.getId());
 	}
 	
-	public void save(T obj) {
+	public T save(T obj) {
 		if (cacheMap.containsKey(obj.getId())) {
 			update(obj);
 		} else {
 			insert(obj);
 		}
-		cache(obj);
+		return cache(obj);
 	}
 	
-	public final void delete(long id) {
+	public void delete(long id) {
 		DbTable dbTable = getDbTableAnnotation();
 		// Cherche l'id
 		String idField = null;
@@ -116,10 +122,13 @@ public abstract class DatabaseTable<T extends Persistable> {
 	}
 	
 	private T cache(T obj) {
-		if (cacheMap.containsKey(obj.getId())) {
-			cacheMap.replace(obj.getId(), obj);
-		} else {
-			cacheMap.put(obj.getId(), obj);
+		// Si le cache est actif
+		if (DatabaseProperties.getBool("cache")) {
+			if (cacheMap.containsKey(obj.getId())) {
+				cacheMap.replace(obj.getId(), obj);
+			} else {
+				cacheMap.put(obj.getId(), obj);
+			}
 		}
 		//System.out.println("Cache size: " + cacheMap.size());
 		//cacheMap.forEach((key, val) -> System.out.println("Cache: " + key));
@@ -241,9 +250,10 @@ public abstract class DatabaseTable<T extends Persistable> {
 				if(field.isAnnotationPresent(DbLink.class)) {
 					DatabaseUtils.checkIfPersistable(field);
 					if(field.get(obj) == null) {
-						params.add(0l);
+						params.add(null);
 					} else {
 						Persistable persistable = (Persistable) field.get(obj);
+						// TODO Sauvegarder l'autre objet avant
 						params.add(persistable.getId());
 					}
 				}
@@ -310,9 +320,10 @@ public abstract class DatabaseTable<T extends Persistable> {
 				if(field.isAnnotationPresent(DbLink.class)) {
 					DatabaseUtils.checkIfPersistable(field);
 					if(field.get(obj) == null) {
-						params.add(0l);
+						params.add(null);
 					} else {
 						Persistable persistable = (Persistable) field.get(obj);
+						// TODO Sauvegarder l'autre objet avant
 						params.add(persistable.getId());
 					}
 				}