μ‘°ν μ±λ₯μ΄ μ νλλ 쿼리μ λν μΊμ± λ° Redisλ₯Ό νμ©νμ¬ μ±λ₯μ κ°μ ν μ μλ λ°©μμ μ‘°μ¬ν©λλ€.
μ½μνΈ λκΈ°μ΄ μλΉμ€μμλ νΉμ μ½μνΈ μΌμ λ°μ΄ν°κ° μμ£Ό μ‘°ν λ©λλ€. κ°μ μ½μνΈμ λν΄ λ€μμ μ¬μ©μκ° λμμ μ‘°ν μμ²μ 보λ΄λ κ²½μ°, λ°μ΄ν°λ² μ΄μ€μ λΆνκ° μ§μ€λμ΄ μ±λ₯ μ νλ μ§μ°μ΄ λ°μν μ μμ΅λλ€. μ΄λ₯Ό ν΄κ²°νκ³ μ μ½μνΈ μΌμ μ‘°ν μ Redis μΊμ±μ μ μ©νμ¬, λ°μ΄ν°λ² μ΄μ€μ λΆνλ₯Ό μ€μ΄κ³ μλ΅ μλλ₯Ό κ°μ νκ³ μ ν©λλ€.
λ°μ΄ν°λ² μ΄μ€ λΆν κ°μ: Redisλ λ©λͺ¨λ¦¬ κΈ°λ° λ°μ΄ν° μ μ₯μλ‘ μ‘°ν μλκ° λ§€μ° λΉ λ¦ λλ€. μ΄λ₯Ό ν΅ν΄ λ°μ΄ν°λ² μ΄μ€μ μ§μ μ κ·Όνλ λΉλλ₯Ό μ€μ΄κ³ , λ°μ΄ν°λ² μ΄μ€μ λΆνλ₯Ό κ°μμν¬ μ μμ΅λλ€.
λΉ λ₯Έ μλ΅ μκ°: μΊμλ λ°μ΄ν°λ₯Ό μ¬μ©νλ©΄ μ¬μ©μ μμ²μ μ¦κ°μ μΌλ‘ μλ΅ν μ μμ΄ μ±λ₯μ κ°μ ν μ μμ΅λλ€.
TTL μ€μ κ°λ₯: Redisλ μΊμλ λ°μ΄ν°μ μ ν¨ κΈ°κ°μ μ€μ ν μ μλ TTLμ μ§μν©λλ€.
λ€μν λ°μ΄ν° ꡬ쑰 μ§μ: Redisλ λ¬Έμμ΄λΏλ§ μλλΌ λ¦¬μ€νΈ, ν΄μ λ± λ€μν λ°μ΄ν° ꡬ쑰λ₯Ό μ§μνλ―λ‘, λ€μν λ°μ΄ν°λ₯Ό ν¨κ³Όμ μΌλ‘ μ μ₯νκ³ μ‘°νν μ μμ΅λλ€.
@EnableCaching
@Configuration
public class RedissonConfig {
@Bean
public CacheManager contentCacheManager(RedisConnectionFactory cf) {
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()))
.entryTtl(Duration.ofMinutes(3L)); // μΊμ μλͺ
3λΆ μ€μ
return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(cf).cacheDefaults(redisCacheConfiguration).build();
}
@Bean
public RedissonClient redissonClient() {
Config config = new Config();
config.useSingleServer().setAddress("redis://localhost:6379");
return Redisson.create(config);
}
}
Redis μΊμλ₯Ό ꡬμ±νλ λ° νμν CacheManager, RedisCacheConfiguration, RedisConnectionFactory λ±μ μ μν©λλ€.
serializeKeysWith
: Redis μΊμμμ μ¬μ©λ μΊμ ν€μ μ§λ ¬ν λ°©μμ μ€μ ν©λλ€. μ΄ μμ μμλ StringRedisSerializerλ₯Ό μ¬μ©νμ¬, μΊμμ ν€λ₯Ό λ¬Έμμ΄ ννλ‘ μ§λ ¬νν©λλ€.
serializeValuesWith
: μΊμ κ°μ μ§λ ¬ν λ°©μμ μ€μ ν©λλ€. μ¬κΈ°μλ GenericJackson2JsonRedisSerializerλ₯Ό μ¬μ©νμ¬, μΊμμ κ°μ JSON νμμΌλ‘ μ§λ ¬νν©λλ€. μ΄λ 볡μ‘ν κ°μ²΄λ€μ Redisμ μ μ₯ν λ μ μ©νλ©°, μ§λ ¬νλ λ°μ΄ν°λ₯Ό JSON νμμΌλ‘ λ³ννμ¬ Redisμ μ μ₯ν©λλ€.
entryTtl(Duration.ofMinutes(3L))
: μΊμ νλͺ©μ TTLμ μ€μ ν©λλ€. μΊμ νλͺ©μ 3λΆμ΄ μ§λλ©΄ λ§λ£λμ΄ Redisμμ μλμΌλ‘ μμ λ©λλ€.
@Cacheable(value = "concertSchedules")
public List<Schedule> redisCacheableSchedule(Long concertId) {
List<Schedule> schedules = scheduleRepository.existsSchedule(concertId);
Schedule.existSchedules(schedules);
return schedules;
}
@Cacheable
μ΄λ
Έν
μ΄μ
μ μ¬μ©νμ¬ Redis μΊμμμ μ€μΌμ€μ μ‘°ννκ±°λ, μΊμκ° μμΌλ©΄ λ°μ΄ν°λ² μ΄μ€μμ μ‘°ννμ¬ κ²°κ³Όλ₯Ό μΊμ±ν©λλ€.
@Test
void μ€μΌμ€_μ‘°ν_μ±κ³΅() {
// Given
Long concertId = 1L;
long startTime = System.nanoTime();
// When
List<Schedule> schedules = scheduleService.schedule(concertId);
// Then
long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println("μ€μΌμ€ μ‘°ν κ²½κ³Ό μκ° (λλ
Έμ΄): " + duration);
Assertions.assertThat(schedules).hasSize(2);
}
@Test
void λ λμ€_μΊμ±_μ€μΌμ€_μ‘°ν_μ±κ³΅() {
// Given
Long concertId = 1L;
long startTime = System.nanoTime();
// When
List<Schedule> schedules = scheduleService.redisCacheableSchedule(concertId);
// Then
long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println("λ λμ€ μΊμ± μ€μΌμ€ μ‘°ν κ²½κ³Ό μκ° (λλ
Έμ΄): " + duration);
Assertions.assertThat(schedules).hasSize(2);
}
λ°μ΄ν°λ² μ΄μ€μμ μ§μ μ€μΌμ€μ μ‘°ννλ μ±λ₯κ³Ό Redis μΊμμμ μ€μΌμ€μ μ‘°ννλ μ±λ₯μ λΆμνλ μ½λλ₯Ό μμ±νμ¬ Redis μΊμ±μ΄ μ±λ₯μ λ―ΈμΉλ μν₯μ νκ°νκ³ , μΊμ νμ©μΌλ‘ μλ΅ μκ°μ΄ μΌλ§λ κ°μ λλμ§ νμΈνλ €κ³ ν©λλ€.
GET concertSchedules::1
λͺ
λ Ήμ΄λ₯Ό ν΅ν΄ λ€μκ³Ό κ°μ΄ μΊμ± λ°μ΄ν°λ₯Ό νμΈν μ μμμ΅λλ€.
ν μ€νΈ κ²°κ³Ό, Redis μΊμλ₯Ό νμ©ν μ‘°νλ λ°μ΄ν°λ² μ΄μ€μμ μ§μ μ‘°ννλ κ²λ³΄λ€ λΉ λ₯Έ μ±λ₯μ 보μμ΅λλ€. ꡬ체μ μΌλ‘, λ°μ΄ν°λ² μ΄μ€μμ μ§μ μ‘°νν κ²½κ³Ό μκ°μ μ½ 338.86msμκ³ , Redis μΊμμμ μ‘°νν κ²½κ³Ό μκ°μ μ½ 259.24mλ‘ μ½ 22.6% λΉ λ₯Έ μ±λ₯μ 보μμ΅λλ€.
Redis μΊμκ° λ°μ΄ν°λ² μ΄μ€μ λΆλ΄μ μ€μ¬μ£Όκ³ , λ°λ³΅μ μΌλ‘ μμ²λλ λ°μ΄ν°λ₯Ό λΉ λ₯΄κ² λ°νν μ μλ€λ κ²μ νμΈν μ μμμ΅λλ€. μΊμλ λ°μ΄ν°λ λ©λͺ¨λ¦¬μμ λ°λ‘ μ‘°νλκΈ° λλ¬Έμ λ°μ΄ν°λ² μ΄μ€ μ κ·Ό μκ°μ λ¨μΆμν¬ μ μμ΅λλ€.
Redis μΊμ±μ λ°μ΄ν°λ² μ΄μ€ μ‘°ν μ±λ₯μ κ°μ νλ κΈ°μ λ‘, λ°λ³΅μ μΈ λ°μ΄ν° μμ²μ λν μλ΅ μλλ₯Ό ν₯μμν¬ μ μμ΅λλ€. Redisλ₯Ό νμ©ν μΊμ μ‘°νκ° λ°μ΄ν°λ² μ΄μ€ μ‘°νλ³΄λ€ λΉ λ₯΄λ€λ μ μ ν° μ₯μ μ΄λΌκ³ μκ°ν©λλ€.