feat(cache): initial redis support
This commit is contained in:
parent
fecfb7300b
commit
87e998d578
1 changed files with 20 additions and 4 deletions
|
@ -1,5 +1,8 @@
|
||||||
package xyz.atnrch.nicko.storage.cache.redis;
|
package xyz.atnrch.nicko.storage.cache.redis;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.GsonBuilder;
|
||||||
|
import redis.clients.jedis.Jedis;
|
||||||
import xyz.atnrch.nicko.disguise.ActionResult;
|
import xyz.atnrch.nicko.disguise.ActionResult;
|
||||||
import xyz.atnrch.nicko.disguise.NickoProfile;
|
import xyz.atnrch.nicko.disguise.NickoProfile;
|
||||||
import xyz.atnrch.nicko.storage.cache.Cache;
|
import xyz.atnrch.nicko.storage.cache.Cache;
|
||||||
|
@ -9,23 +12,36 @@ import java.util.Optional;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
public class RedisCache extends Cache {
|
public class RedisCache extends Cache {
|
||||||
|
private final Gson gson = new GsonBuilder().serializeNulls().setPrettyPrinting().create();
|
||||||
|
private RedisCacheProvider provider;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CacheProvider getProvider() {
|
public CacheProvider getProvider() {
|
||||||
return null;
|
if (provider == null) {
|
||||||
|
provider = new RedisCacheProvider();
|
||||||
|
}
|
||||||
|
return provider;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ActionResult<Void> cache(UUID uuid, NickoProfile profile) {
|
public ActionResult<Void> cache(UUID uuid, NickoProfile profile) {
|
||||||
return null;
|
final Jedis jedis = provider.getJedis();
|
||||||
|
jedis.set("nicko:" + uuid.toString(), gson.toJson(profile));
|
||||||
|
return new ActionResult<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isCached(UUID uuid) {
|
public boolean isCached(UUID uuid) {
|
||||||
return false;
|
final Jedis jedis = provider.getJedis();
|
||||||
|
return jedis.exists("nicko:" + uuid.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<NickoProfile> retrieve(UUID uuid) {
|
public Optional<NickoProfile> retrieve(UUID uuid) {
|
||||||
return Optional.empty();
|
final Jedis jedis = provider.getJedis();
|
||||||
|
// TODO (Ineanto, 5/20/23): Check if cached before because Jedis returns a bulk reply so this is unsafe
|
||||||
|
final String data = jedis.get("nicko:" + uuid.toString());
|
||||||
|
final NickoProfile profile = gson.fromJson(data, NickoProfile.class);
|
||||||
|
return Optional.of(profile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue