Skip to content

Commit

Permalink
feature: caffeine 캐싱 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
choihuk committed Dec 13, 2023
1 parent 05bbaa8 commit 6fd55a1
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 3 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
}
33 changes: 33 additions & 0 deletions src/main/java/aplus/insurancesystem/common/cache/CacheType.java
Original file line number Diff line number Diff line change
@@ -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;

}
42 changes: 42 additions & 0 deletions src/main/java/aplus/insurancesystem/common/config/CacheConfig.java
Original file line number Diff line number Diff line change
@@ -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<CaffeineCache> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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());
Expand All @@ -98,13 +103,15 @@ 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);
}

@Override
@Transactional
@CacheEvict(value = CacheConst.INSURANCE_DETAIL, key = "#insuranceId")
public void registerInsurance(Long insuranceId) {
Insurance insurance = insuranceQueryService.getInsurance(insuranceId);
insurance.setAuthorization(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand All @@ -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());
Expand All @@ -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)
Expand Down

0 comments on commit 6fd55a1

Please sign in to comment.