feat: working on transfer with cache
This commit is contained in:
parent
fb562b43b9
commit
e00875faa2
7 changed files with 178 additions and 1 deletions
|
@ -0,0 +1,31 @@
|
||||||
|
package net.artelnatif.nicko.config;
|
||||||
|
|
||||||
|
public class DataSourceConfiguration {
|
||||||
|
private final String address;
|
||||||
|
private final String port;
|
||||||
|
private final String username;
|
||||||
|
private final String password;
|
||||||
|
|
||||||
|
public DataSourceConfiguration(String address, String port, String username, String password) {
|
||||||
|
this.address = address;
|
||||||
|
this.port = port;
|
||||||
|
this.username = username;
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAddress() {
|
||||||
|
return address;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPort() {
|
||||||
|
return port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUsername() {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,47 @@
|
||||||
|
package net.artelnatif.nicko.config;
|
||||||
|
|
||||||
|
public class NewConfiguration {
|
||||||
|
private final DataSourceConfiguration sqlConfiguration;
|
||||||
|
private final DataSourceConfiguration redisConfiguration;
|
||||||
|
private final String prefix;
|
||||||
|
private final Boolean local;
|
||||||
|
private final Boolean customLocale;
|
||||||
|
|
||||||
|
public NewConfiguration(DataSourceConfiguration sqlConfiguration, DataSourceConfiguration redisConfiguration, String prefix, Boolean local, Boolean customLocale) {
|
||||||
|
this.sqlConfiguration = sqlConfiguration;
|
||||||
|
this.redisConfiguration = redisConfiguration;
|
||||||
|
this.prefix = prefix;
|
||||||
|
this.local = local;
|
||||||
|
this.customLocale = customLocale;
|
||||||
|
}
|
||||||
|
|
||||||
|
public NewConfiguration() {
|
||||||
|
this(
|
||||||
|
new DataSourceConfiguration("", "", "", ""),
|
||||||
|
new DataSourceConfiguration("", "", "", ""),
|
||||||
|
"",
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DataSourceConfiguration getSqlConfiguration() {
|
||||||
|
return sqlConfiguration;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DataSourceConfiguration getRedisConfiguration() {
|
||||||
|
return redisConfiguration;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPrefix() {
|
||||||
|
return prefix;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Boolean isLocal() {
|
||||||
|
return local;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Boolean isCustomLocale() {
|
||||||
|
return customLocale;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,11 +1,13 @@
|
||||||
package net.artelnatif.nicko.storage;
|
package net.artelnatif.nicko.storage;
|
||||||
|
|
||||||
import net.artelnatif.nicko.config.Configuration;
|
import net.artelnatif.nicko.config.Configuration;
|
||||||
import net.artelnatif.nicko.i18n.I18NDict;
|
|
||||||
import net.artelnatif.nicko.disguise.ActionResult;
|
import net.artelnatif.nicko.disguise.ActionResult;
|
||||||
import net.artelnatif.nicko.disguise.NickoProfile;
|
import net.artelnatif.nicko.disguise.NickoProfile;
|
||||||
|
import net.artelnatif.nicko.i18n.I18NDict;
|
||||||
import net.artelnatif.nicko.mojang.MojangAPI;
|
import net.artelnatif.nicko.mojang.MojangAPI;
|
||||||
import net.artelnatif.nicko.mojang.MojangUtils;
|
import net.artelnatif.nicko.mojang.MojangUtils;
|
||||||
|
import net.artelnatif.nicko.storage.cache.Cache;
|
||||||
|
import net.artelnatif.nicko.storage.cache.redis.RedisCache;
|
||||||
import net.artelnatif.nicko.storage.json.JSONStorage;
|
import net.artelnatif.nicko.storage.json.JSONStorage;
|
||||||
import net.artelnatif.nicko.storage.sql.SQLStorage;
|
import net.artelnatif.nicko.storage.sql.SQLStorage;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
@ -17,12 +19,14 @@ import java.util.UUID;
|
||||||
|
|
||||||
public class PlayerDataStore {
|
public class PlayerDataStore {
|
||||||
private final Storage storage;
|
private final Storage storage;
|
||||||
|
private final Cache cache;
|
||||||
private final MojangAPI mojangAPI;
|
private final MojangAPI mojangAPI;
|
||||||
private final HashMap<UUID, NickoProfile> profiles = new HashMap<>();
|
private final HashMap<UUID, NickoProfile> profiles = new HashMap<>();
|
||||||
|
|
||||||
public PlayerDataStore(MojangAPI mojangAPI, Configuration configuration) {
|
public PlayerDataStore(MojangAPI mojangAPI, Configuration configuration) {
|
||||||
this.mojangAPI = mojangAPI;
|
this.mojangAPI = mojangAPI;
|
||||||
this.storage = configuration.isLocal() ? new JSONStorage() : new SQLStorage(configuration);
|
this.storage = configuration.isLocal() ? new JSONStorage() : new SQLStorage(configuration);
|
||||||
|
this.cache = new RedisCache(); // The only option for now!
|
||||||
}
|
}
|
||||||
|
|
||||||
public void performProfileUpdate(UUID uuid, NickoProfile profile) {
|
public void performProfileUpdate(UUID uuid, NickoProfile profile) {
|
||||||
|
@ -81,4 +85,8 @@ public class PlayerDataStore {
|
||||||
public Storage getStorage() {
|
public Storage getStorage() {
|
||||||
return storage;
|
return storage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Cache getCache() {
|
||||||
|
return cache;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
27
core/src/main/java/net/artelnatif/nicko/storage/cache/Cache.java
vendored
Normal file
27
core/src/main/java/net/artelnatif/nicko/storage/cache/Cache.java
vendored
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
package net.artelnatif.nicko.storage.cache;
|
||||||
|
|
||||||
|
import net.artelnatif.nicko.disguise.ActionResult;
|
||||||
|
import net.artelnatif.nicko.disguise.NickoProfile;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public abstract class Cache {
|
||||||
|
private boolean error = false;
|
||||||
|
|
||||||
|
public abstract CacheProvider getProvider();
|
||||||
|
|
||||||
|
public abstract ActionResult<Void> cache(UUID uuid, NickoProfile profile);
|
||||||
|
|
||||||
|
public abstract boolean isCached(UUID uuid);
|
||||||
|
|
||||||
|
public abstract Optional<NickoProfile> retrieve(UUID uuid);
|
||||||
|
|
||||||
|
public boolean isError() {
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setError(boolean error) {
|
||||||
|
this.error = error;
|
||||||
|
}
|
||||||
|
}
|
7
core/src/main/java/net/artelnatif/nicko/storage/cache/CacheProvider.java
vendored
Normal file
7
core/src/main/java/net/artelnatif/nicko/storage/cache/CacheProvider.java
vendored
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
package net.artelnatif.nicko.storage.cache;
|
||||||
|
|
||||||
|
public interface CacheProvider {
|
||||||
|
boolean init();
|
||||||
|
|
||||||
|
boolean close();
|
||||||
|
}
|
31
core/src/main/java/net/artelnatif/nicko/storage/cache/redis/RedisCache.java
vendored
Normal file
31
core/src/main/java/net/artelnatif/nicko/storage/cache/redis/RedisCache.java
vendored
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
package net.artelnatif.nicko.storage.cache.redis;
|
||||||
|
|
||||||
|
import net.artelnatif.nicko.disguise.ActionResult;
|
||||||
|
import net.artelnatif.nicko.disguise.NickoProfile;
|
||||||
|
import net.artelnatif.nicko.storage.cache.Cache;
|
||||||
|
import net.artelnatif.nicko.storage.cache.CacheProvider;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class RedisCache extends Cache {
|
||||||
|
@Override
|
||||||
|
public CacheProvider getProvider() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ActionResult<Void> cache(UUID uuid, NickoProfile profile) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isCached(UUID uuid) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<NickoProfile> retrieve(UUID uuid) {
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
}
|
26
core/src/main/java/net/artelnatif/nicko/storage/cache/redis/RedisCacheProvider.java
vendored
Normal file
26
core/src/main/java/net/artelnatif/nicko/storage/cache/redis/RedisCacheProvider.java
vendored
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
package net.artelnatif.nicko.storage.cache.redis;
|
||||||
|
|
||||||
|
import net.artelnatif.nicko.storage.cache.CacheProvider;
|
||||||
|
import redis.clients.jedis.Jedis;
|
||||||
|
import redis.clients.jedis.JedisPool;
|
||||||
|
|
||||||
|
public class RedisCacheProvider implements CacheProvider {
|
||||||
|
private JedisPool pool;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean init() {
|
||||||
|
// TODO: 3/12/23 Get port from configuration
|
||||||
|
pool = new JedisPool("localhost", 6379);
|
||||||
|
return !pool.isClosed() && pool.getResource() != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean close() {
|
||||||
|
pool.close();
|
||||||
|
return pool.isClosed();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Jedis getJedis() {
|
||||||
|
return pool.getResource();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue