mirror of
https://github.com/Xiao-MoMi/Custom-Fishing.git
synced 2025-12-23 08:59:34 +00:00
Cache the json data
This commit is contained in:
@@ -18,6 +18,7 @@
|
|||||||
package net.momirealms.customfishing.api.storage.data;
|
package net.momirealms.customfishing.api.storage.data;
|
||||||
|
|
||||||
import com.google.gson.annotations.SerializedName;
|
import com.google.gson.annotations.SerializedName;
|
||||||
|
import net.momirealms.customfishing.api.BukkitCustomFishingPlugin;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
@@ -46,6 +47,7 @@ public class PlayerData {
|
|||||||
protected EarningData earningData;
|
protected EarningData earningData;
|
||||||
transient private UUID uuid;
|
transient private UUID uuid;
|
||||||
transient private boolean locked;
|
transient private boolean locked;
|
||||||
|
transient private byte[] jsonBytes;
|
||||||
|
|
||||||
public PlayerData(UUID uuid, String name, StatisticData statisticsData, InventoryData bagData, EarningData earningData, boolean isLocked) {
|
public PlayerData(UUID uuid, String name, StatisticData statisticsData, InventoryData bagData, EarningData earningData, boolean isLocked) {
|
||||||
this.name = name;
|
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.
|
* Gets the statistics data for the player.
|
||||||
*
|
*
|
||||||
@@ -169,6 +178,11 @@ public class PlayerData {
|
|||||||
return locked;
|
return locked;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set if the data is locked
|
||||||
|
*
|
||||||
|
* @param locked locked or not
|
||||||
|
*/
|
||||||
public void locked(boolean locked) {
|
public void locked(boolean locked) {
|
||||||
this.locked = locked;
|
this.locked = locked;
|
||||||
}
|
}
|
||||||
@@ -182,6 +196,11 @@ public class PlayerData {
|
|||||||
return uuid;
|
return uuid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the uuid of the data
|
||||||
|
*
|
||||||
|
* @param uuid uuid
|
||||||
|
*/
|
||||||
public void uuid(UUID uuid) {
|
public void uuid(UUID uuid) {
|
||||||
this.uuid = uuid;
|
this.uuid = uuid;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -152,7 +152,7 @@ public class MongoDBProvider extends AbstractStorage {
|
|||||||
Document query = new Document("uuid", uuid);
|
Document query = new Document("uuid", uuid);
|
||||||
Bson updates = Updates.combine(
|
Bson updates = Updates.combine(
|
||||||
Updates.set("lock", unlock ? 0 : getCurrentSeconds()),
|
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);
|
UpdateOptions options = new UpdateOptions().upsert(true);
|
||||||
UpdateResult result = collection.updateOne(query, updates, options);
|
UpdateResult result = collection.updateOne(query, updates, options);
|
||||||
future.complete(result.wasAcknowledged());
|
future.complete(result.wasAcknowledged());
|
||||||
|
|||||||
@@ -296,7 +296,7 @@ public class RedisManager extends AbstractStorage {
|
|||||||
jedis.setex(
|
jedis.setex(
|
||||||
getRedisKey("cf_data", uuid),
|
getRedisKey("cf_data", uuid),
|
||||||
10,
|
10,
|
||||||
plugin.getStorageManager().toBytes(playerData)
|
playerData.toBytes()
|
||||||
);
|
);
|
||||||
future.complete(true);
|
future.complete(true);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|||||||
@@ -168,7 +168,7 @@ public abstract class AbstractSQLDatabase extends AbstractStorage {
|
|||||||
PreparedStatement statement = connection.prepareStatement(String.format(SqlConstants.SQL_UPDATE_BY_UUID, getTableName("data")))
|
PreparedStatement statement = connection.prepareStatement(String.format(SqlConstants.SQL_UPDATE_BY_UUID, getTableName("data")))
|
||||||
) {
|
) {
|
||||||
statement.setInt(1, unlock ? 0 : getCurrentSeconds());
|
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.setString(3, uuid.toString());
|
||||||
statement.executeUpdate();
|
statement.executeUpdate();
|
||||||
future.complete(true);
|
future.complete(true);
|
||||||
|
|||||||
@@ -193,7 +193,7 @@ public class SQLiteProvider extends AbstractSQLDatabase {
|
|||||||
PreparedStatement statement = connection.prepareStatement(String.format(SqlConstants.SQL_UPDATE_BY_UUID, getTableName("data")))
|
PreparedStatement statement = connection.prepareStatement(String.format(SqlConstants.SQL_UPDATE_BY_UUID, getTableName("data")))
|
||||||
) {
|
) {
|
||||||
statement.setInt(1, unlock ? 0 : getCurrentSeconds());
|
statement.setInt(1, unlock ? 0 : getCurrentSeconds());
|
||||||
statement.setBytes(2, plugin.getStorageManager().toBytes(playerData));
|
statement.setBytes(2, playerData.toBytes());
|
||||||
statement.setString(3, uuid.toString());
|
statement.setString(3, uuid.toString());
|
||||||
statement.executeUpdate();
|
statement.executeUpdate();
|
||||||
future.complete(true);
|
future.complete(true);
|
||||||
|
|||||||
Reference in New Issue
Block a user