From 74672f0bdcd5aae54e5ab6fcf76d7e92ca11e477 Mon Sep 17 00:00:00 2001 From: Flibio Date: Sun, 6 May 2018 20:02:04 -0600 Subject: [PATCH] Fix a caching issue --- .../economylite/api/PlayerEconService.java | 19 +++++++++-- .../economylite/api/VirtualEconService.java | 33 +++++++++++++------ .../economylite/impl/PlayerServiceCommon.java | 4 +-- .../impl/VirtualServiceCommon.java | 4 +-- 4 files changed, 43 insertions(+), 17 deletions(-) diff --git a/src/main/java/io/github/flibio/economylite/api/PlayerEconService.java b/src/main/java/io/github/flibio/economylite/api/PlayerEconService.java index c974174..5187525 100644 --- a/src/main/java/io/github/flibio/economylite/api/PlayerEconService.java +++ b/src/main/java/io/github/flibio/economylite/api/PlayerEconService.java @@ -23,9 +23,22 @@ public interface PlayerEconService { * @param uuid The UUID of the player to get the balance of. * @param currency The currency to use. * @param cause What is getting the balance. + * @param cache Whether not to utilize the cache. * @return The balance of the player. */ - public BigDecimal getBalance(UUID uuid, Currency currency, Cause cause); + public BigDecimal getBalance(UUID uuid, Currency currency, Cause cause, boolean cache); + + /** + * Gets the balance of a player. Caching is used. + * + * @param uuid The UUID of the player to get the balance of. + * @param currency The currency to use. + * @param cause What is getting the balance. + * @return The balance of the player. + */ + default public BigDecimal getBalance(UUID uuid, Currency currency, Cause cause) { + return getBalance(uuid, currency, cause, true); + } /** * Sets the balance of a player. @@ -48,7 +61,7 @@ public interface PlayerEconService { * @return If the player's balance was changed successfully. */ default public boolean withdraw(UUID uuid, BigDecimal amount, Currency currency, Cause cause) { - return setBalance(uuid, getBalance(uuid, currency, cause).subtract(amount), currency, cause); + return setBalance(uuid, getBalance(uuid, currency, cause, false).subtract(amount), currency, cause); } /** @@ -61,7 +74,7 @@ default public boolean withdraw(UUID uuid, BigDecimal amount, Currency currency, * @return If the player's balance was changed successfully. */ default public boolean deposit(UUID uuid, BigDecimal amount, Currency currency, Cause cause) { - return setBalance(uuid, getBalance(uuid, currency, cause).add(amount), currency, cause); + return setBalance(uuid, getBalance(uuid, currency, cause, false).add(amount), currency, cause); } /** diff --git a/src/main/java/io/github/flibio/economylite/api/VirtualEconService.java b/src/main/java/io/github/flibio/economylite/api/VirtualEconService.java index a3b4676..f17553a 100644 --- a/src/main/java/io/github/flibio/economylite/api/VirtualEconService.java +++ b/src/main/java/io/github/flibio/economylite/api/VirtualEconService.java @@ -18,17 +18,30 @@ public interface VirtualEconService { /** * Gets the balance of an account. - * + * * @param id The name of the account to get the balance of. * @param currency The currency to use. * @param cause What is getting the balance. + * @param cache Whether or not the cache will be used. * @return The balance of the account. */ - public BigDecimal getBalance(String id, Currency currency, Cause cause); + public BigDecimal getBalance(String id, Currency currency, Cause cause, boolean cache); + + /** + * Gets the balance of an account. Caching will be used. + * + * @param id The name of the account to get the balance of. + * @param currency The currency to use. + * @param cause What is getting the balance. + * @return The balance of the account. + */ + default public BigDecimal getBalance(String id, Currency currency, Cause cause) { + return getBalance(id, currency, cause, true); + } /** * Sets the balance of an account. - * + * * @param id The String of the player to set the balance of. * @param balance The new balance of the id. * @param currency The currency to use. @@ -39,7 +52,7 @@ public interface VirtualEconService { /** * Removes currency from an accounts's balance. - * + * * @param id The name of the account to remove currency from. * @param amount The amount of currency to remove. * @param currency The currency to use. @@ -47,12 +60,12 @@ public interface VirtualEconService { * @return If the account's balance was changed successfully. */ default public boolean withdraw(String id, BigDecimal amount, Currency currency, Cause cause) { - return setBalance(id, getBalance(id, currency, cause).subtract(amount), currency, cause); + return setBalance(id, getBalance(id, currency, cause, false).subtract(amount), currency, cause); } /** * Adds currency to an account's balance. - * + * * @param id The name of the account to add currency to. * @param amount The amount of currency to add. * @param currency The currency to use. @@ -60,12 +73,12 @@ default public boolean withdraw(String id, BigDecimal amount, Currency currency, * @return If the account's balance was changed successfully. */ default public boolean deposit(String id, BigDecimal amount, Currency currency, Cause cause) { - return setBalance(id, getBalance(id, currency, cause).add(amount), currency, cause); + return setBalance(id, getBalance(id, currency, cause, false).add(amount), currency, cause); } /** * Checks if an account exists in the system for the specified currency. - * + * * @param id The name of the account to check for. * @param currency The currency to check against. * @param cause What is checking if the account exists. @@ -75,7 +88,7 @@ default public boolean deposit(String id, BigDecimal amount, Currency currency, /** * Clears a currency from the database. - * + * * @param currency The currency to clear. * @param cause What is checking if the account exists. */ @@ -83,7 +96,7 @@ default public boolean deposit(String id, BigDecimal amount, Currency currency, /** * Gets the top virtual accounts registered in the EconomyLite system. - * + * * @param start The starting account to get. * @param end The ending account to get. * @param cause What is checking if the account exists. diff --git a/src/main/java/io/github/flibio/economylite/impl/PlayerServiceCommon.java b/src/main/java/io/github/flibio/economylite/impl/PlayerServiceCommon.java index fc0c9aa..e21767a 100644 --- a/src/main/java/io/github/flibio/economylite/impl/PlayerServiceCommon.java +++ b/src/main/java/io/github/flibio/economylite/impl/PlayerServiceCommon.java @@ -50,9 +50,9 @@ public boolean isWorking() { return manager.testConnection(); } - public BigDecimal getBalance(UUID uuid, Currency currency, Cause cause) { + public BigDecimal getBalance(UUID uuid, Currency currency, Cause cause, boolean cache) { BigDecimal result = balCache.getIfPresent(formId(uuid, currency)); - if (result != null) { + if (cache && result != null) { debug("playercommon: {C} Balance of '" + uuid.toString() + "' - " + cause.toString() + " = " + result.toPlainString()); return result; } diff --git a/src/main/java/io/github/flibio/economylite/impl/VirtualServiceCommon.java b/src/main/java/io/github/flibio/economylite/impl/VirtualServiceCommon.java index 18901b0..bb74dc2 100644 --- a/src/main/java/io/github/flibio/economylite/impl/VirtualServiceCommon.java +++ b/src/main/java/io/github/flibio/economylite/impl/VirtualServiceCommon.java @@ -53,9 +53,9 @@ public boolean isWorking() { return manager.testConnection(); } - public BigDecimal getBalance(String id, Currency currency, Cause cause) { + public BigDecimal getBalance(String id, Currency currency, Cause cause, boolean cache) { BigDecimal result = balCache.getIfPresent(formId(id, currency)); - if (result != null) { + if (cache && result != null) { debug("virtcommon: {C} Balance of '" + id + "' - " + cause.toString() + " = " + result.toPlainString()); return result; }