Skip to content

Commit

Permalink
feature_Support_LRU_User_Info_Cache
Browse files Browse the repository at this point in the history
  • Loading branch information
cmgyqjj committed Sep 22, 2024
1 parent a727b1b commit 4ed3e18
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

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

import static com.crossoverjie.cim.route.constant.Constant.ACCOUNT_PREFIX;
Expand Down Expand Up @@ -115,22 +116,22 @@ public RouteHandle buildRouteHandle() throws Exception {
}

@Bean("userInfoCache")
public LoadingCache<Long, CIMUserInfo> userInfoCache(RedisTemplate<String, String> redisTemplate) {
public LoadingCache<Long, Optional<CIMUserInfo>> userInfoCache(RedisTemplate<String, String> redisTemplate) {
return CacheBuilder.newBuilder()
.initialCapacity(64)
.maximumSize(1024)
.concurrencyLevel(Runtime.getRuntime().availableProcessors())
.expireAfterWrite(10, TimeUnit.MINUTES)
.build(new CacheLoader<>() {
@Override
public CIMUserInfo load(Long userId) throws Exception {
public Optional<CIMUserInfo> load(Long userId) throws Exception {
CIMUserInfo cimUserInfo = null;
String sendUserName = redisTemplate.opsForValue().get(ACCOUNT_PREFIX + userId);
if (sendUserName == null) {
return null;
return Optional.empty();
}
cimUserInfo = new CIMUserInfo(userId, sendUserName);
return cimUserInfo;
return Optional.of(cimUserInfo);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ public class UserInfoCacheServiceImpl implements UserInfoCacheService {
private RedisTemplate<String,String> redisTemplate ;

@Resource(name = "userInfoCache")
private LoadingCache<Long, CIMUserInfo> userInfoMap;
private LoadingCache<Long, Optional<CIMUserInfo>> userInfoMap;

@Override
public Optional<CIMUserInfo> loadUserInfoByUserId(Long userId) {
//Retrieve user information using a second-level cache.
CIMUserInfo cimUserInfo = userInfoMap.getUnchecked(userId);
return Optional.ofNullable(cimUserInfo);
Optional<CIMUserInfo> cimUserInfo = userInfoMap.getUnchecked(userId);
return cimUserInfo;
}

@Override
Expand Down

0 comments on commit 4ed3e18

Please sign in to comment.