/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package FunctionLayer.Caching; import FunctionLayer.ErrorMessageDisplay; import FunctionLayer.LogicFacade; import FunctionLayer.MapBoard; import FunctionLayer.RecentTimes; import FunctionLayer.UserBanners; import FunctionLayer.Users; import com.google.common.base.Stopwatch; import com.google.common.collect.MapMaker; import java.beans.PropertyVetoException; import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author install1 */ //https://stackoverflow.com/questions/15357953/cached-map-with-data-initialization-and-whole-data-refreshed-periodically-in-jav public class GACaching { //change to 3 hours again public static final long EXPIRE_TIME_IN_SECONDS = TimeUnit.SECONDS.convert(3, TimeUnit.HOURS); public static int UPDATE_AVATARS_CYCLE = 2; public static int AVATAR_INDEXES = 0; public static GACaching instance = new GACaching(); private final ConcurrentMap UsersCache; private final ConcurrentMap UsersCachePoints; private final ConcurrentMap MapCache; private final ConcurrentMap MapCacheValues; private final ConcurrentMap UserBannerCache; private final ConcurrentMap UserCacheTimeAmount; private final ConcurrentMap UserCacheRecentTimes; private final Stopwatch stopwatch; public GACaching() { this.stopwatch = Stopwatch.createUnstarted(); this.UsersCache = new MapMaker().concurrencyLevel(2).makeMap(); this.MapCache = new MapMaker().concurrencyLevel(2).makeMap(); this.UsersCachePoints = new MapMaker().concurrencyLevel(2).makeMap(); this.MapCacheValues = new MapMaker().concurrencyLevel(2).makeMap(); this.UserBannerCache = new MapMaker().concurrencyLevel(2).makeMap(); this.UserCacheTimeAmount = new MapMaker().concurrencyLevel(2).makeMap(); this.UserCacheRecentTimes = new MapMaker().concurrencyLevel(2).makeMap(); } public synchronized void Initialization() throws ErrorMessageDisplay { // if the map is not initialized, initialize it with the data try { if (!stopwatch.isRunning()) { putAllConcurrentMaps(); stopwatch.start(); } if (stopwatch.elapsed(TimeUnit.MINUTES) >= UPDATE_AVATARS_CYCLE && AVATAR_INDEXES < UserCacheTimeAmount.values().size()) { UPDATE_AVATARS_CYCLE = (int) (stopwatch.elapsed().toMinutes() + 2); updateAvatars(AVATAR_INDEXES); } // if the map is expired, refresh all the data in the map if (stopwatch.elapsed(TimeUnit.SECONDS) >= EXPIRE_TIME_IN_SECONDS) { UPDATE_AVATARS_CYCLE = 2; if (AVATAR_INDEXES >= UserCacheTimeAmount.values().size()) { AVATAR_INDEXES = 0; } clearConcurrentMaps(); putAllConcurrentMaps(); stopwatch.reset(); } } catch (SQLException | IOException | PropertyVetoException ex) { throw new ErrorMessageDisplay("Initialization failed: " + ex); } } public synchronized void UpdateDeletedEntries() throws SQLException, ErrorMessageDisplay, IOException, PropertyVetoException { clearConcurrentMaps(); putAllConcurrentMaps(); stopwatch.reset(); } public synchronized void updateAvatars(int AVATAR_INDEXES1) throws ErrorMessageDisplay { new Thread(() -> { try { LogicFacade.UpdatePlayerAvatarsToDB((new ArrayList(UserCacheTimeAmount.values())), AVATAR_INDEXES1); } catch (IOException | ErrorMessageDisplay ex) { Logger.getLogger(GACaching.class.getName()).log(Level.SEVERE, null, ex); } }).start(); //each itereation handle 200 updates AVATAR_INDEXES += 200; } private Map getUserRecentTimesCache(ConcurrentMap UserCacheTimeAmount1, ConcurrentMap mpb) throws ErrorMessageDisplay { LinkedHashMap LHM = LogicFacade.getRecentTimes(UserCacheTimeAmount1, mpb); return LHM; } private Map getUserCacheContent() throws SQLException, ErrorMessageDisplay, IOException, PropertyVetoException { List Users; Users = LogicFacade.getAllUsersNameSteamIdInitialization(); LinkedHashMap LHM = new LinkedHashMap(); for (int i = 0; i < Users.size(); i++) { LHM.put(i, Users.get(i)); } return LHM; } private Map getMapBoardCacheContent() throws ErrorMessageDisplay, SQLException, IOException, PropertyVetoException { List MB; MB = LogicFacade.GetAllMapColoumns(); LinkedHashMap LHM = new LinkedHashMap(); for (int i = 0; i < MB.size(); i++) { LHM.put(i, MB.get(i)); } return LHM; } private Map getUserPointCacheContent(List PlayerLeaderBoard, List mapBoard) throws ErrorMessageDisplay, SQLException, IOException, PropertyVetoException { List Users; Users = LogicFacade.getPlayerPointsAndRankInitialization(PlayerLeaderBoard, mapBoard); LinkedHashMap LHM = new LinkedHashMap(); for (int i = 0; i < Users.size(); i++) { LHM.put(i, Users.get(i)); } return LHM; } private Map getMapBoardCacheValues(List mapBoard, List PlayerLeaderBoard) throws ErrorMessageDisplay { List MB; MB = LogicFacade.getAllMapValues(mapBoard, PlayerLeaderBoard); LinkedHashMap LHM = new LinkedHashMap(); for (int i = 0; i < MB.size(); i++) { LHM.put(i, MB.get(i)); } return LHM; } private Map getUserBannerCacheContent(List PlayerLeaderBoard) throws ErrorMessageDisplay { List UB; UB = LogicFacade.getUserBanners(PlayerLeaderBoard); LinkedHashMap LHM = new LinkedHashMap(); for (int i = 0; i < UB.size(); i++) { LHM.put(i, UB.get(i)); } return LHM; } private Map getUserTimeCache(List PlayerLeaderBoard, List mapBoard) { List Users; Users = LogicFacade.getPlayertimeAmount(PlayerLeaderBoard, mapBoard); LinkedHashMap LHM = new LinkedHashMap(); for (int i = 0; i < Users.size(); i++) { LHM.put(i, Users.get(i)); } return LHM; } public List getAllMapValues() { List MapColoumns = new ArrayList(MapCacheValues.values()); return MapColoumns; } public List getUserBanners() { List UB = new ArrayList(UserBannerCache.values()); return UB; } public List getAllPlayerValues() { List Users = new ArrayList(UserCacheTimeAmount.values()); return Users; } public List getAllRecentTimes() { List RT = new ArrayList(UserCacheRecentTimes.values()); return RT; } public int getAvatatindexTest() { return AVATAR_INDEXES; } public int getUPDATE_AVATARS_CYCLE() { return UPDATE_AVATARS_CYCLE; } public int getStopWatchMinutes() { return (int) stopwatch.elapsed(TimeUnit.MINUTES); } private void clearConcurrentMaps() { UsersCache.clear(); MapCache.clear(); UsersCachePoints.clear(); MapCacheValues.clear(); UserBannerCache.clear(); UserCacheTimeAmount.clear(); UserCacheRecentTimes.clear(); } private void putAllConcurrentMaps() throws SQLException, ErrorMessageDisplay, IOException, PropertyVetoException { UsersCache.putAll(getUserCacheContent()); MapCache.putAll(getMapBoardCacheContent()); UsersCachePoints.putAll(getUserPointCacheContent(new ArrayList(UsersCache.values()), new ArrayList(MapCache.values()))); MapCacheValues.putAll(getMapBoardCacheValues(new ArrayList(MapCache.values()), new ArrayList(UsersCachePoints.values()))); UserBannerCache.putAll(getUserBannerCacheContent(new ArrayList(UsersCachePoints.values()))); UserCacheTimeAmount.putAll(getUserTimeCache(new ArrayList(UsersCachePoints.values()), new ArrayList(MapCacheValues.values()))); UserCacheRecentTimes.putAll(getUserRecentTimesCache(UserCacheTimeAmount, MapCacheValues)); } }