Open skarltjr opened 2 years ago
@EnableCaching
@Configuration
public class RedisConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Bean
public RedisConnectionFactory redisConnectionFactory() {
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
redisStandaloneConfiguration.setPort(port);
redisStandaloneConfiguration.setHostName(host);
LettuceConnectionFactory lettuceConnectionFactory =
new LettuceConnectionFactory(redisStandaloneConfiguration);
return lettuceConnectionFactory;
}
@Bean
public RedisCacheManager redisCacheManager() {
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.serializeKeysWith(
RedisSerializationContext.SerializationPair.fromSerializer(
new StringRedisSerializer()))
.serializeValuesWith(
RedisSerializationContext.SerializationPair.fromSerializer(
new GenericJackson2JsonRedisSerializer()))
.disableCachingNullValues()
.entryTtl(Duration.ofHours(1L));
return RedisCacheManager.RedisCacheManagerBuilder
.fromConnectionFactory(redisConnectionFactory())
.cacheDefaults(redisCacheConfiguration)
.build();
}
}
@GetMapping("/cache/{id}")
@Cacheable(value = "MyEntity",key="#id",unless = "#result==null",cacheManager = "redisCacheManager")
public String handle(@PathVariable Long id) {
myService.withCache(id);
return "string";
}
@GetMapping("/non/{id}")
public String handle2(@PathVariable Long id) {
myService.withoutCache(id);
return "string";
}
@Service
@RequiredArgsConstructor
public class MyService {
//private final MyService2 myService2;
private final MyRepo myRepo;
public void init() {
myRepo.save(new MyEntity(null,1));
}
@Transactional(readOnly = true)
public void withCache(Long id) {
myRepo.findById(id);
}
@Transactional(readOnly = true)
public void withoutCache(Long id) {
myRepo.findById(id);
}
}
import time
from locust import HttpUser, task
from locust.user.wait_time import constant
class QuickstartUser(HttpUser):
wait_time = constant(1)
@task
def monitor_check(self):
self.client.get("/cache/1")
무엇을 확인하고자 하는가?
1. 캐시를 사용하지 않은 부하테스트 결과
2. 특정 데이터를 미리 캐시해둔 후 부하테스트
부하테스트 참고 : https://jobc.tistory.com/225