Skip to content

Commit

Permalink
Caching for find multiple holiday invites endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
fizzy-fifs committed Oct 26, 2024
1 parent 3609360 commit f8f1899
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
5 changes: 2 additions & 3 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 @@ -28,7 +28,7 @@ public HolidayInviteController(HolidayInviteService holidayInviteService) {

@PostMapping("/findmultiplebyid")
@Operation(summary = "Find multiple holiday invites by their ids")
public ResponseEntity findMultipleById(@RequestBody List<String> holidayInviteIds) throws JsonProcessingException {
public ResponseEntity<String> findMultipleById(@RequestBody List<String> holidayInviteIds) throws JsonProcessingException {
return holidayInviteService.findMultipleById(holidayInviteIds);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.example.holidayplanner.holidayInvite;

import com.example.holidayplanner.helpers.CacheHelper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
Expand All @@ -14,27 +17,48 @@ public class HolidayInviteService {
@Autowired
private final HolidayInviteRepository holidayInviteRepository;

private final CacheHelper<HolidayInvite> holidayInviteCacheHelper;

@Autowired
private final ObjectMapper objectMapper;

public HolidayInviteService(HolidayInviteRepository holidayInviteRepository, ObjectMapper objectMapper) {
public HolidayInviteService(HolidayInviteRepository holidayInviteRepository, ObjectMapper objectMapper, CacheManager cacheManager) {
this.holidayInviteRepository = holidayInviteRepository;
this.objectMapper = objectMapper;
holidayInviteCacheHelper = new CacheHelper<>(cacheManager, "holiday invites", HolidayInvite.class);
}

public ResponseEntity findMultipleById(List<String> holidayInviteIds) throws JsonProcessingException {
public ResponseEntity<String> findMultipleById(List<String> holidayInviteIds) throws JsonProcessingException {
if (holidayInviteIds == null || holidayInviteIds.isEmpty()) {
return ResponseEntity.badRequest().body("No holiday invite ids provided");
}

Iterable<HolidayInvite> holidayInvite = holidayInviteRepository.findAllById(holidayInviteIds);
List<HolidayInvite> holidayInvite = findMultipleGroupInvitesByIdInCacheOrDatabase(holidayInviteIds);

if (holidayInvite == null) {
if (holidayInvite.isEmpty()) {
return ResponseEntity.badRequest().body("No holiday invites found");
}

String holidayInviteJson = objectMapper.writeValueAsString(holidayInvite);

return ResponseEntity.ok().body(holidayInviteJson);
}

public List<HolidayInvite> findMultipleGroupInvitesByIdInCacheOrDatabase(List<String> holidayInviteIds) {
List<HolidayInvite> cachedHolidayInvites = holidayInviteCacheHelper.getCachedEntries(holidayInviteIds);

List<String> idsToFetch = holidayInviteIds.stream().filter(id -> cachedHolidayInvites.stream().noneMatch(holidayInvite -> holidayInvite.getId().equals(id))).toList();

if (idsToFetch.isEmpty()) {
return cachedHolidayInvites;
}

List<HolidayInvite> freshHolidayInvites = holidayInviteRepository.findAllById(idsToFetch);

holidayInviteCacheHelper.cacheEntries(freshHolidayInvites, HolidayInvite::getId);
List<HolidayInvite> allHolidayInvites = new ArrayList<>(cachedHolidayInvites);
allHolidayInvites.addAll(freshHolidayInvites);

return allHolidayInvites;
}
}

0 comments on commit f8f1899

Please sign in to comment.