Skip to content

Commit

Permalink
ref[balancer]: use LazyCache instead of Caffeine
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Mar 26, 2024
1 parent f56bb65 commit a3c7dd0
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 13 deletions.
4 changes: 0 additions & 4 deletions boot/src/main/java/com/zfoo/boot/graalvm/GraalvmOrmHints.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

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;
Expand Down Expand Up @@ -42,9 +41,6 @@ public void registerHints(RuntimeHints hints, ClassLoader classLoader) {

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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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() {
}
Expand All @@ -50,7 +45,7 @@ public Session selectProvider(List<Session> providers, Object packet, Object arg
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());
Expand Down

0 comments on commit a3c7dd0

Please sign in to comment.