diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 6be3397..c62a434 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -4,11 +4,10 @@ - @@ -595,7 +593,8 @@ - diff --git a/src/main/java/com/example/holidayplanner/helpers/CacheHelper.java b/src/main/java/com/example/holidayplanner/helpers/CacheHelper.java index 0736922..8506126 100644 --- a/src/main/java/com/example/holidayplanner/helpers/CacheHelper.java +++ b/src/main/java/com/example/holidayplanner/helpers/CacheHelper.java @@ -15,9 +15,9 @@ public class CacheHelper { private final String cacheName; - public CacheHelper(CacheManager cacheManager, Class type) { + public CacheHelper(CacheManager cacheManager, String cacheName) { this.cacheManager = cacheManager; - this.cacheName = type.getName().toLowerCase(); + this.cacheName = cacheName; } public List getCachedEntries(List cachedKey) { diff --git a/src/main/java/com/example/holidayplanner/user/UserService.java b/src/main/java/com/example/holidayplanner/user/UserService.java index e4492fc..3e15eb9 100644 --- a/src/main/java/com/example/holidayplanner/user/UserService.java +++ b/src/main/java/com/example/holidayplanner/user/UserService.java @@ -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; @@ -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; @@ -73,8 +75,10 @@ public class UserService { @Autowired private final ReportUserRepository reportUserRepository; + private final CacheHelper 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; @@ -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(); @@ -488,13 +493,23 @@ public ResponseEntity findMultipleById(List userIds) throws Json return ResponseEntity.badRequest().body("No ids provided"); } - List users = userRepository.findAllById(userIds); + List cachedUsers = userCacheHelper.getCachedEntries(userIds); + + List idsToFetch = userIds.stream().filter(id -> + cachedUsers.stream().noneMatch(user -> user.getId().equals(id))).toList(); + + List freshUsers = userRepository.findAllById(idsToFetch); - if (users.isEmpty()) { + userCacheHelper.cacheEntries(freshUsers, User::getId); + + List 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); }