Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature:Support_LRU_User_Info_Cache #156

Merged
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
5d77aac
feature:Support_LRU_User_Info_Cache
cmgyqjj Sep 21, 2024
9198e92
feature:Support_LRU_User_Info_Cache
cmgyqjj Sep 21, 2024
ef869cb
feature:Support_LRU_User_Info_Cache
cmgyqjj Sep 21, 2024
fa415d1
Merge branch 'master' into feature_Support_LRU_User_Info_Cache
cmgyqjj Sep 21, 2024
0823d51
feature_Support_LRU_User_Info_Cache
cmgyqjj Sep 21, 2024
6aed87f
Merge branch 'master' into feature_Support_LRU_User_Info_Cache
cmgyqjj Sep 22, 2024
8eb336d
feature_Support_LRU_User_Info_Cache
cmgyqjj Sep 22, 2024
36a3a37
Merge branch 'master' into feature_Support_LRU_User_Info_Cache
crossoverJie Sep 22, 2024
36d414b
Merge branch 'master' into feature_Support_LRU_User_Info_Cache
crossoverJie Sep 22, 2024
54b9370
Merge branch 'master' into feature_Support_LRU_User_Info_Cache
crossoverJie Sep 22, 2024
a760273
Merge branch 'master' into feature_Support_LRU_User_Info_Cache
crossoverJie Sep 22, 2024
e4e9135
Update cim-forward-route/src/main/java/com/crossoverjie/cim/route/ser…
cmgyqjj Sep 22, 2024
03712e5
Update cim-forward-route/src/main/java/com/crossoverjie/cim/route/con…
cmgyqjj Sep 22, 2024
6b1a1fb
Update cim-forward-route/src/main/java/com/crossoverjie/cim/route/con…
cmgyqjj Sep 22, 2024
65d0c2d
Update cim-forward-route/src/main/java/com/crossoverjie/cim/route/con…
cmgyqjj Sep 22, 2024
3961542
feature_Support_LRU_User_Info_Cache
cmgyqjj Sep 22, 2024
6e08eb7
Update cim-forward-route/src/main/java/com/crossoverjie/cim/route/con…
cmgyqjj Sep 22, 2024
a727b1b
feature_Support_LRU_User_Info_Cache
cmgyqjj Sep 22, 2024
4ed3e18
feature_Support_LRU_User_Info_Cache
cmgyqjj Sep 22, 2024
72d7be1
Merge branch 'master' into feature_Support_LRU_User_Info_Cache
crossoverJie Sep 22, 2024
5070761
Merge remote-tracking branch 'cr/feature_Support_LRU_User_Info_Cache'…
crossoverJie Sep 22, 2024
106a42e
Update cim-forward-route/src/main/java/com/crossoverjie/cim/route/con…
cmgyqjj Sep 22, 2024
dbde9e8
Update cim-forward-route/src/main/java/com/crossoverjie/cim/route/con…
cmgyqjj Sep 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import com.crossoverjie.cim.common.metastore.MetaStore;
import com.crossoverjie.cim.common.metastore.ZkConfiguration;
import com.crossoverjie.cim.common.metastore.ZkMetaStoreImpl;
import com.crossoverjie.cim.common.pojo.CIMUserInfo;
import com.crossoverjie.cim.common.route.algorithm.RouteHandle;
import com.crossoverjie.cim.common.route.algorithm.consistenthash.AbstractConsistentHash;
import com.crossoverjie.cim.server.api.ServerApi;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.cache.Weigher;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
Expand All @@ -24,8 +26,11 @@
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.lang.reflect.Method;
import java.time.Duration;
import java.util.concurrent.TimeUnit;

import static com.crossoverjie.cim.route.constant.Constant.ACCOUNT_PREFIX;

/**
* Function:
*
Expand Down Expand Up @@ -109,6 +114,27 @@ public RouteHandle buildRouteHandle() throws Exception {

}

@Bean
public LoadingCache<Long, CIMUserInfo> USER_INFO_MAP(RedisTemplate<String, String> redisTemplate) {
return CacheBuilder.newBuilder()
.initialCapacity(64)
.maximumSize(1024)
.concurrencyLevel(Runtime.getRuntime().availableProcessors())
.expireAfterWrite(10, TimeUnit.MINUTES)
.build(new CacheLoader<Long, CIMUserInfo>() {
@Override
public CIMUserInfo load(Long userId) throws Exception {
CIMUserInfo cimUserInfo = null;
String sendUserName = redisTemplate.opsForValue().get(ACCOUNT_PREFIX + userId);
if (sendUserName == null) {
sendUserName = "unLoginUser";
}
cimUserInfo = new CIMUserInfo(userId, sendUserName);
return cimUserInfo;
}
});
}

@Bean
public ServerApi serverApi(OkHttpClient okHttpClient) {
return RpcProxyManager.create(ServerApi.class, okHttpClient);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import com.crossoverjie.cim.common.pojo.CIMUserInfo;
import com.crossoverjie.cim.route.service.UserInfoCacheService;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
Expand All @@ -11,6 +14,8 @@
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import static com.crossoverjie.cim.route.constant.Constant.ACCOUNT_PREFIX;
import static com.crossoverjie.cim.route.constant.Constant.LOGIN_STATUS_PREFIX;
Expand All @@ -26,30 +31,16 @@
@Service
public class UserInfoCacheServiceImpl implements UserInfoCacheService {

/**
* todo 本地缓存,为了防止内存撑爆,后期可换为 LRU。
*/
private final static Map<Long,CIMUserInfo> USER_INFO_MAP = new ConcurrentHashMap<>(64) ;

@Autowired
private RedisTemplate<String,String> redisTemplate ;

@Autowired
private LoadingCache<Long, CIMUserInfo> USER_INFO_MAP;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use camel case naming for variable name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, USER_INFO_MAP has been replaced by userInfoMap.


@Override
public Optional<CIMUserInfo> loadUserInfoByUserId(Long userId) {

//优先从本地缓存获取
CIMUserInfo cimUserInfo = USER_INFO_MAP.get(userId);
if (cimUserInfo != null){
return Optional.of(cimUserInfo);
}

//load redis
String sendUserName = redisTemplate.opsForValue().get(ACCOUNT_PREFIX + userId);
if (sendUserName != null){
cimUserInfo = new CIMUserInfo(userId,sendUserName) ;
USER_INFO_MAP.put(userId,cimUserInfo) ;
}

//Retrieve user information using a second-level cache.
CIMUserInfo cimUserInfo = USER_INFO_MAP.getUnchecked(userId);
return Optional.ofNullable(cimUserInfo);
}

Expand Down
Loading