-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ajoute une gestion de cache pour la récupération des paramètres...
... sous forme de Provider, le 1er appel charge les paramètres et les met dans un cache qui restera disponible jusqu'à invalidation par un redémarrage du serveur ou un update. On en profite pour le greffer sur les paramètres de la table *Parametre* Change-Id: Ie9cc4b3207ca8fb56bc1dc1bcbe85fb829b21b09
- Loading branch information
Showing
7 changed files
with
232 additions
and
4 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
remocra/src/main/java/fr/sdis83/remocra/data/CleValeurClasseData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package fr.sdis83.remocra.data; | ||
|
||
public class CleValeurClasseData { | ||
private final String cle; | ||
private final Object valeur; | ||
private Class<?> clazz = null; | ||
|
||
public String getCle() { | ||
return cle; | ||
} | ||
|
||
public Object getValeur() { | ||
return valeur; | ||
} | ||
|
||
public Class<?> getClazz() { | ||
return clazz; | ||
} | ||
|
||
public CleValeurClasseData(String cle, Object valeur, Class<?> clazz) { | ||
this.cle = cle; | ||
this.valeur = valeur; | ||
this.clazz = clazz; | ||
} | ||
|
||
public CleValeurClasseData(String cle, Object valeur) { | ||
this.cle = cle; | ||
this.valeur = valeur; | ||
} | ||
|
||
public void setClazz(Class<?> clazz) { | ||
this.clazz = clazz; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
remocra/src/main/java/fr/sdis83/remocra/data/ParametreData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package fr.sdis83.remocra.data; | ||
|
||
import fr.sdis83.remocra.db.model.remocra.enums.TypeParametre; | ||
import fr.sdis83.remocra.db.model.remocra.tables.pojos.Parametre; | ||
import fr.sdis83.remocra.domain.remocra.ParamConf; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class ParametreData { | ||
private final Map<String, CleValeurClasseData> mapValueByKey = new HashMap<>(); | ||
|
||
public ParametreData( | ||
Collection<CleValeurClasseData> listParamConfData, Collection<Parametre> listParametreData) { | ||
for (CleValeurClasseData CleValeurClasseData : listParamConfData) { | ||
mapValueByKey.put( | ||
CleValeurClasseData.getCle(), | ||
new CleValeurClasseData( | ||
CleValeurClasseData.getCle(), | ||
CleValeurClasseData.getValeur(), | ||
ParamConf.ParamConfParam.getClassFromCle(CleValeurClasseData.getCle()))); | ||
} | ||
for (Parametre parametre : listParametreData) { | ||
mapValueByKey.put( | ||
parametre.getCleParametre(), | ||
new CleValeurClasseData( | ||
parametre.getCleParametre(), | ||
parametre.getValeurParametre(), | ||
getClassFromTypeParametre(parametre.getTypeParametre()))); | ||
} | ||
} | ||
|
||
/** | ||
* Retourne la classe attendue pour un type de paramètre donné | ||
* | ||
* @param typeParametre {@link TypeParametre} | ||
* @return Class<?> | ||
*/ | ||
private static Class<?> getClassFromTypeParametre(TypeParametre typeParametre) { | ||
switch (typeParametre) { | ||
case INTEGER: | ||
return Integer.class; | ||
case DOUBLE: | ||
return Double.class; | ||
case GEOMETRY: | ||
case BINARY: | ||
case STRING: | ||
default: | ||
return String.class; | ||
} | ||
} | ||
|
||
public String getValeurString(String cle) { | ||
if (!mapValueByKey.containsKey(cle)) { | ||
return null; | ||
} | ||
return mapValueByKey.get(cle).getValeur().toString(); | ||
} | ||
|
||
public Object getValeur(String cle) { | ||
if (!mapValueByKey.containsKey(cle)) { | ||
return null; | ||
} | ||
if (mapValueByKey.get(cle).getClazz().equals(Integer.class)) { | ||
return Integer.valueOf((String) mapValueByKey.get(cle).getValeur()); | ||
} | ||
|
||
// TODO pour chaque type concret, transtyper proprement | ||
return mapValueByKey.get(cle).getValeur().toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
remocra/src/main/java/fr/sdis83/remocra/usecase/parametre/ParametreDataProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package fr.sdis83.remocra.usecase.parametre; | ||
|
||
import fr.sdis83.remocra.data.CleValeurClasseData; | ||
import fr.sdis83.remocra.data.ParametreData; | ||
import fr.sdis83.remocra.db.model.remocra.tables.pojos.Parametre; | ||
import fr.sdis83.remocra.repository.ParamConfRepository; | ||
import fr.sdis83.remocra.repository.ParametreRepository; | ||
import java.util.Collection; | ||
import javax.inject.Inject; | ||
import javax.inject.Provider; | ||
import javax.inject.Singleton; | ||
import org.apache.log4j.Logger; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Singleton | ||
@Configuration | ||
public class ParametreDataProvider implements Provider<ParametreData> { | ||
private ParametreData parametreData = null; | ||
|
||
private final Logger logger = Logger.getLogger(getClass()); | ||
|
||
@Inject private ParamConfRepository paramConfRepository; | ||
|
||
@Inject private ParametreRepository parametreRepository; | ||
|
||
public void reloadParametres() { | ||
parametreData = getParametreData(); | ||
} | ||
|
||
private ParametreData getParametreData() { | ||
Collection<CleValeurClasseData> listParamConf = paramConfRepository.getParametres(); | ||
Collection<Parametre> listParametres = parametreRepository.getParametres(); | ||
|
||
return new ParametreData(listParamConf, listParametres); | ||
} | ||
|
||
@Override | ||
public ParametreData get() { | ||
if (parametreData == null) { | ||
logger.info("Mise en cache des paramètres"); | ||
parametreData = getParametreData(); | ||
} | ||
return parametreData; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
remocra/src/main/java/fr/sdis83/remocra/usecase/parametre/UpdateParametreUseCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package fr.sdis83.remocra.usecase.parametre; | ||
|
||
import fr.sdis83.remocra.repository.ParamConfRepository; | ||
import fr.sdis83.remocra.repository.ParametreRepository; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class UpdateParametreUseCase { | ||
private final Logger logger = LoggerFactory.getLogger(getClass()); | ||
|
||
@Autowired ParametreDataProvider parametreDataProvider; | ||
|
||
@Autowired ParametreRepository parametreRepository; | ||
|
||
@Autowired ParamConfRepository paramConfRepository; | ||
|
||
/** | ||
* Met à jour un paramètre en BDD, et invalide le cache. | ||
* | ||
* @param cle Clé du paramètre | ||
* @param valeur Valeur stringifiée du paramètre pour stockage dans une colonne TEXT | ||
*/ | ||
public void updateParamConf(String cle, String valeur) { | ||
paramConfRepository.updateParamConf(cle, valeur); | ||
logger.info("Invalidation des paramètres : enregistrement de ParamConf " + cle); | ||
parametreDataProvider.reloadParametres(); | ||
} | ||
|
||
/** | ||
* Met à jour un paramètre en BDD, et invalide le cache. <br> | ||
* <b>La valeur sera stringifiée proprement par l'appelant pour garantir son stockage</b> | ||
* | ||
* @param cle Clé du paramètre | ||
* @param valeur Valeur stringifiée du paramètre pour stockage dans une colonne TEXT | ||
*/ | ||
public void updateParametre(String cle, String valeur) { | ||
parametreRepository.updateByKey(cle, valeur); | ||
logger.info("Invalidation des paramètres : enregistrement de Parametre " + cle); | ||
parametreDataProvider.reloadParametres(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters