Skip to content

Commit

Permalink
feat: 견종 목록 조회시 로컬 캐시 적용 (#533)
Browse files Browse the repository at this point in the history
* build: 의존성 추가

* feat: 캐시 설정 추가

* feat: 견종 목록 읽기시 캐싱 적용

* feat: 견종 등록 API 추가 및 등록시 캐싱 해제되도록 설정
  • Loading branch information
iamjooon2 authored Nov 9, 2023
1 parent 727dbcc commit d37fe10
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 2 deletions.
3 changes: 3 additions & 0 deletions backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-cache'

implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect'
Expand Down Expand Up @@ -70,6 +71,8 @@ dependencies {
implementation 'software.amazon.awssdk:s3:2.20.121'
implementation 'software.amazon.awssdk:url-connection-client:2.20.121'

implementation 'com.github.ben-manes.caffeine:caffeine'

testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.rest-assured:rest-assured:5.3.1'

Expand Down
13 changes: 13 additions & 0 deletions backend/src/main/java/zipgo/admin/application/AdminService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import zipgo.admin.dto.BrandCreateRequest;
Expand All @@ -11,6 +12,10 @@
import zipgo.admin.dto.PrimaryIngredientCreateRequest;
import zipgo.brand.domain.Brand;
import zipgo.brand.domain.repository.BrandRepository;
import zipgo.pet.domain.Breed;
import zipgo.pet.domain.PetSize;
import zipgo.pet.domain.repository.BreedRepository;
import zipgo.pet.domain.repository.PetSizeRepository;
import zipgo.petfood.domain.Functionality;
import zipgo.petfood.domain.PetFood;
import zipgo.petfood.domain.PetFoodFunctionality;
Expand All @@ -31,6 +36,8 @@ public class AdminService {
private final FunctionalityRepository functionalityRepository;
private final PrimaryIngredientRepository primaryIngredientRepository;
private final PetFoodRepository petFoodRepository;
private final BreedRepository breedRepository;
private final PetSizeRepository petSizeRepository;

public Long createBrand(BrandCreateRequest request, String imageUrl) {
Brand brand = request.toEntity(imageUrl);
Expand Down Expand Up @@ -149,4 +156,10 @@ public void deletePetFood(Long petFoodId) {
petFoodRepository.deleteById(petFoodId);
}

@CacheEvict(cacheNames = "breeds", key = "'allBreeds'")
public Long createBreed(Long petSizeId, String breedName) {
PetSize petSize = petSizeRepository.getReferenceById(petSizeId);
Breed breed = breedRepository.save(Breed.builder().name(breedName).petSize(petSize).build());
return breed.getId();
}
}
8 changes: 8 additions & 0 deletions backend/src/main/java/zipgo/admin/dto/BreedCreateRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package zipgo.admin.dto;

public record BreedCreateRequest(
Long petSizeId,
String breedName
) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import zipgo.admin.application.AdminService;
import zipgo.admin.dto.BrandCreateRequest;
import zipgo.admin.dto.BrandSelectResponse;
import zipgo.admin.dto.BreedCreateRequest;
import zipgo.admin.dto.FunctionalityCreateRequest;
import zipgo.admin.dto.FunctionalitySelectResponse;
import zipgo.admin.dto.PetFoodCreateRequest;
Expand Down Expand Up @@ -127,4 +128,12 @@ public ResponseEntity<Void> deletePetFood(@PathVariable Long petFoodId) {
return ResponseEntity.noContent().build();
}

@PostMapping("/breeds")
public ResponseEntity<Long> createBreeds(
@RequestBody BreedCreateRequest breedCreateRequest
) {
Long breedId = adminService.createBreed(breedCreateRequest.petSizeId(), breedCreateRequest.breedName());
return ResponseEntity.created(URI.create("/breeds/" + breedId)).build();
}

}
16 changes: 16 additions & 0 deletions backend/src/main/java/zipgo/common/cache/CacheType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package zipgo.common.cache;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum CacheType {

BREEDS("breeds", 1),
;

private final String name;
private final int maxSize;

}
39 changes: 39 additions & 0 deletions backend/src/main/java/zipgo/common/config/CacheConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package zipgo.common.config;

import java.util.Arrays;
import java.util.List;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import zipgo.common.cache.CacheType;

@EnableCaching
@Configuration
public class CacheConfig {

@Bean
public CacheManager cacheManager() {
SimpleCacheManager cacheManager = new SimpleCacheManager();
cacheManager.setCaches(caches());
return cacheManager;
}

private List<CaffeineCache> caches() {
return Arrays.stream(CacheType.values())
.map(cacheType -> new CaffeineCache(cacheType.getName(), cache(cacheType)))
.toList();
}

private Cache<Object, Object> cache(CacheType cacheType) {
return Caffeine.newBuilder()
.maximumSize(cacheType.getMaxSize())
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public Pet readPet(Long petId) {
}

public List<Breed> readBreeds() {
Breeds breeds = Breeds.from(breedRepository.findAll());
Breeds breeds = Breeds.from(breedRepository.findAllBreeds());
return breeds.getOrderedBreeds();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@

import java.util.List;
import java.util.Optional;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.jpa.repository.JpaRepository;
import zipgo.pet.domain.Breed;
import zipgo.pet.domain.PetSize;
import zipgo.pet.exception.BreedsNotFoundException;

public interface BreedRepository extends JpaRepository<Breed, Long> {

@Cacheable(cacheNames = "breeds", key = "'allBreeds'")
default List<Breed> findAllBreeds() {
return findAll();
}

Optional<Breed> findByPetSizeAndName(PetSize petSize, String name);

default Breed getByPetSizeAndName(PetSize petSize, String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public GetReviewMetadataResponse getReviewMetadata() {
}

private List<Metadata> findAllBreeds() {
Breeds breeds = Breeds.from(breedRepository.findAll());
Breeds breeds = Breeds.from(breedRepository.findAllBreeds());
return breeds.getOrderedBreeds().stream()
.map(breed -> new Metadata(breed.getId(), breed.getName()))
.toList();
Expand Down

0 comments on commit d37fe10

Please sign in to comment.