mirror of
https://github.com/proxiodev/RedisBungee.git
synced 2026-04-16 11:50:27 +00:00
Added basic expiry to cache and tests.
This commit is contained in:
@@ -26,8 +26,6 @@
|
||||
*/
|
||||
package com.imaginarycode.minecraft.redisbungee;
|
||||
|
||||
import com.google.common.cache.Cache;
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import com.google.common.net.InetAddresses;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
@@ -50,6 +48,7 @@ import java.net.InetAddress;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.Level;
|
||||
|
||||
/**
|
||||
@@ -57,18 +56,34 @@ import java.util.logging.Level;
|
||||
*
|
||||
* @since 0.3.3
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
public class DataManager implements Listener {
|
||||
private final RedisBungee plugin;
|
||||
// TODO: Add cleanup for this.
|
||||
private final InternalCache<UUID, String> serverCache = createCache();
|
||||
private final InternalCache<UUID, String> proxyCache = createCache();
|
||||
private final InternalCache<UUID, InetAddress> ipCache = createCache();
|
||||
private final InternalCache<UUID, Long> lastOnlineCache = createCache();
|
||||
private final InternalCache<UUID, String> proxyCache = createCache(TimeUnit.MINUTES.toMillis(60));
|
||||
private final InternalCache<UUID, InetAddress> ipCache = createCache(TimeUnit.MINUTES.toMillis(60));
|
||||
private final InternalCache<UUID, Long> lastOnlineCache = createCache(TimeUnit.MINUTES.toMillis(60));
|
||||
|
||||
public DataManager(RedisBungee plugin) {
|
||||
this.plugin = plugin;
|
||||
plugin.getProxy().getScheduler().schedule(plugin, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
proxyCache.cleanup();
|
||||
ipCache.cleanup();
|
||||
lastOnlineCache.cleanup();
|
||||
}
|
||||
}, 1, 1, TimeUnit.MINUTES);
|
||||
}
|
||||
|
||||
public static <K, V> InternalCache<K, V> createCache() {
|
||||
return new InternalCache<>();
|
||||
}
|
||||
|
||||
public static <K, V> InternalCache<K, V> createCache(long entryWriteExpiry) {
|
||||
return new InternalCache<>(entryWriteExpiry);
|
||||
}
|
||||
|
||||
private final JsonParser parser = new JsonParser();
|
||||
|
||||
public String getServer(final UUID uuid) {
|
||||
|
||||
@@ -26,10 +26,12 @@
|
||||
*/
|
||||
package com.imaginarycode.minecraft.redisbungee;
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.Pipeline;
|
||||
|
||||
class RedisUtil {
|
||||
@VisibleForTesting
|
||||
public class RedisUtil {
|
||||
// Compatibility restraints prevent me from using using HDEL with multiple keys.
|
||||
public static void cleanUpPlayer(String player, Jedis rsc) {
|
||||
rsc.srem("proxy:" + RedisBungee.getApi().getServerId() + ":usersOnline", player);
|
||||
|
||||
@@ -26,39 +26,78 @@
|
||||
*/
|
||||
package com.imaginarycode.minecraft.redisbungee.util;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
// I would use the Guava, but can't because I need a few more properties.
|
||||
// I would use the Guava cache, but can't because I need a few more properties.
|
||||
public class InternalCache<K, V> {
|
||||
private final ConcurrentMap<K, V> map = new ConcurrentHashMap<>(128, 0.75f, 4);
|
||||
private final ConcurrentMap<K, Holder> map = new ConcurrentHashMap<>(128, 0.75f, 4);
|
||||
private final long entryWriteExpiry;
|
||||
|
||||
public InternalCache() {
|
||||
this.entryWriteExpiry = 0;
|
||||
}
|
||||
|
||||
public InternalCache(long entryWriteExpiry) {
|
||||
this.entryWriteExpiry = entryWriteExpiry;
|
||||
}
|
||||
|
||||
public V get(K key, Callable<V> loader) throws ExecutionException {
|
||||
V value = map.get(key);
|
||||
Holder value = map.get(key);
|
||||
|
||||
if (value == null || (entryWriteExpiry > 0 && System.currentTimeMillis() > value.expiry)) {
|
||||
V freshValue;
|
||||
|
||||
if (value == null) {
|
||||
try {
|
||||
value = loader.call();
|
||||
freshValue = loader.call();
|
||||
} catch (Exception e) {
|
||||
throw new ExecutionException(e);
|
||||
}
|
||||
|
||||
if (value == null)
|
||||
if (freshValue == null)
|
||||
return null;
|
||||
|
||||
map.putIfAbsent(key, value);
|
||||
map.putIfAbsent(key, value = new Holder(freshValue, System.currentTimeMillis() + entryWriteExpiry));
|
||||
}
|
||||
|
||||
return value;
|
||||
return value.value;
|
||||
}
|
||||
|
||||
public V put(K key, V value) {
|
||||
return map.put(key, value);
|
||||
Holder holder = map.put(key, new Holder(value, System.currentTimeMillis() + entryWriteExpiry));
|
||||
|
||||
if (holder == null)
|
||||
return null;
|
||||
|
||||
return holder.value;
|
||||
}
|
||||
|
||||
public void invalidate(K key) {
|
||||
map.remove(key);
|
||||
}
|
||||
|
||||
// Run periodically to clean up the cache mappings.
|
||||
public void cleanup() {
|
||||
if (entryWriteExpiry <= 0)
|
||||
return;
|
||||
|
||||
long fixedReference = System.currentTimeMillis();
|
||||
for (Iterator<Map.Entry<K, Holder>> it = map.entrySet().iterator(); it.hasNext(); ) {
|
||||
Map.Entry<K, Holder> entry = it.next();
|
||||
if (entry.getValue().expiry > fixedReference)
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
|
||||
@Data
|
||||
private class Holder {
|
||||
private final V value;
|
||||
private final long expiry;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user