Skip to content

Commit 63240e9

Browse files
committed
Fixed chaching issue
1 parent fd29586 commit 63240e9

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

src/main/java/me/leoko/advancedban/manager/PunishmentManager.java

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,23 @@ public void setup() {
5555
* @return the interim data
5656
*/
5757
public InterimData load(String name, String uuid, String ip) {
58-
Set<Punishment> punishments = new HashSet<>();
59-
Set<Punishment> history = new HashSet<>();
58+
Set<Punishment> punishments = new HashSet<>();
59+
Set<Punishment> history = new HashSet<>();
6060
try (ResultSet resultsPunishments = DatabaseManager.get().executeResultStatement(SQLQuery.SELECT_USER_PUNISHMENTS_WITH_IP, uuid, ip); ResultSet resultsHistory = DatabaseManager.get().executeResultStatement(SQLQuery.SELECT_USER_PUNISHMENTS_HISTORY_WITH_IP, uuid, ip)) {
61-
62-
while (resultsPunishments.next()) {
61+
if (resultsHistory == null || resultsPunishments == null)
62+
return null;
63+
64+
while (resultsPunishments.next()) {
6365
punishments.add(getPunishmentFromResultSet(resultsPunishments));
64-
}
66+
}
6567
while (resultsHistory.next()) {
6668
history.add(getPunishmentFromResultSet(resultsHistory));
6769
}
68-
70+
6971
} catch (SQLException ex) {
7072
universal.log("An error has occurred loading the punishments from the database.");
7173
universal.debugSqlException(ex);
74+
return null;
7275
}
7376
return new InterimData(uuid, name, ip, punishments, history);
7477
}
@@ -116,7 +119,7 @@ public List<Punishment> getPunishments(String target, PunishmentType put, boolea
116119
List<Punishment> ptList = new ArrayList<>();
117120

118121
if (isCached(target)) {
119-
for (Iterator<Punishment> iterator = (current ? punishments : history).iterator(); iterator.hasNext();) {
122+
for (Iterator<Punishment> iterator = (current ? punishments : history).iterator(); iterator.hasNext(); ) {
120123
Punishment pt = iterator.next();
121124
if ((put == null || put == pt.getType().getBasic()) && pt.getUuid().equals(target)) {
122125
if (!current || !pt.isExpired()) {
@@ -176,19 +179,26 @@ public List<Punishment> getPunishments(SQLQuery sqlQuery, Object... parameters)
176179
* @return the punishment
177180
*/
178181
public Punishment getPunishment(int id) {
179-
ResultSet rs = DatabaseManager.get().executeResultStatement(SQLQuery.SELECT_PUNISHMENT_BY_ID, id);
180-
Punishment pt = null;
181-
try {
182+
final Optional<Punishment> cachedPunishment = getLoadedPunishments(false).stream()
183+
.filter(punishment -> punishment.getId() == id).findAny();
184+
185+
if (cachedPunishment.isPresent())
186+
return cachedPunishment.get();
187+
188+
189+
try (ResultSet rs = DatabaseManager.get().executeResultStatement(SQLQuery.SELECT_PUNISHMENT_BY_ID, id)) {
182190
if (rs.next()) {
183-
pt = getPunishmentFromResultSet(rs);
191+
final Punishment punishment = getPunishmentFromResultSet(rs);
192+
if (!punishment.isExpired())
193+
return punishment;
184194
}
185-
rs.close();
186195
} catch (SQLException ex) {
187196
universal.log("An error has occurred getting a punishment by his id.");
188197
universal.debug("Punishment id: '" + id + "'");
189198
universal.debugSqlException(ex);
190199
}
191-
return pt == null || pt.isExpired() ? null : pt;
200+
201+
return null;
192202
}
193203

194204
/**
@@ -200,7 +210,7 @@ public Punishment getPunishment(int id) {
200210
public Punishment getWarn(int id) {
201211
Punishment punishment = getPunishment(id);
202212

203-
if(punishment == null)
213+
if (punishment == null)
204214
return null;
205215

206216
return punishment.getType().getBasic() == PunishmentType.WARNING ? punishment : null;
@@ -292,14 +302,14 @@ public int getCalculationLevel(String uuid, String layout) {
292302
if (isCached(uuid)) {
293303
return (int) history.stream().filter(pt -> pt.getUuid().equals(uuid) && layout.equalsIgnoreCase(pt.getCalculation())).count();
294304
}
295-
305+
296306
int i = 0;
297307
try (ResultSet resultSet = DatabaseManager.get().executeResultStatement(SQLQuery.SELECT_USER_PUNISHMENTS_HISTORY_BY_CALCULATION, uuid, layout)) {
298-
308+
299309
while (resultSet.next()) {
300-
i++;
310+
i++;
301311
}
302-
312+
303313
} catch (SQLException ex) {
304314
universal.log("An error has occurred getting the level for the layout '" + layout + "' for '" + uuid + "'");
305315
universal.debugSqlException(ex);

0 commit comments

Comments
 (0)