Adding cache to LeaguePointsDAO.

This commit is contained in:
marcins78@gmail.com
2011-12-15 14:57:12 +00:00
parent f1aecc6f18
commit 078236ad25
3 changed files with 70 additions and 7 deletions

View File

@@ -7,26 +7,29 @@ import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class LeaguePointsDAO {
private DbAccess _dbAccess;
private Map<League, List<Standing>> _leagueStandings = new ConcurrentHashMap<League, List<Standing>>();
private Map<LeagueSerie, List<Standing>> _serieStandings = new ConcurrentHashMap<LeagueSerie, List<Standing>>();
public LeaguePointsDAO(DbAccess dbAccess) {
_dbAccess = dbAccess;
}
public void addPoints(League league, LeagueSerie season, String playerName, int points) {
public synchronized void addPoints(League league, LeagueSerie serie, String playerName, int points) {
try {
Connection conn = _dbAccess.getDataSource().getConnection();
try {
PreparedStatement statement = conn.prepareStatement("insert into league_points (league_type, season_type, player_name, points) values (?, ?, ?, ?)");
try {
statement.setString(1, league.getType());
statement.setString(2, season.getType());
statement.setString(2, serie.getType());
statement.setString(3, playerName);
statement.setInt(4, points);
statement.execute();
@@ -39,22 +42,37 @@ public class LeaguePointsDAO {
} catch (SQLException exp) {
throw new RuntimeException(exp);
}
_leagueStandings.remove(league);
_serieStandings.remove(serie);
}
public Map<String, Integer> getLeagueStandings(League league) {
public List<Standing> getLeagueStandings(League league) {
List<Standing> standings = _leagueStandings.get(league);
if (standings == null) {
synchronized (this) {
standings = loadLeagueStandings(league);
_leagueStandings.put(league, standings);
}
}
return standings;
}
private List<Standing> loadLeagueStandings(League league) {
try {
Connection conn = _dbAccess.getDataSource().getConnection();
try {
PreparedStatement statement = conn.prepareStatement("select player_name, sum(points) from league_points where league_type=? order by 2 desc");
PreparedStatement statement = conn.prepareStatement("select player_name, sum(points), count(*) from league_points where league_type=? order by 2 desc");
try {
statement.setString(1, league.getType());
ResultSet rs = statement.executeQuery();
try {
Map<String, Integer> result = new LinkedHashMap<String, Integer>();
List<Standing> result = new LinkedList<Standing>();
while (rs.next()) {
String playerName = rs.getString(1);
int sumPoints = rs.getInt(2);
result.put(playerName, sumPoints);
int gamesPlayed = rs.getInt(3);
result.add(new Standing(playerName, sumPoints, gamesPlayed));
}
return result;
} finally {
@@ -72,6 +90,17 @@ public class LeaguePointsDAO {
}
public List<Standing> getSerieStandings(League league, LeagueSerie serie) {
List<Standing> standings = _serieStandings.get(serie);
if (standings == null) {
synchronized (this) {
standings = loadSerieStandings(league, serie);
_serieStandings.put(serie, standings);
}
}
return standings;
}
private List<Standing> loadSerieStandings(League league, LeagueSerie serie) {
try {
Connection conn = _dbAccess.getDataSource().getConnection();
try {

View File

@@ -42,4 +42,21 @@ public class League {
public int getStart() {
return _start;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
League league = (League) o;
if (_id != league._id) return false;
return true;
}
@Override
public int hashCode() {
return _id;
}
}

View File

@@ -28,4 +28,21 @@ public class LeagueSerie {
public int getStart() {
return _start;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
LeagueSerie that = (LeagueSerie) o;
if (_type != null ? !_type.equals(that._type) : that._type != null) return false;
return true;
}
@Override
public int hashCode() {
return _type != null ? _type.hashCode() : 0;
}
}