Skip to content

Commit

Permalink
Caching :)
Browse files Browse the repository at this point in the history
  • Loading branch information
Firestone82 committed Apr 29, 2024
1 parent 70b42c8 commit b35fce3
Show file tree
Hide file tree
Showing 15 changed files with 70 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import cz.trailsthroughshadows.api.configuration.ImageLoaderConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
Expand All @@ -28,6 +29,7 @@ public class ImageController {


@GetMapping("/{type}/{file}")
@Cacheable(value = "image", key = "T(java.util.Objects).hash(#type, #file, #width, #height, #size, #radius, #token)")
public @ResponseBody byte[] getImage(
@PathVariable String type,
@PathVariable String file,
Expand Down Expand Up @@ -79,6 +81,7 @@ public class ImageController {
}

@GetMapping(value = "/svg/{type}/{file:.+\\.svg}")
@Cacheable(value = "svg", key = "T(java.util.Objects).hash(#type, #file)")
public ResponseEntity<Resource> getSwg(
@PathVariable String type,
@PathVariable String file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -48,15 +50,18 @@ public class ActionController {
private ValidationService validation;
private ActionRepo actionRepo;
private EffectRepo effectRepo;

@Autowired
private EnemyRepo enemyRepo;

@Autowired
private ClazzRepo clazzRepo;

@Autowired
private RaceRepo raceRepo;

@GetMapping("/actions")
//@Cacheable(value = "action")
@Cacheable(value = "action", key="T(java.util.Objects).hash(#page, #limit, #filter, #sort, #include, #lazy)")
public ResponseEntity<RestPaginatedResult<Action>> findAllEntities(
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "100") int limit,
Expand Down Expand Up @@ -115,7 +120,7 @@ public ResponseEntity<RestPaginatedResult<Action>> findAllEntities(
content = @Content)
})
@GetMapping("/actions/{id}")
//@Cacheable(value = "action", key = "#id")
@Cacheable(value = "action", key="T(java.util.Objects).hash(#id, #include, #lazy)")
public ResponseEntity<Action> findById(
@Parameter(description = "Id of the action to be obtained.\n\n Cannot be empty.", required = true)
@PathVariable int id,
Expand Down Expand Up @@ -270,6 +275,7 @@ public ActionDTO createInnner(ActionDTO action) {
}

@PutMapping("/actions/{id}")
@CacheEvict(value = "action", allEntries = true)
public ResponseEntity<MessageResponse> update(@PathVariable int id, @RequestBody ActionDTO action) {
validation.validate(action);

Expand All @@ -279,17 +285,18 @@ public ResponseEntity<MessageResponse> update(@PathVariable int id, @RequestBody
}

@DeleteMapping("/actions/{id}")
@CacheEvict(value = "action", allEntries = true)
public ResponseEntity<MessageResponse> delete(@PathVariable int id) {
ActionDTO entity = actionRepo
.findById(id)
.orElseThrow(() -> RestException.of(HttpStatus.NOT_FOUND, "Action with id '{}' not found!", id));

actionRepo.delete(entity);
return new ResponseEntity<>(MessageResponse.of(HttpStatus.OK, "Action with id '{}' deleted!", id),
HttpStatus.OK);
return new ResponseEntity<>(MessageResponse.of(HttpStatus.OK, "Action with id '{}' deleted!", id), HttpStatus.OK);
}

@PostMapping("/actions")
@CacheEvict(value = "action", allEntries = true)
public ResponseEntity<MessageResponse> createList(@RequestBody List<ActionDTO> actions) {
List<String> ids = new ArrayList<>();

Expand All @@ -305,6 +312,7 @@ public ResponseEntity<MessageResponse> createList(@RequestBody List<ActionDTO> a
}

@GetMapping("/actions/{id}/card")
@Cacheable(value = "actionCard", key="T(java.util.Objects).hash(#id)")
public ResponseEntity<LinkedHashMap<String, Object>> getCard(@PathVariable int id) {
ActionDTO entitydto = actionRepo
.findById(id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class SummonController {
private SummonRepo summonRepo;

@GetMapping("/")
@Cacheable(value = "summon")
@Cacheable(value = "summon" ,key="T(java.util.Objects).hash(#page, #limit, #filter, #sort, #include, #lazy)")
public ResponseEntity<RestPaginatedResult<SummonDTO>> findAllEntities(
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "100") int limit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class ClazzController {
private ActionRepo actionRepo;

@GetMapping("/classes")
@Cacheable(value = "class")
@Cacheable(value = "class", key="T(java.util.Objects).hash(#page, #limit, #filter, #sort, #include, #lazy)")
public ResponseEntity<RestPaginatedResult<Clazz>> getEnemies(
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "100") int limit,
Expand Down Expand Up @@ -72,7 +72,7 @@ public ResponseEntity<RestPaginatedResult<Clazz>> getEnemies(
}

@GetMapping("/classes/{id}")
@Cacheable(value = "class", key = "#id")
@Cacheable(value = "class", key="T(java.util.Objects).hash(#id, #include, #lazy)")
public ResponseEntity<Clazz> findById(
@PathVariable int id,
@RequestParam(required = false, defaultValue = "") List<String> include,
Expand All @@ -92,7 +92,7 @@ public ResponseEntity<Clazz> findById(
}

@PutMapping("classes/{id}")
@CacheEvict(value = "class", key = "#id")
@CacheEvict(value = "class", allEntries = true)
public ResponseEntity<MessageResponse> updateEntity(
@PathVariable int id,
@RequestBody ClazzDTO entity
Expand Down Expand Up @@ -222,7 +222,7 @@ private EffectDTO processEffects(EffectDTO inputEffect) {
}

@DeleteMapping("/classes/{id}")
@CacheEvict(value = "class", key = "#id")
@CacheEvict(value = "class", allEntries = true)
public ResponseEntity<MessageResponse> deleteEntity(
@PathVariable int id
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class RaceController {
private ActionRepo actionRepo;

@GetMapping("/races")
@Cacheable(value = "race")
@Cacheable(value = "race", key="T(java.util.Objects).hash(#page, #limit, #filter, #sort, #include, #lazy)")
public ResponseEntity<RestPaginatedResult<Race>> findAllEntities(
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "100") int limit,
Expand Down Expand Up @@ -72,7 +72,7 @@ public ResponseEntity<RestPaginatedResult<Race>> findAllEntities(
}

@GetMapping("/races/{id}")
@Cacheable(value = "race", key = "#id")
@Cacheable(value = "race", key="T(java.util.Objects).hash(#id, #include, #lazy)")
public ResponseEntity<Race> findById(
@PathVariable int id,
@RequestParam(required = false, defaultValue = "") List<String> include,
Expand All @@ -92,7 +92,7 @@ public ResponseEntity<Race> findById(
}

@PutMapping("races/{id}")
@CacheEvict(value = "race", key = "#id")
@CacheEvict(value = "race", allEntries = true)
public ResponseEntity<MessageResponse> updateRaceById(
@PathVariable int id,
@RequestBody RaceDTO entity
Expand Down Expand Up @@ -219,7 +219,7 @@ private EffectDTO processEffects(EffectDTO inputEffect) {
}

@DeleteMapping("/races/{id}")
@CacheEvict(value = "race", key = "#id")
@CacheEvict(value = "race", allEntries = true)
public ResponseEntity<MessageResponse> deleteEntity(@PathVariable int id) {
RaceDTO entity = raceRepo
.findById(id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class CampaignController {
private ValidationService validation;

@GetMapping("")
@Cacheable(value = "campaign")
@Cacheable(value = "campaign", key="T(java.util.Objects).hash(#page, #limit, #filter, #sort, #include, #lazy)")
public ResponseEntity<RestPaginatedResult<CampaignDTO>> findAllEntities(
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "100") int limit,
Expand Down Expand Up @@ -77,7 +77,7 @@ public ResponseEntity<RestPaginatedResult<CampaignDTO>> findAllEntities(
}

@GetMapping("/{id}")
@Cacheable(value = "campaign")
@Cacheable(value = "campaign", key="T(java.util.Objects).hash(#id, #include, #lazy)")
public CampaignDTO findById(
@PathVariable int id,
@RequestParam(required = false, defaultValue = "") List<String> include,
Expand All @@ -96,8 +96,8 @@ public CampaignDTO findById(

}

@Cacheable(value = "campaign")
@GetMapping("/{id}/location/{idLocation}")
@Cacheable(value = "campaign", key="T(java.util.Objects).hash(#id, #idLocation, #include, #lazy)")
public CampaignLocation findById2(
@PathVariable int id,
@PathVariable int idLocation,
Expand All @@ -121,8 +121,8 @@ public CampaignLocation findById2(
return location;
}

@Transactional
@PutMapping("/{id}")
@Transactional(rollbackOn = Exception.class)
@CacheEvict(value = "campaign", allEntries = true)
public ResponseEntity<MessageResponse> update(@PathVariable int id, @RequestBody CampaignDTO campaign) {
log.debug("Updating campaign with id '{}': {}", id, campaign);
Expand Down Expand Up @@ -190,8 +190,8 @@ public ResponseEntity<MessageResponse> update(@PathVariable int id, @RequestBody
return new ResponseEntity<>(MessageResponse.of(HttpStatus.OK, "Campaign with id '{}' updated!", id), HttpStatus.OK);
}

@Transactional
@DeleteMapping("/{id}")
@Transactional(rollbackOn = Exception.class)
@CacheEvict(value = "campaign", allEntries = true)
public ResponseEntity<MessageResponse> delete(@PathVariable int id) {
CampaignDTO campaign = campaignRepo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class EffectController {
private EffectRepo effectRepo;

@GetMapping("/effects")
@Cacheable(value = "effect")
@Cacheable(value = "effect", key="T(java.util.Objects).hash(#page, #limit, #filter, #sort, #include, #lazy)")
public ResponseEntity<RestPaginatedResult<Effect>> getEnemies(
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "100") int limit,
Expand Down Expand Up @@ -76,7 +76,7 @@ public ResponseEntity<List<EffectDTO.EffectTarget>> getEffectTarget() {
}

@GetMapping("/effects/{id}")
// @Cacheable(value = "effect", key = "#id")
@Cacheable(value = "effect", key="T(java.util.Objects).hash(#id, #include, #lazy)")
public ResponseEntity<Effect> findById(
@PathVariable int id,
@RequestParam(required = false, defaultValue = "") List<String> include,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class EnemyController {
private ActionRepo actionRepo;

@GetMapping("/enemies")
@Cacheable(value = "enemy")
@Cacheable(value = "enemy", key="T(java.util.Objects).hash(#page, #limit, #filter, #sort, #include, #lazy)")
public ResponseEntity<RestPaginatedResult<Enemy>> getEnemies(
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "100") int limit,
Expand Down Expand Up @@ -75,7 +75,7 @@ public ResponseEntity<RestPaginatedResult<Enemy>> getEnemies(
}

@GetMapping("/enemies/{id}")
@Cacheable(value = "enemy", key = "#id")
@Cacheable(value = "enemy", key="T(java.util.Objects).hash(#id, #include, #lazy)")
public ResponseEntity<Enemy> findById(
@PathVariable int id,
@RequestParam(required = false, defaultValue = "") List<String> include,
Expand All @@ -95,7 +95,7 @@ public ResponseEntity<Enemy> findById(
}

@PutMapping("/enemies/{id}")
@CacheEvict(value = "enemy", key = "#id")
@CacheEvict(value = "enemy", allEntries = true)
public ResponseEntity<MessageResponse> updateEnemyById(
@PathVariable int id,
@RequestBody EnemyDTO enemy
Expand Down Expand Up @@ -225,7 +225,7 @@ private EffectDTO processEffects(EffectDTO inputEffect) {
}

@DeleteMapping("/enemies/{id}")
@CacheEvict(value = "enemy", key = "#id")
@CacheEvict(value = "enemy", allEntries = true)
public ResponseEntity<MessageResponse> deleteEnemy(@PathVariable int id) {
EnemyDTO enemyDTO = enemyRepo
.findById(id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import cz.trailsthroughshadows.api.util.reflect.Sorting;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -27,7 +28,7 @@ public class ItemController {
private ItemRepo itemRepo;

@GetMapping("/items")
@Cacheable(value = "item")
@Cacheable(value = "item", key="T(java.util.Objects).hash(#page, #limit, #filter, #sort, #include, #lazy)")
public ResponseEntity<RestPaginatedResult<Item>> findAllEntities(
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "100") int limit,
Expand Down Expand Up @@ -60,7 +61,7 @@ public ResponseEntity<RestPaginatedResult<Item>> findAllEntities(
}

@GetMapping("/items/{id}")
@Cacheable(value = "item", key = "#id")
@Cacheable(value = "item", key="T(java.util.Objects).hash(#id, #include, #lazy)")
public ResponseEntity<Item> findById(
@PathVariable int id,
@RequestParam(required = false, defaultValue = "") List<String> include,
Expand All @@ -80,7 +81,7 @@ public ResponseEntity<Item> findById(
}

@PostMapping("/items")
@Cacheable(value = "item")
@CacheEvict(value = "item", allEntries = true)
public ResponseEntity<MessageResponse> createEntity(@RequestBody List<ItemDTO> enTITIES) {
enTITIES.forEach(validation::validate);
List<ItemDTO> saved = itemRepo.saveAll(enTITIES);
Expand All @@ -90,7 +91,7 @@ public ResponseEntity<MessageResponse> createEntity(@RequestBody List<ItemDTO> e
}

@PutMapping("/items/{id}")
@Cacheable(value = "item", key = "#id")
@CacheEvict(value = "item", allEntries = true)
public ResponseEntity<MessageResponse> update(@PathVariable int id, @RequestBody ItemDTO item) {
validation.validate(item);

Expand All @@ -112,7 +113,7 @@ public ResponseEntity<MessageResponse> update(@PathVariable int id, @RequestBody
}

@DeleteMapping("/items/{id}")
@Cacheable(value = "item", key = "#id")
@CacheEvict(value = "item", allEntries = true)
public ResponseEntity<MessageResponse> delete(@PathVariable int id) {
ItemDTO entityToDelete = itemRepo
.findById(id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import cz.trailsthroughshadows.api.util.reflect.Sorting;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -37,6 +39,7 @@ public class AdventureController {
private CharacterService characterService;

@GetMapping("/{id}")
@Cacheable(value = "adventure", key="T(java.util.Objects).hash(#id, #include, #lazy)")
public ResponseEntity<Adventure> findById(
@PathVariable int id,
@RequestParam(required = false, defaultValue = "") List<String> include,
Expand All @@ -61,6 +64,7 @@ public ResponseEntity<Adventure> findById(
}

@GetMapping("")
@Cacheable(value = "adventure", key="T(java.util.Objects).hash(#page, #limit, #filter, #sort, #include, #lazy)")
public ResponseEntity<RestPaginatedResult<AdventureDTO>> findAllEntities(
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "100") int limit,
Expand Down Expand Up @@ -97,6 +101,7 @@ public ResponseEntity<RestPaginatedResult<AdventureDTO>> findAllEntities(
}

@PostMapping("/{idLicense}")
@CacheEvict(value = "adventure", allEntries = true)
public ResponseEntity<RestResponse> addAdventure(
@PathVariable int idLicense,
@RequestBody AdventureDTO adventure,
Expand All @@ -107,6 +112,7 @@ public ResponseEntity<RestResponse> addAdventure(
}

@PutMapping("/{id}")
@CacheEvict(value = "adventure", allEntries = true)
public ResponseEntity<RestResponse> updateAdventure(
@PathVariable int id,
@RequestBody AdventureDTO adventure,
Expand All @@ -117,6 +123,7 @@ public ResponseEntity<RestResponse> updateAdventure(
}

@DeleteMapping("/{id}")
@CacheEvict(value = "adventure", allEntries = true)
public ResponseEntity<RestResponse> deleteAdventure(
@PathVariable int id,
HttpServletRequest request
Expand All @@ -126,6 +133,7 @@ public ResponseEntity<RestResponse> deleteAdventure(
}

@GetMapping("/{id}/characters")
@Cacheable(value = "character", key="T(java.util.Objects).hash(#page, #limit, #filter, #sort, #include, #lazy)")
public ResponseEntity<RestPaginatedResult<Character>> findAllEntities(
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "100") int limit,
Expand Down Expand Up @@ -165,6 +173,7 @@ public ResponseEntity<RestPaginatedResult<Character>> findAllEntities(
}

@PostMapping("/{id}/characters")
@CacheEvict(value = "character", allEntries = true)
public ResponseEntity<RestResponse> addCharacter(
@PathVariable int id,
@RequestBody CharacterDTO character,
Expand Down
Loading

0 comments on commit b35fce3

Please sign in to comment.