mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-08-16 06:23:41 +00:00
ref[balancer]: use LazyCache instead of Caffeine
This commit is contained in:
@@ -14,7 +14,6 @@ package com.zfoo.boot.graalvm;
|
||||
|
||||
import com.zfoo.orm.anno.GraalvmNativeEntityCache;
|
||||
import com.zfoo.orm.config.OrmConfig;
|
||||
import com.zfoo.protocol.util.ClassUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.aot.hint.BindingReflectionHintsRegistrar;
|
||||
@@ -42,9 +41,6 @@ public class GraalvmOrmHints implements RuntimeHintsRegistrar {
|
||||
|
||||
var classes = new ArrayList<Class<?>>();
|
||||
classes.add(OrmConfig.class);
|
||||
// SSLMSA
|
||||
classes.add(ClassUtils.forName("com.github.benmanes.caffeine.cache.SSLMSA"));
|
||||
classes.add(ClassUtils.forName("com.github.benmanes.caffeine.cache.PSAMS"));
|
||||
|
||||
var filterClasses = HintUtils.filterAllClass(List.of(GraalvmNativeEntityCache.class), Collections.emptyList());
|
||||
classes.addAll(filterClasses);
|
||||
|
||||
+4
-9
@@ -1,14 +1,12 @@
|
||||
package com.zfoo.net.consumer.balancer;
|
||||
|
||||
import com.github.benmanes.caffeine.cache.Cache;
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
import com.zfoo.net.NetContext;
|
||||
import com.zfoo.net.session.Session;
|
||||
import com.zfoo.protocol.ProtocolManager;
|
||||
import com.zfoo.scheduler.util.LazyCache;
|
||||
import com.zfoo.scheduler.util.TimeUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 记忆化一致性hash
|
||||
@@ -19,15 +17,12 @@ public class CachedConsistentHashLoadBalancer extends AbstractConsumerLoadBalanc
|
||||
|
||||
public static final CachedConsistentHashLoadBalancer INSTANCE = new CachedConsistentHashLoadBalancer();
|
||||
private static final long EXPIRED_ACCESS_DURATION = 10 * TimeUtils.MILLIS_PER_MINUTE;
|
||||
private static final long MAX_CACHE_SIZE = 10_0000;
|
||||
private static final int MAX_CACHE_SIZE = 10_0000;
|
||||
|
||||
/**
|
||||
* cache the sid after load balancer,key:[argument + protocolModuleId] -> value:[sid]
|
||||
*/
|
||||
private Cache<Long, Long> cache = Caffeine.newBuilder()
|
||||
.expireAfterAccess(EXPIRED_ACCESS_DURATION, TimeUnit.MILLISECONDS)
|
||||
.maximumSize(MAX_CACHE_SIZE)
|
||||
.build();
|
||||
private LazyCache<Long, Long> cache = new LazyCache<>(MAX_CACHE_SIZE, EXPIRED_ACCESS_DURATION, 5 * TimeUtils.MILLIS_PER_MINUTE, null);
|
||||
|
||||
private CachedConsistentHashLoadBalancer() {
|
||||
}
|
||||
@@ -50,7 +45,7 @@ public class CachedConsistentHashLoadBalancer extends AbstractConsumerLoadBalanc
|
||||
var protocolModuleId = (long) ProtocolManager.moduleByProtocol(packet.getClass()).getId();
|
||||
// 8 Byte cachedKey = 7 byte of argument + 1 byte of protocolModuleId
|
||||
var cachedKey = arg.longValue() << 8 | protocolModuleId;
|
||||
var sid = cache.getIfPresent(cachedKey);
|
||||
var sid = cache.get(cachedKey);
|
||||
if (sid == null) {
|
||||
var providerSession = ConsistentHashLoadBalancer.getInstance().selectProvider(providers, packet, argument);
|
||||
cache.put(cachedKey, providerSession.getSid());
|
||||
|
||||
Reference in New Issue
Block a user