Skip to content

Commit

Permalink
Implement caching for find multiple users by id endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
fizzy-fifs committed Oct 24, 2024
1 parent 20a625f commit e012f31
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
27 changes: 13 additions & 14 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public class CacheHelper<T> {

private final String cacheName;

public CacheHelper(CacheManager cacheManager, Class<T> type) {
public CacheHelper(CacheManager cacheManager, String cacheName) {
this.cacheManager = cacheManager;
this.cacheName = type.getName().toLowerCase();
this.cacheName = cacheName;
}

public List<T> getCachedEntries(List<String> cachedKey) {
Expand Down
23 changes: 19 additions & 4 deletions src/main/java/com/example/holidayplanner/user/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.example.holidayplanner.config.jwt.JwtUtil;
import com.example.holidayplanner.config.jwt.token.Token;
import com.example.holidayplanner.config.jwt.token.TokenService;
import com.example.holidayplanner.helpers.CacheHelper;
import com.example.holidayplanner.helpers.Helper;
import com.example.holidayplanner.user.reportUser.ReportUser;
import com.example.holidayplanner.user.reportUser.ReportUserRepository;
Expand All @@ -16,6 +17,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.MongoTemplate;
Expand Down Expand Up @@ -73,8 +75,10 @@ public class UserService {
@Autowired
private final ReportUserRepository reportUserRepository;

private final CacheHelper<User> userCacheHelper;

@Autowired
public UserService(UserRepository userRepository, RoleRepository roleRepository, MyUserDetailsService myUserDetailsService, JwtUtil jwtTokenUtil, TokenService tokenService, AuthenticationManager authenticationManager, MongoTemplate mongoTemplate, UserDeactivationRequestRepository userDeactivationRequestRepository, ReportUserRepository reportUserRepository) {
public UserService(UserRepository userRepository, RoleRepository roleRepository, MyUserDetailsService myUserDetailsService, JwtUtil jwtTokenUtil, TokenService tokenService, AuthenticationManager authenticationManager, MongoTemplate mongoTemplate, UserDeactivationRequestRepository userDeactivationRequestRepository, ReportUserRepository reportUserRepository, CacheManager cacheManager) {
this.userRepository = userRepository;
this.roleRepository = roleRepository;
this.myUserDetailsService = myUserDetailsService;
Expand All @@ -84,6 +88,7 @@ public UserService(UserRepository userRepository, RoleRepository roleRepository,
this.mongoTemplate = mongoTemplate;
this.userDeactivationRequestRepository = userDeactivationRequestRepository;
this.reportUserRepository = reportUserRepository;
this.userCacheHelper = new CacheHelper<>(cacheManager, "user");
this.mapper = new ObjectMapper().findAndRegisterModules();
this.mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
this.passwordEncoder = new BCryptPasswordEncoder();
Expand Down Expand Up @@ -488,13 +493,23 @@ public ResponseEntity<String> findMultipleById(List<String> userIds) throws Json
return ResponseEntity.badRequest().body("No ids provided");
}

List<User> users = userRepository.findAllById(userIds);
List<User> cachedUsers = userCacheHelper.getCachedEntries(userIds);

List<String> idsToFetch = userIds.stream().filter(id ->
cachedUsers.stream().noneMatch(user -> user.getId().equals(id))).toList();

List<User> freshUsers = userRepository.findAllById(idsToFetch);

if (users.isEmpty()) {
userCacheHelper.cacheEntries(freshUsers, User::getId);

List<User> allUsers = new ArrayList<>(cachedUsers);
allUsers.addAll(freshUsers);

if (allUsers.isEmpty()) {
return ResponseEntity.badRequest().body("No users found");
}

String usersJson = mapper.writeValueAsString(users);
String usersJson = mapper.writeValueAsString(allUsers);

return ResponseEntity.ok(usersJson);
}
Expand Down

0 comments on commit e012f31

Please sign in to comment.