package kz.cafe.admin;

import java.util.*;
import java.util.concurrent.*;
import kz.cafe.db.*;
import org.apache.commons.logging.*;

@SuppressWarnings("rawtypes")
public final class Caching{
   
   private static Log log = LogFactory.getLog(MongoService.class);
   private static final String LOG = "[UI CACHE] ";
   
   
   public static ConcurrentHashMap<Class, List<Object>> mapCache =  new ConcurrentHashMap<Class, List<Object>>();
   
   
   public static void clean(){
      mapCache.clear();
   }
   
   
   @SuppressWarnings("unchecked")
   public static List<Object> getList(Class classs){
      List<Object> listResult = mapCache.get(classs);
      
      if(listResult == null){
         log.debug(LOG + "load class=" + classs.getName());
         long lStartTime = System.currentTimeMillis();
         
         listResult = MongoService.getList(classs);
         mapCache.put(classs, listResult);
         
         log.info(LOG + "ok, time=" + (System.currentTimeMillis() - lStartTime) + " ms");
      }
      
      return listResult;
   }
}
