9
0
mirror of https://github.com/Xiao-MoMi/Custom-Fishing.git synced 2025-12-19 15:09:24 +00:00

Cache the json data

This commit is contained in:
XiaoMoMi
2024-07-31 01:15:30 +08:00
parent e819f6b079
commit 5b2ad3c854
5 changed files with 23 additions and 4 deletions

View File

@@ -18,6 +18,7 @@
package net.momirealms.customfishing.api.storage.data;
import com.google.gson.annotations.SerializedName;
import net.momirealms.customfishing.api.BukkitCustomFishingPlugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -46,6 +47,7 @@ public class PlayerData {
protected EarningData earningData;
transient private UUID uuid;
transient private boolean locked;
transient private byte[] jsonBytes;
public PlayerData(UUID uuid, String name, StatisticData statisticsData, InventoryData bagData, EarningData earningData, boolean isLocked) {
this.name = name;
@@ -124,6 +126,13 @@ public class PlayerData {
}
}
public byte[] toBytes() {
if (jsonBytes == null) {
jsonBytes = BukkitCustomFishingPlugin.getInstance().getStorageManager().toBytes(this);
}
return jsonBytes;
}
/**
* Gets the statistics data for the player.
*
@@ -169,6 +178,11 @@ public class PlayerData {
return locked;
}
/**
* Set if the data is locked
*
* @param locked locked or not
*/
public void locked(boolean locked) {
this.locked = locked;
}
@@ -182,6 +196,11 @@ public class PlayerData {
return uuid;
}
/**
* Set the uuid of the data
*
* @param uuid uuid
*/
public void uuid(UUID uuid) {
this.uuid = uuid;
}

View File

@@ -152,7 +152,7 @@ public class MongoDBProvider extends AbstractStorage {
Document query = new Document("uuid", uuid);
Bson updates = Updates.combine(
Updates.set("lock", unlock ? 0 : getCurrentSeconds()),
Updates.set("data", new Binary(plugin.getStorageManager().toBytes(playerData))));
Updates.set("data", new Binary(playerData.toBytes())));
UpdateOptions options = new UpdateOptions().upsert(true);
UpdateResult result = collection.updateOne(query, updates, options);
future.complete(result.wasAcknowledged());

View File

@@ -296,7 +296,7 @@ public class RedisManager extends AbstractStorage {
jedis.setex(
getRedisKey("cf_data", uuid),
10,
plugin.getStorageManager().toBytes(playerData)
playerData.toBytes()
);
future.complete(true);
} catch (Exception e) {

View File

@@ -168,7 +168,7 @@ public abstract class AbstractSQLDatabase extends AbstractStorage {
PreparedStatement statement = connection.prepareStatement(String.format(SqlConstants.SQL_UPDATE_BY_UUID, getTableName("data")))
) {
statement.setInt(1, unlock ? 0 : getCurrentSeconds());
statement.setBlob(2, new ByteArrayInputStream(plugin.getStorageManager().toBytes(playerData)));
statement.setBlob(2, new ByteArrayInputStream(playerData.toBytes()));
statement.setString(3, uuid.toString());
statement.executeUpdate();
future.complete(true);

View File

@@ -193,7 +193,7 @@ public class SQLiteProvider extends AbstractSQLDatabase {
PreparedStatement statement = connection.prepareStatement(String.format(SqlConstants.SQL_UPDATE_BY_UUID, getTableName("data")))
) {
statement.setInt(1, unlock ? 0 : getCurrentSeconds());
statement.setBytes(2, plugin.getStorageManager().toBytes(playerData));
statement.setBytes(2, playerData.toBytes());
statement.setString(3, uuid.toString());
statement.executeUpdate();
future.complete(true);