Я пытаюсь настроить серверный кеш должным образом и ищу конструктивную критику в отношении настроек, которые у меня есть в настоящее время. Кеш загружается при запуске сервлета и больше никогда не изменяется, поэтому по сути это кеш только для чтения. Это очевидно должно остаться в памяти на всю жизнь сервлета. Вот как у меня это настроено
private static List<ProductData> _cache;
private static ProductManager productManager;
private ProductManager() {
try {
lookup();
} catch (Exception ex) {
_cache = null;
}
}
public synchronized static ProductManager getInstance() {
if (productManager== null) {
productManager= new ProductManager();
}
return productManager;
}
Кеш настраивается сервлетом, как показано ниже:
private ProductManager productManager;
public void init(ServletConfig config) throws ServletException {
productManager = ProductManager.getInstance();
}
И наконец, вот как я к нему обращаюсь:
public static ProductData lookup(long id) throws Exception {
if (_cache != null) {
for (int i = 0; i < _cache.size(); i++) {
if (_cache.get(i).id == id) {
return _cache.get(i);
}
}
}
// Look it up in the DB.
}
public static List<ProductData> lookup() throws Exception {
if (_cache != null) {
return _cache;
}
// Read it from the DB.
_cache = list;
return list;
}