Skip to content

Commit

Permalink
Merge remote-tracking branch 'cr/feature_Support_LRU_User_Info_Cache'…
Browse files Browse the repository at this point in the history
… into feature_Support_LRU_User_Info_Cache
  • Loading branch information
crossoverJie committed Sep 22, 2024
2 parents 72d7be1 + 4ed3e18 commit 5070761
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 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 @@ -114,23 +115,23 @@ public RouteHandle buildRouteHandle() throws Exception {

}

@Bean
public LoadingCache<Long, CIMUserInfo> USER_INFO_MAP(RedisTemplate<String, String> redisTemplate) {
@Bean("userInfoCache")
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<Long, CIMUserInfo>() {
.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) {
sendUserName = "unLoginUser";
return Optional.empty();
}
cimUserInfo = new CIMUserInfo(userId, sendUserName);
return cimUserInfo;
return Optional.of(cimUserInfo);

Check warning on line 134 in cim-forward-route/src/main/java/com/crossoverjie/cim/route/config/BeanConfig.java

View check run for this annotation

Codecov / codecov/patch

cim-forward-route/src/main/java/com/crossoverjie/cim/route/config/BeanConfig.java#L133-L134

Added lines #L133 - L134 were not covered by tests
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -34,14 +35,14 @@ public class UserInfoCacheServiceImpl implements UserInfoCacheService {
@Autowired
private RedisTemplate<String,String> redisTemplate ;

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

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

@Override
Expand Down

0 comments on commit 5070761

Please sign in to comment.