-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: 견종 목록 조회시 로컬 캐시 적용 #533
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' | ||
|
@@ -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' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 가비의 설명에 덧붙이자면 아래와 같은 Cache Storage의 종류가 있습니다!
|
||
|
||
testImplementation 'org.springframework.boot:spring-boot-starter-test' | ||
testImplementation 'io.rest-assured:rest-assured:5.3.1' | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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); | ||
|
@@ -149,4 +156,10 @@ public void deletePetFood(Long petFoodId) { | |
petFoodRepository.deleteById(petFoodId); | ||
} | ||
|
||
@CacheEvict(cacheNames = "breeds", key = "'allBreeds'") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 어노테이션을 붙이지 않은 메서드에서만 캐싱이 되는건가요?? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저는 애노테이션을 붙이면 정보에 해당하는 캐시를 무효화하는 것이라 이해했습니당. cacheNames 는 캐시의 이름입니다. 키랑 다른 개념이요 캐시 매니저 빈을 생성할 때 캐시 이름을 등록하더라구요. @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();
}
@Getter
@AllArgsConstructor
public enum CacheType {
BREEDS("breeds", 1),
;
private final String name;
private final int maxSize;
} |
||
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(); | ||
} | ||
Comment on lines
+160
to
+164
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍🏻👍🏻👍🏻 |
||
} |
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 |
---|---|---|
@@ -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; | ||
|
||
} |
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(); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
spring-web에서 기본적으로 제공해주는 캐시 라이브러리(에를 들면 @EnabalCaching) 도 있는걸로 알고있는데 이 의존성은 뭔가요??
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wonyongChoi05
라고합니..