Skip to content

Commit fe7caa5

Browse files
committed
fix: RMapCache → RBucket per key로 전환
1 parent b32af75 commit fe7caa5

1 file changed

Lines changed: 26 additions & 15 deletions

File tree

src/main/java/sopt/makers/authentication/adapter/out/cache/UserCacheRepositoryImpl.java

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,57 @@
77
import sopt.makers.authentication.application.port.out.user.UserCacheRepository;
88
import sopt.makers.authentication.domain.user.User;
99

10-
import java.util.HashSet;
10+
import java.time.Duration;
11+
import java.util.HashMap;
1112
import java.util.List;
1213
import java.util.Map;
13-
import java.util.concurrent.TimeUnit;
14-
import java.util.stream.Collectors;
1514

16-
import org.redisson.api.RMapCache;
15+
import org.redisson.api.BatchResult;
16+
import org.redisson.api.RBatch;
1717
import org.redisson.api.RedissonClient;
18+
import org.redisson.client.codec.Codec;
1819
import org.redisson.codec.TypedJsonJacksonCodec;
1920
import org.springframework.stereotype.Component;
2021

2122
import com.fasterxml.jackson.databind.ObjectMapper;
2223

2324
@Component
2425
public class UserCacheRepositoryImpl implements UserCacheRepository {
25-
private static final long TTL_DAYS = 1;
26-
private final RMapCache<Long, CachedUserProfile> cache;
26+
private static final Duration TTL = Duration.ofDays(1);
27+
private static final String KEY_PREFIX = USER_CACHE_NAME + ":";
28+
29+
private final RedissonClient redissonClient;
2730
private final UserMapper userMapper;
31+
private final Codec codec;
2832

2933
public UserCacheRepositoryImpl(
3034
RedissonClient redissonClient, UserMapper userMapper, ObjectMapper objectMapper) {
31-
this.cache =
32-
redissonClient.getMapCache(
33-
USER_CACHE_NAME,
34-
new TypedJsonJacksonCodec(Long.class, CachedUserProfile.class, objectMapper));
35+
this.redissonClient = redissonClient;
3536
this.userMapper = userMapper;
37+
this.codec = new TypedJsonJacksonCodec(CachedUserProfile.class, objectMapper);
3638
}
3739

3840
@Override
3941
public Map<Long, User> getAllPresent(List<Long> userIds) {
4042
if (userIds.isEmpty()) return Map.of();
4143

42-
Map<Long, CachedUserProfile> cachedProfiles = cache.getAll(new HashSet<>(userIds));
43-
44-
return cachedProfiles.entrySet().stream()
45-
.collect(Collectors.toMap(Map.Entry::getKey, e -> userMapper.toDomain(e.getValue())));
44+
RBatch batch = redissonClient.createBatch();
45+
userIds.forEach(id -> batch.getBucket(KEY_PREFIX + id, codec).getAsync());
46+
BatchResult<?> result = batch.execute();
47+
48+
List<?> responses = result.getResponses();
49+
Map<Long, User> users = new HashMap<>();
50+
for (int i = 0; i < userIds.size(); i++) {
51+
CachedUserProfile profile = (CachedUserProfile) responses.get(i);
52+
if (profile != null) {
53+
users.put(userIds.get(i), userMapper.toDomain(profile));
54+
}
55+
}
56+
return users;
4657
}
4758

4859
@Override
4960
public void put(User user) {
50-
cache.put(user.getId(), userMapper.toCache(user), TTL_DAYS, TimeUnit.DAYS);
61+
redissonClient.getBucket(KEY_PREFIX + user.getId(), codec).set(userMapper.toCache(user), TTL);
5162
}
5263
}

0 commit comments

Comments
 (0)