mirror of
https://github.com/ChestShop-authors/ChestShop-3.git
synced 2024-11-12 13:34:52 +01:00
Move cache to own class in Breeze package
This commit is contained in:
parent
21815197c6
commit
b22d8fd95d
46
src/main/java/com/Acrobot/Breeze/Collection/SimpleCache.java
Normal file
46
src/main/java/com/Acrobot/Breeze/Collection/SimpleCache.java
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
package com.Acrobot.Breeze.Collection;
|
||||||
|
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
|
public class SimpleCache<K, V> {
|
||||||
|
private final LinkedHashMap<K, V> map;
|
||||||
|
|
||||||
|
public SimpleCache(int cacheSize) {
|
||||||
|
map = new LinkedHashMap<K, V>(cacheSize * 10/9, 0.7f, true) {
|
||||||
|
@Override
|
||||||
|
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
|
||||||
|
return size() > cacheSize;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public V put(K key, V value) {
|
||||||
|
return map.put(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public V get(K key) {
|
||||||
|
return map.get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public V get(K key, Callable<? extends V> loader) throws ExecutionException {
|
||||||
|
if (contains(key)) {
|
||||||
|
return map.get(key);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
V value = loader.call();
|
||||||
|
if (value != null) {
|
||||||
|
put(key, value);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new ExecutionException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean contains(K key) {
|
||||||
|
return map.containsKey(key);
|
||||||
|
}
|
||||||
|
}
|
@ -2,6 +2,7 @@ package com.Acrobot.ChestShop.UUIDs;
|
|||||||
|
|
||||||
import com.Acrobot.Breeze.Utils.Encoding.Base62;
|
import com.Acrobot.Breeze.Utils.Encoding.Base62;
|
||||||
import com.Acrobot.Breeze.Utils.NameUtil;
|
import com.Acrobot.Breeze.Utils.NameUtil;
|
||||||
|
import com.Acrobot.Breeze.Collection.SimpleCache;
|
||||||
import com.Acrobot.ChestShop.ChestShop;
|
import com.Acrobot.ChestShop.ChestShop;
|
||||||
import com.Acrobot.ChestShop.Configuration.Properties;
|
import com.Acrobot.ChestShop.Configuration.Properties;
|
||||||
import com.Acrobot.ChestShop.Database.Account;
|
import com.Acrobot.ChestShop.Database.Account;
|
||||||
@ -17,10 +18,7 @@ import org.bukkit.entity.Player;
|
|||||||
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.LinkedHashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.Callable;
|
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
@ -33,9 +31,9 @@ import java.util.logging.Level;
|
|||||||
public class NameManager {
|
public class NameManager {
|
||||||
private static Dao<Account, String> accounts;
|
private static Dao<Account, String> accounts;
|
||||||
|
|
||||||
private static SimpleLoadingCache<String, Account> usernameToAccount = new SimpleLoadingCache<>(Properties.CACHE_SIZE);
|
private static SimpleCache<String, Account> usernameToAccount = new SimpleCache<>(Properties.CACHE_SIZE);
|
||||||
private static SimpleLoadingCache<UUID, Account> uuidToAccount = new SimpleLoadingCache<>(Properties.CACHE_SIZE);
|
private static SimpleCache<UUID, Account> uuidToAccount = new SimpleCache<>(Properties.CACHE_SIZE);
|
||||||
private static SimpleLoadingCache<String, Account> shortToAccount = new SimpleLoadingCache<>(Properties.CACHE_SIZE);
|
private static SimpleCache<String, Account> shortToAccount = new SimpleCache<>(Properties.CACHE_SIZE);
|
||||||
|
|
||||||
private static Account adminAccount;
|
private static Account adminAccount;
|
||||||
private static Account serverEconomyAccount;
|
private static Account serverEconomyAccount;
|
||||||
@ -309,43 +307,4 @@ public class NameManager {
|
|||||||
return serverEconomyAccount;
|
return serverEconomyAccount;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class SimpleLoadingCache<K, V> {
|
|
||||||
private final LinkedHashMap<K, V> map;
|
|
||||||
|
|
||||||
public SimpleLoadingCache(int cacheSize) {
|
|
||||||
map = new LinkedHashMap<K, V>(cacheSize * 10/9, 0.7f, true) {
|
|
||||||
@Override
|
|
||||||
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
|
|
||||||
return size() > cacheSize;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
public void put(K key, V value) {
|
|
||||||
map.put(key, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public V get(K key) {
|
|
||||||
return map.get(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
public V get(K key, Callable<? extends V> loader) throws ExecutionException {
|
|
||||||
if (contains(key)) {
|
|
||||||
return map.get(key);
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
V value = loader.call();
|
|
||||||
if (value != null) {
|
|
||||||
put(key, value);
|
|
||||||
}
|
|
||||||
return value;
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new ExecutionException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean contains(K key) {
|
|
||||||
return map.containsKey(key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user