|
7 | 7 | import sopt.makers.authentication.application.port.out.user.UserCacheRepository; |
8 | 8 | import sopt.makers.authentication.domain.user.User; |
9 | 9 |
|
10 | | -import java.util.HashSet; |
| 10 | +import java.time.Duration; |
| 11 | +import java.util.HashMap; |
11 | 12 | import java.util.List; |
12 | 13 | import java.util.Map; |
13 | | -import java.util.concurrent.TimeUnit; |
14 | | -import java.util.stream.Collectors; |
15 | 14 |
|
16 | | -import org.redisson.api.RMapCache; |
| 15 | +import org.redisson.api.BatchResult; |
| 16 | +import org.redisson.api.RBatch; |
17 | 17 | import org.redisson.api.RedissonClient; |
| 18 | +import org.redisson.client.codec.Codec; |
18 | 19 | import org.redisson.codec.TypedJsonJacksonCodec; |
19 | 20 | import org.springframework.stereotype.Component; |
20 | 21 |
|
21 | 22 | import com.fasterxml.jackson.databind.ObjectMapper; |
22 | 23 |
|
23 | 24 | @Component |
24 | 25 | 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; |
27 | 30 | private final UserMapper userMapper; |
| 31 | + private final Codec codec; |
28 | 32 |
|
29 | 33 | public UserCacheRepositoryImpl( |
30 | 34 | 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; |
35 | 36 | this.userMapper = userMapper; |
| 37 | + this.codec = new TypedJsonJacksonCodec(CachedUserProfile.class, objectMapper); |
36 | 38 | } |
37 | 39 |
|
38 | 40 | @Override |
39 | 41 | public Map<Long, User> getAllPresent(List<Long> userIds) { |
40 | 42 | if (userIds.isEmpty()) return Map.of(); |
41 | 43 |
|
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; |
46 | 57 | } |
47 | 58 |
|
48 | 59 | @Override |
49 | 60 | 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); |
51 | 62 | } |
52 | 63 | } |
0 commit comments