Skip to content

Commit

Permalink
Cache db lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Nov 4, 2024
1 parent 5b2ef57 commit 0ead2c9
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/main/java/net/pistonmaster/pistonchat/tools/TempDataTool.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package net.pistonmaster.pistonchat.tools;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import lombok.RequiredArgsConstructor;
import net.pistonmaster.pistonchat.PistonChat;
import org.bukkit.entity.Player;
Expand All @@ -8,10 +11,18 @@
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RequiredArgsConstructor
public class TempDataTool {
private final PistonChat plugin;
private final LoadingCache<UUID, Boolean> whisperCache = Caffeine.newBuilder()
.expireAfterWrite(5, TimeUnit.SECONDS)
.build(this::loadIsWhisperingEnabled);
private final LoadingCache<UUID, Boolean> chatCache = Caffeine.newBuilder()
.expireAfterWrite(5, TimeUnit.SECONDS)
.build(this::loadIsChatEnabled);

public void setWhisperingEnabled(Player player, boolean value) {
try (Connection connection = plugin.getDs().getConnection()) {
Expand All @@ -20,6 +31,8 @@ public void setWhisperingEnabled(Player player, boolean value) {
statement.setBoolean(2, value);
statement.setBoolean(3, value);
statement.execute();

whisperCache.put(player.getUniqueId(), value);
} catch (SQLException e) {
throw new RuntimeException(e);
}
Expand All @@ -32,15 +45,22 @@ public void setChatEnabled(Player player, boolean value) {
statement.setBoolean(2, value);
statement.setBoolean(3, value);
statement.execute();

chatCache.put(player.getUniqueId(), value);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}

@SuppressWarnings("BooleanMethodIsAlwaysInverted")
public boolean isWhisperingEnabled(Player player) {
return Boolean.TRUE.equals(whisperCache.get(player.getUniqueId()));
}

public boolean loadIsWhisperingEnabled(UUID uuid) {
try (Connection connection = plugin.getDs().getConnection()) {
PreparedStatement statement = connection.prepareStatement("SELECT `whisper_enabled` FROM `pistonchat_settings_whisper` WHERE `uuid` = ?;");
statement.setString(1, player.getUniqueId().toString());
statement.setString(1, uuid.toString());

ResultSet resultSet = statement.executeQuery();
if (!resultSet.next()) {
Expand All @@ -53,10 +73,15 @@ public boolean isWhisperingEnabled(Player player) {
}
}

@SuppressWarnings("BooleanMethodIsAlwaysInverted")
public boolean isChatEnabled(Player player) {
return Boolean.TRUE.equals(chatCache.get(player.getUniqueId()));
}

public boolean loadIsChatEnabled(UUID uuid) {
try (Connection connection = plugin.getDs().getConnection()) {
PreparedStatement statement = connection.prepareStatement("SELECT `chat_enabled` FROM `pistonchat_settings_chat` WHERE `uuid` = ?;");
statement.setString(1, player.getUniqueId().toString());
statement.setString(1, uuid.toString());

ResultSet resultSet = statement.executeQuery();
if (!resultSet.next()) {
Expand Down

0 comments on commit 0ead2c9

Please sign in to comment.