From 6fd55a1b4df370f321ea1f2985c62d8c79f7ae7c Mon Sep 17 00:00:00 2001 From: Hyeok_Choi Date: Wed, 13 Dec 2023 15:50:19 +0900 Subject: [PATCH] =?UTF-8?q?feature:=20caffeine=20=EC=BA=90=EC=8B=B1=20?= =?UTF-8?q?=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 + .../common/cache/CacheConst.java | 8 ++++ .../common/cache/CacheType.java | 33 +++++++++++++++ .../common/config/CacheConfig.java | 42 +++++++++++++++++++ .../service/InsuranceServiceImpl.java | 9 +++- .../customer/service/CustomerServiceImpl.java | 11 ++++- 6 files changed, 102 insertions(+), 3 deletions(-) create mode 100644 src/main/java/aplus/insurancesystem/common/cache/CacheConst.java create mode 100644 src/main/java/aplus/insurancesystem/common/cache/CacheType.java create mode 100644 src/main/java/aplus/insurancesystem/common/config/CacheConfig.java diff --git a/build.gradle b/build.gradle index 910fc25..6ad96c3 100644 --- a/build.gradle +++ b/build.gradle @@ -29,6 +29,8 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-security' implementation "com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.9.0" implementation 'org.springframework.boot:spring-boot-starter-security' + implementation 'org.springframework.boot:spring-boot-starter-cache' + implementation 'com.github.ben-manes.caffeine:caffeine' implementation 'net.nurigo:sdk:4.3.0' compileOnly 'org.projectlombok:lombok' diff --git a/src/main/java/aplus/insurancesystem/common/cache/CacheConst.java b/src/main/java/aplus/insurancesystem/common/cache/CacheConst.java new file mode 100644 index 0000000..c6053cd --- /dev/null +++ b/src/main/java/aplus/insurancesystem/common/cache/CacheConst.java @@ -0,0 +1,8 @@ +package aplus.insurancesystem.common.cache; + +public class CacheConst { + + public static final String INSURANCE_DETAIL = "insuranceDetail"; + public static final String CUSTOMER_INFO = "customerInfo"; + public static final String CUSTOMER_ALL_INFO = "customerAllInfo"; +} diff --git a/src/main/java/aplus/insurancesystem/common/cache/CacheType.java b/src/main/java/aplus/insurancesystem/common/cache/CacheType.java new file mode 100644 index 0000000..565f565 --- /dev/null +++ b/src/main/java/aplus/insurancesystem/common/cache/CacheType.java @@ -0,0 +1,33 @@ +package aplus.insurancesystem.common.cache; + +import lombok.Getter; + +@Getter +public enum CacheType { + INSURANCE_DETAIL( + CacheConst.INSURANCE_DETAIL, + 3 * 60 * 60, + 100 + ), + CUSTOMER_INFO( + CacheConst.CUSTOMER_INFO, + 3 * 60 * 60, + 10_000 + ), + CUSTOMER_ALL_INFO( + CacheConst.CUSTOMER_ALL_INFO, + 3 * 60 * 60, + 10_000 + ); + + CacheType(String cacheName, int expireAfterWrite, int maximumSize) { + this.cacheName = cacheName; + this.expireAfterWrite = expireAfterWrite; + this.maximumSize = maximumSize; + } + + private final String cacheName; + private final int expireAfterWrite; + private final int maximumSize; + +} diff --git a/src/main/java/aplus/insurancesystem/common/config/CacheConfig.java b/src/main/java/aplus/insurancesystem/common/config/CacheConfig.java new file mode 100644 index 0000000..10f246c --- /dev/null +++ b/src/main/java/aplus/insurancesystem/common/config/CacheConfig.java @@ -0,0 +1,42 @@ +package aplus.insurancesystem.common.config; + +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +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 com.github.benmanes.caffeine.cache.Caffeine; + +import aplus.insurancesystem.common.cache.CacheType; + +@EnableCaching +@Configuration +public class CacheConfig { + + @Bean + public CacheManager cacheManager() { + List caches = Arrays.stream(CacheType.values()) + .map( + cache -> new CaffeineCache( + cache.getCacheName(), + Caffeine.newBuilder() + .recordStats() + .expireAfterWrite(cache.getExpireAfterWrite(), TimeUnit.SECONDS) + .maximumSize(cache.getMaximumSize()) + .build() + ) + ) + .collect(Collectors.toList()); + + SimpleCacheManager cacheManager = new SimpleCacheManager(); + cacheManager.setCaches(caches); + return cacheManager; + } +} diff --git a/src/main/java/aplus/insurancesystem/domain/Insurance/service/InsuranceServiceImpl.java b/src/main/java/aplus/insurancesystem/domain/Insurance/service/InsuranceServiceImpl.java index 7fb915a..0f9da0f 100644 --- a/src/main/java/aplus/insurancesystem/domain/Insurance/service/InsuranceServiceImpl.java +++ b/src/main/java/aplus/insurancesystem/domain/Insurance/service/InsuranceServiceImpl.java @@ -4,15 +4,18 @@ import java.util.List; import java.util.stream.Collectors; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import aplus.insurancesystem.common.cache.CacheConst; import aplus.insurancesystem.domain.Insurance.dto.request.DesignInsuranceRequest; import aplus.insurancesystem.domain.Insurance.dto.request.UpdateInsuranceRequest; import aplus.insurancesystem.domain.Insurance.dto.response.InsuranceDetailResponse; import aplus.insurancesystem.domain.Insurance.entity.Guarantee; -import aplus.insurancesystem.domain.Insurance.entity.insurance.Insurance; import aplus.insurancesystem.domain.Insurance.entity.Terms; +import aplus.insurancesystem.domain.Insurance.entity.insurance.Insurance; import aplus.insurancesystem.domain.Insurance.exception.InsuranceNotFoundException; import aplus.insurancesystem.domain.Insurance.repository.GuaranteeRepository; import aplus.insurancesystem.domain.Insurance.repository.InsuranceRepository; @@ -30,6 +33,7 @@ public class InsuranceServiceImpl implements InsuranceService { private final TermsQueryService termsQueryService; @Override + @Cacheable(value = CacheConst.INSURANCE_DETAIL, key = "#insuranceId") public InsuranceDetailResponse getInsuranceDetail(Long insuranceId) { return insuranceRepository.findById(insuranceId) .map(InsuranceDetailResponse::of) @@ -73,6 +77,7 @@ public void designInsurance(DesignInsuranceRequest request) { @Override @Transactional + @CacheEvict(value = CacheConst.INSURANCE_DETAIL, key = "#insuranceId") public void updateInsurance(Long insuranceId, UpdateInsuranceRequest request) { Insurance insurance = insuranceQueryService.getInsurance(insuranceId); insurance.setInsuranceName(request.getInsuranceName()); @@ -98,6 +103,7 @@ public void updateInsurance(Long insuranceId, UpdateInsuranceRequest request) { @Override @Transactional + @CacheEvict(value = CacheConst.INSURANCE_DETAIL, key = "#insuranceId") public void deleteInsurance(Long insuranceId) { Insurance insurance = insuranceQueryService.getInsurance(insuranceId); insuranceRepository.delete(insurance); @@ -105,6 +111,7 @@ public void deleteInsurance(Long insuranceId) { @Override @Transactional + @CacheEvict(value = CacheConst.INSURANCE_DETAIL, key = "#insuranceId") public void registerInsurance(Long insuranceId) { Insurance insurance = insuranceQueryService.getInsurance(insuranceId); insurance.setAuthorization(true); diff --git a/src/main/java/aplus/insurancesystem/domain/customer/service/CustomerServiceImpl.java b/src/main/java/aplus/insurancesystem/domain/customer/service/CustomerServiceImpl.java index 1de4350..be88331 100644 --- a/src/main/java/aplus/insurancesystem/domain/customer/service/CustomerServiceImpl.java +++ b/src/main/java/aplus/insurancesystem/domain/customer/service/CustomerServiceImpl.java @@ -3,10 +3,13 @@ import java.util.List; import java.util.stream.Collectors; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.Cacheable; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import aplus.insurancesystem.common.cache.CacheConst; import aplus.insurancesystem.domain.contract.service.ContractService; import aplus.insurancesystem.domain.customer.dto.request.CustomerUpdateRequest; import aplus.insurancesystem.domain.customer.dto.request.JoinRequest; @@ -33,8 +36,9 @@ public class CustomerServiceImpl implements CustomerService { private final PasswordEncoder passwordEncoder; @Override - public CustomerInfoResponse getCustomerInfo(Long userId) { - return customerRepository.findById(userId) + @Cacheable(value = CacheConst.CUSTOMER_INFO, key = "#customerId") + public CustomerInfoResponse getCustomerInfo(Long customerId) { + return customerRepository.findById(customerId) .map(CustomerInfoResponse::of) .orElseThrow(CustomerNotFoundException::new); } @@ -58,6 +62,7 @@ public CustomerDetailResponse getCustomerDetail(Long customerId) { @Override @Transactional + @CacheEvict(value = {CacheConst.CUSTOMER_INFO, CacheConst.CUSTOMER_ALL_INFO}, key = "#customerId") public void updateCustomer(Long customerId, CustomerUpdateRequest request) { Customer customer = customerQueryService.getCustomer(customerId); customer.setCustomerName(request.getCustomerName()); @@ -70,12 +75,14 @@ public void updateCustomer(Long customerId, CustomerUpdateRequest request) { @Override @Transactional + @CacheEvict(value = {CacheConst.CUSTOMER_INFO, CacheConst.CUSTOMER_ALL_INFO}, key = "#customerId") public void deleteCustomer(Long customerId) { Customer customer = customerQueryService.getCustomer(customerId); customerRepository.delete(customer); } @Override + @Cacheable(value = CacheConst.CUSTOMER_ALL_INFO, key = "#customerId") public CustomerAllInfoResponse getCustomerAllInfo(Long customerId) { return customerRepository.findById(customerId) .map(CustomerAllInfoResponse::of)