Skip to content

Commit

Permalink
Feat: caching for bin location address (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
pingowl authored May 29, 2024
1 parent dac7429 commit 591aea9
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'com.mysql:mysql-connector-j'
implementation 'org.springframework.boot:spring-boot-starter-cache'

// lombok
compileOnly 'org.projectlombok:lombok'
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/drugbox/common/config/CacheConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.drugbox.common.config;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableCaching
public class CacheConfig {
@Bean
public CacheManager cacheManager() {
return new ConcurrentMapCacheManager("addresses");
}
}
3 changes: 3 additions & 0 deletions src/main/java/com/drugbox/service/MapService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -132,13 +133,15 @@ public void saveDrugBinLocations(){
}
}

@Cacheable("addresses")
public List<BinLocationResponse> getSeoulDrugBinLocations(){
List<BinLocation> binLocations = binLocationRepository.findAll();
return binLocations.stream()
.map(bin -> BinLocationToBinLocationResponse(bin))
.collect(Collectors.toList());
}

@Cacheable("addresses")
public List<BinLocationResponse> getDivisionDrugBinLocations(String addrLvl1, String addrLvl2){
List<BinLocation> binLocations = new ArrayList<>();
if(addrLvl2 == null){
Expand Down
45 changes: 45 additions & 0 deletions src/test/java/com/drugbox/CacheTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.drugbox;

import com.drugbox.service.MapService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cache.CacheManager;
import org.springframework.test.context.junit4.SpringRunner;

import static org.junit.Assert.assertTrue;

@RunWith(SpringRunner.class)
@SpringBootTest
public class CacheTest {

@Autowired
MapService mapService;

@Autowired
private CacheManager cacheManager;

@BeforeEach
public void setUp() {
cacheManager.getCache("addresses").clear();
mapService.saveSeoulDrugBinLocations();
}

@Test
public void 서울_주소_캐싱_테스트(){
long start = System.currentTimeMillis();
mapService.getSeoulDrugBinLocations();
long executionTimeWithoutCache = System.currentTimeMillis() - start;
System.out.println("Execution time without cache: " + executionTimeWithoutCache + "ms");

start = System.currentTimeMillis();
mapService.getSeoulDrugBinLocations();
long executionTimeWithCache = System.currentTimeMillis() - start;
System.out.println("Execution time with cache: " + executionTimeWithCache + "ms");

assertTrue("Execution time with cache should be less than without cache",
executionTimeWithoutCache > executionTimeWithCache);
}
}

0 comments on commit 591aea9

Please sign in to comment.