Caching league matches and points.
This commit is contained in:
@@ -115,5 +115,10 @@ public class LeaguePointsDAO {
|
||||
public int getPoints() {
|
||||
return _points;
|
||||
}
|
||||
|
||||
public void addPointsForMatch(int points) {
|
||||
_points += points;
|
||||
_gamesPlayed++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ public class LeagueService {
|
||||
|
||||
private LeagueDAO _leagueDao;
|
||||
private LeaguePointsDAO _leaguePointsDao;
|
||||
// Cached on this layer
|
||||
private LeagueMatchDAO _leagueMatchDao;
|
||||
private LeagueParticipationDAO _leagueParticipationDAO;
|
||||
private CollectionsManager _collectionsManager;
|
||||
@@ -44,6 +45,9 @@ public class LeagueService {
|
||||
private ExpireObjectCache<League, Collection<LeagueMatch>> _cachedLeagueMatches = new ExpireObjectCache<League, Collection<LeagueMatch>>();
|
||||
private ExpireObjectCache<LeagueSerieData, Collection<LeagueMatch>> _cachedSerieMatches = new ExpireObjectCache<LeagueSerieData, Collection<LeagueMatch>>();
|
||||
|
||||
private ExpireObjectCache<League, Map<String, LeaguePointsDAO.Points>> _cachedLeaguePoints = new ExpireObjectCache<League, Map<String, LeaguePointsDAO.Points>>();
|
||||
private ExpireObjectCache<LeagueSerieData, Map<String, LeaguePointsDAO.Points>> _cachedSeriePoints = new ExpireObjectCache<LeagueSerieData, Map<String, LeaguePointsDAO.Points>>();
|
||||
|
||||
private int _activeLeaguesLoadedDate;
|
||||
private List<League> _activeLeagues;
|
||||
|
||||
@@ -99,6 +103,26 @@ public class LeagueService {
|
||||
});
|
||||
}
|
||||
|
||||
private Map<String, LeaguePointsDAO.Points> getLeaguePoints(final League league) {
|
||||
return _cachedLeaguePoints.getCachedObject(league,
|
||||
new Producable<League, Map<String, LeaguePointsDAO.Points>>() {
|
||||
@Override
|
||||
public Map<String, LeaguePointsDAO.Points> produce(League key) {
|
||||
return new ConcurrentHashMap<String, LeaguePointsDAO.Points>(_leaguePointsDao.getLeaguePoints(league));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private Map<String, LeaguePointsDAO.Points> getLeagueSeriePoints(final League league, final LeagueSerieData serie) {
|
||||
return _cachedSeriePoints.getCachedObject(serie,
|
||||
new Producable<LeagueSerieData, Map<String, LeaguePointsDAO.Points>>() {
|
||||
@Override
|
||||
public Map<String, LeaguePointsDAO.Points> produce(LeagueSerieData key) {
|
||||
return new ConcurrentHashMap<String, LeaguePointsDAO.Points>(_leaguePointsDao.getLeagueSeriePoints(league, serie));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public List<League> getActiveLeagues() {
|
||||
if (DateUtils.getCurrentDate() == _activeLeaguesLoadedDate)
|
||||
return Collections.unmodifiableList(_activeLeagues);
|
||||
@@ -204,8 +228,9 @@ public class LeagueService {
|
||||
public void reportLeagueGameResult(League league, LeagueSerieData serie, String winner, String loser) {
|
||||
addMatch(league, serie, winner, loser);
|
||||
|
||||
_leaguePointsDao.addPoints(league, serie, winner, 2);
|
||||
_leaguePointsDao.addPoints(league, serie, loser, 1);
|
||||
addPoints(league, serie, winner, 2);
|
||||
addPoints(league, serie, loser, 1);
|
||||
|
||||
_leagueStandings.remove(league);
|
||||
_leagueSerieStandings.remove(serie);
|
||||
|
||||
@@ -213,6 +238,34 @@ public class LeagueService {
|
||||
awardPrizesToPlayer(league, serie, loser, false);
|
||||
}
|
||||
|
||||
private void addPoints(League league, LeagueSerieData serie, String winner, int points) {
|
||||
_leaguePointsDao.addPoints(league, serie, winner, points);
|
||||
storeNewLeaguePoints(league, winner, points);
|
||||
storeNewLeagueSeriePoints(league, serie, winner, points);
|
||||
}
|
||||
|
||||
private void storeNewLeagueSeriePoints(League league, LeagueSerieData serie, String winner, int points) {
|
||||
final Map<String, LeaguePointsDAO.Points> leagueSeriePoints = getLeagueSeriePoints(league, serie);
|
||||
final LeaguePointsDAO.Points playerPoints = leagueSeriePoints.get(winner);
|
||||
if (playerPoints != null)
|
||||
playerPoints.addPointsForMatch(points);
|
||||
else {
|
||||
LeaguePointsDAO.Points newPoints = new LeaguePointsDAO.Points(points, 1);
|
||||
leagueSeriePoints.put(winner, newPoints);
|
||||
}
|
||||
}
|
||||
|
||||
private void storeNewLeaguePoints(League league, String winner, int points) {
|
||||
final Map<String, LeaguePointsDAO.Points> leaguePoints = getLeaguePoints(league);
|
||||
final LeaguePointsDAO.Points playerPoints = leaguePoints.get(winner);
|
||||
if (playerPoints != null)
|
||||
playerPoints.addPointsForMatch(points);
|
||||
else {
|
||||
LeaguePointsDAO.Points newPoints = new LeaguePointsDAO.Points(points, 1);
|
||||
leaguePoints.put(winner, newPoints);
|
||||
}
|
||||
}
|
||||
|
||||
private void addMatch(League league, LeagueSerieData serie, String winner, String loser) {
|
||||
LeagueMatch match = new LeagueMatch(winner, loser);
|
||||
_leagueMatchDao.addPlayedMatch(league, serie, match);
|
||||
@@ -270,14 +323,14 @@ public class LeagueService {
|
||||
}
|
||||
|
||||
private List<LeagueStanding> createLeagueSerieStandings(League league, LeagueSerieData leagueSerie) {
|
||||
final Map<String, LeaguePointsDAO.Points> points = _leaguePointsDao.getLeagueSeriePoints(league, leagueSerie);
|
||||
final Map<String, LeaguePointsDAO.Points> points = getLeagueSeriePoints(league, leagueSerie);
|
||||
final Collection<LeagueMatch> matches = getLeagueSerieMatches(league, leagueSerie);
|
||||
|
||||
return createStandingsForMatchesAndPoints(points, matches);
|
||||
}
|
||||
|
||||
private List<LeagueStanding> createLeagueStandings(League league) {
|
||||
final Map<String, LeaguePointsDAO.Points> points = _leaguePointsDao.getLeaguePoints(league);
|
||||
final Map<String, LeaguePointsDAO.Points> points = getLeaguePoints(league);
|
||||
final Collection<LeagueMatch> matches = getLeagueMatches(league);
|
||||
|
||||
return createStandingsForMatchesAndPoints(points, matches);
|
||||
|
||||
Reference in New Issue
Block a user