This commit is contained in:
zaCade
2019-03-02 15:17:41 +01:00
parent 57bbd5d55e
commit d1fa3d4eb7
464 changed files with 8280 additions and 0 deletions
@@ -0,0 +1,220 @@
/*
* 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<Integer, Users> UsersCache;
private final ConcurrentMap<Integer, Users> UsersCachePoints;
private final ConcurrentMap<Integer, MapBoard> MapCache;
private final ConcurrentMap<Integer, MapBoard> MapCacheValues;
private final ConcurrentMap<Integer, UserBanners> UserBannerCache;
private final ConcurrentMap<Integer, Users> UserCacheTimeAmount;
private final ConcurrentMap<Integer, RecentTimes> 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<Integer, RecentTimes> getUserRecentTimesCache(ConcurrentMap<Integer, Users> UserCacheTimeAmount1, ConcurrentMap<Integer, MapBoard> mpb) throws ErrorMessageDisplay {
LinkedHashMap<Integer, RecentTimes> LHM = LogicFacade.getRecentTimes(UserCacheTimeAmount1, mpb);
return LHM;
}
private Map<Integer, Users> getUserCacheContent() throws SQLException, ErrorMessageDisplay, IOException, PropertyVetoException {
List<Users> Users;
Users = LogicFacade.getAllUsersNameSteamIdInitialization();
LinkedHashMap<Integer, Users> LHM = new LinkedHashMap();
for (int i = 0; i < Users.size(); i++) {
LHM.put(i, Users.get(i));
}
return LHM;
}
private Map<Integer, MapBoard> getMapBoardCacheContent() throws ErrorMessageDisplay, SQLException, IOException, PropertyVetoException {
List<MapBoard> MB;
MB = LogicFacade.GetAllMapColoumns();
LinkedHashMap<Integer, MapBoard> LHM = new LinkedHashMap();
for (int i = 0; i < MB.size(); i++) {
LHM.put(i, MB.get(i));
}
return LHM;
}
private Map<Integer, Users> getUserPointCacheContent(List<Users> PlayerLeaderBoard, List<MapBoard> mapBoard) throws ErrorMessageDisplay, SQLException, IOException, PropertyVetoException {
List<Users> Users;
Users = LogicFacade.getPlayerPointsAndRankInitialization(PlayerLeaderBoard, mapBoard);
LinkedHashMap<Integer, Users> LHM = new LinkedHashMap();
for (int i = 0; i < Users.size(); i++) {
LHM.put(i, Users.get(i));
}
return LHM;
}
private Map<Integer, MapBoard> getMapBoardCacheValues(List<MapBoard> mapBoard, List<Users> PlayerLeaderBoard) throws ErrorMessageDisplay {
List<MapBoard> MB;
MB = LogicFacade.getAllMapValues(mapBoard, PlayerLeaderBoard);
LinkedHashMap<Integer, MapBoard> LHM = new LinkedHashMap();
for (int i = 0; i < MB.size(); i++) {
LHM.put(i, MB.get(i));
}
return LHM;
}
private Map<Integer, UserBanners> getUserBannerCacheContent(List<Users> PlayerLeaderBoard) throws ErrorMessageDisplay {
List<UserBanners> UB;
UB = LogicFacade.getUserBanners(PlayerLeaderBoard);
LinkedHashMap<Integer, UserBanners> LHM = new LinkedHashMap();
for (int i = 0; i < UB.size(); i++) {
LHM.put(i, UB.get(i));
}
return LHM;
}
private Map<Integer, Users> getUserTimeCache(List<Users> PlayerLeaderBoard, List<MapBoard> mapBoard) {
List<Users> Users;
Users = LogicFacade.getPlayertimeAmount(PlayerLeaderBoard, mapBoard);
LinkedHashMap<Integer, Users> LHM = new LinkedHashMap();
for (int i = 0; i < Users.size(); i++) {
LHM.put(i, Users.get(i));
}
return LHM;
}
public List<MapBoard> getAllMapValues() {
List<MapBoard> MapColoumns = new ArrayList(MapCacheValues.values());
return MapColoumns;
}
public List<UserBanners> getUserBanners() {
List<UserBanners> UB = new ArrayList(UserBannerCache.values());
return UB;
}
public List<Users> getAllPlayerValues() {
List<Users> Users = new ArrayList(UserCacheTimeAmount.values());
return Users;
}
public List<RecentTimes> getAllRecentTimes() {
List<RecentTimes> 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));
}
}