J'ai cache avec tous les détails de client chargement quotidien de DB. Mais avant le chargement des détails quotidiens du client, je dois supprimer toutes les entrées précédentes dans le cache.Java Guava Cache: Comment effacer toutes les entrées de cache?
Actuellement, je fais:
public enum PeriodicUpdater {
TIMER;
private final AtomicBoolean isPublishing = new AtomicBoolean(false);
private final long period = TimeUnit.DAYS.toMillis(1);
@Autowired
@Qualifier("TestUtils") @Setter
private TestUtils testUtils;
public synchronized boolean initialize() {
return initialize(period, period);
}
boolean initialize(long delay, long period) {
if (isPublishing.get()) {
return false;
}
TimerTask task = new TimerTask() {
@Override public void run() {
try {
String path = getFile();
if(TestUtils.getFileNameCache().getIfPresent(path) == null) {
TestUtils.setFileNameCache(testUtils.buildFileCache(path));
}
} catch (Exception e) {
log.warn("Failed!", e);
}
}
};
Timer timer = new Timer("PeriodicUpdater", true); // daemon=true
timer.schedule(task, delay, period);
isPublishing.set(true);
return true;
}
}
J'utilise le cache ici:
public class TestUtils {
private static Cache<String, Map<String, List<String>>> fileCache = CacheBuilder
.newBuilder()
.expireAfterWrite(4, TimeUnit.DAYS)
.build();
public TestUtils() {
String path = getFile();
fileNameCache = buildFileCache(path);
}
public Cache<String, String> buildFileCache(String path) {
Cache<String, String> fileList = CacheBuilder
.newBuilder()
.expireAfterWrite(4, TimeUnit.DAYS)
.build();
fileList.put(path, new Date().toString());
return fileList;
}
/* doing some stuff with the cache */
}
Est-ce correct de faire? Je ne vois pas le cache se dégager. Quelqu'un peut-il me corriger si j'ai tort?
'cache' a une méthode' invalidateAll() 'efface toutes les entrées Je ne vous vois pas appeler cela – shmosel
Puisque vous utilisez une bibliothèque google, je vais laisser tomber cette note sur [alignement horizontal] (https://google.github.io/ styleguide/javaguide.html # s4.6.3-horizontal-alignment) à partir du guide de style Java de google – shmosel
Utiliser la bibliothèque google et adopter le style de codage google sont sans importance (même si j'ai trouvé l'alignement horizontal de l'OP un peu dérangeant aussi: P) –