Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Commit

Permalink
Fix a caching issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Flibio committed May 7, 2018
1 parent 4f29565 commit 74672f0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -39,33 +52,33 @@ 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.
* @param cause What caused the balance change.
* @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.
* @param cause What caused the balance change.
* @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.
Expand All @@ -75,15 +88,15 @@ 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.
*/
public void clearCurrency(Currency currency, Cause cause);

/**
* 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 74672f0

Please sign in to comment.