Skip to content

Commit f4fe7ca

Browse files
authored
[DEPLOY] v1.2.6
[DEPLOY] v1.2.6
2 parents 015fb0e + 0f0361e commit f4fe7ca

3 files changed

Lines changed: 78 additions & 30 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
}

src/main/java/sopt/makers/authentication/adapter/out/persistence/repository/user/UserJpaRepository.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,36 @@ Optional<UserEntity> findByAuthPlatformTypeAndAuthPlatformId(
3737
Optional<UserEntity> findWithActivityHistoriesById(@Param("userId") Long userId);
3838

3939
@Query(
40-
"SELECT DISTINCT u "
40+
"SELECT u.id "
4141
+ "FROM UserEntity u "
42-
+ "JOIN FETCH u.userActivityHistoryList "
42+
+ "JOIN u.userActivityHistoryList a "
4343
+ "WHERE (:name IS NULL OR u.name LIKE %:name%) "
44-
+ "AND EXISTS ("
45-
+ "SELECT 1 FROM UserActivityHistoryEntity a "
46-
+ "WHERE a.user = u "
4744
+ "AND (:generation IS NULL OR a.generation = :generation) "
4845
+ "AND (:part IS NULL OR a.part = :part) "
4946
+ "AND (:team IS NULL OR a.team = :team) "
50-
+ "AND (:isAdmin IS NULL OR :isAdmin = FALSE OR a.role <> 'MEMBER'))")
51-
Page<UserEntity> findAllWithActivityHistoriesByGenerationAndPartAndNameAndTeam(
47+
+ "AND (:isAdmin IS NULL OR :isAdmin = FALSE OR a.role <> 'MEMBER') "
48+
+ "GROUP BY u.id "
49+
+ "ORDER BY u.id DESC")
50+
Page<Long> findUserIdsOrderByRegisteredDesc(
51+
@Param("generation") Integer generation,
52+
@Param("part") Part part,
53+
@Param("name") String name,
54+
@Param("team") Team team,
55+
@Param("isAdmin") Boolean isAdmin,
56+
Pageable pageable);
57+
58+
@Query(
59+
"SELECT u.id "
60+
+ "FROM UserEntity u "
61+
+ "JOIN u.userActivityHistoryList a "
62+
+ "WHERE (:name IS NULL OR u.name LIKE %:name%) "
63+
+ "AND (:generation IS NULL OR a.generation = :generation) "
64+
+ "AND (:part IS NULL OR a.part = :part) "
65+
+ "AND (:team IS NULL OR a.team = :team) "
66+
+ "AND (:isAdmin IS NULL OR :isAdmin = FALSE OR a.role <> 'MEMBER') "
67+
+ "GROUP BY u.id "
68+
+ "ORDER BY u.id ASC")
69+
Page<Long> findUserIdsOrderByRegisteredAsc(
5270
@Param("generation") Integer generation,
5371
@Param("part") Part part,
5472
@Param("name") String name,

src/main/java/sopt/makers/authentication/adapter/out/persistence/repository/user/UserRetriever.java

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,10 @@ public Page<User> findAllByGenerationAndPartAndNameAndTeam(
7373
if (orderBy.isGenerationOrder()) {
7474
return findByGenerationOrder(generation, part, name, team, isAdmin, pageable, orderBy);
7575
}
76-
return userJpaRepository
77-
.findAllWithActivityHistoriesByGenerationAndPartAndNameAndTeam(
78-
generation, part, name, team, isAdmin, pageable)
79-
.map(UserEntity::toDomain);
76+
return findByRegisteredOrder(generation, part, name, team, isAdmin, pageable, orderBy);
8077
}
8178

82-
private Page<User> findByGenerationOrder(
79+
private Page<User> findByRegisteredOrder(
8380
Integer generation,
8481
Part part,
8582
String name,
@@ -88,12 +85,16 @@ private Page<User> findByGenerationOrder(
8885
Pageable pageable,
8986
UserOrderBy orderBy) {
9087
Page<Long> userIdPage =
91-
orderBy == UserOrderBy.LATEST_GENERATION
92-
? userJpaRepository.findUserIdsOrderByMatchedGenerationDesc(
88+
orderBy == UserOrderBy.LATEST_REGISTERED
89+
? userJpaRepository.findUserIdsOrderByRegisteredDesc(
9390
generation, part, name, team, isAdmin, pageable)
94-
: userJpaRepository.findUserIdsOrderByMatchedGenerationAsc(
91+
: userJpaRepository.findUserIdsOrderByRegisteredAsc(
9592
generation, part, name, team, isAdmin, pageable);
9693

94+
return getUsers(pageable, userIdPage);
95+
}
96+
97+
private Page<User> getUsers(Pageable pageable, Page<Long> userIdPage) {
9798
List<Long> orderedIds = userIdPage.getContent();
9899
if (orderedIds.isEmpty()) {
99100
return new PageImpl<>(List.of(), pageable, userIdPage.getTotalElements());
@@ -114,6 +115,24 @@ private Page<User> findByGenerationOrder(
114115
return new PageImpl<>(orderedUsers, pageable, userIdPage.getTotalElements());
115116
}
116117

118+
private Page<User> findByGenerationOrder(
119+
Integer generation,
120+
Part part,
121+
String name,
122+
Team team,
123+
Boolean isAdmin,
124+
Pageable pageable,
125+
UserOrderBy orderBy) {
126+
Page<Long> userIdPage =
127+
orderBy == UserOrderBy.LATEST_GENERATION
128+
? userJpaRepository.findUserIdsOrderByMatchedGenerationDesc(
129+
generation, part, name, team, isAdmin, pageable)
130+
: userJpaRepository.findUserIdsOrderByMatchedGenerationAsc(
131+
generation, part, name, team, isAdmin, pageable);
132+
133+
return getUsers(pageable, userIdPage);
134+
}
135+
117136
public int countByGenerationAndIsSopt(int generation, boolean isSopt) {
118137
return userJpaRepository.countByGenerationAndIsSopt(generation, isSopt);
119138
}

0 commit comments

Comments
 (0)