https://spring.io/guides/gs/caching/
Spring | Home
Cloud Your code, any cloud—we’ve got you covered. Connect and scale your services, whatever your platform.
spring.io
틀린 해석이 있다면 알려주세요 💐
github에 이미 소스가 아주 예쁘게 올라가 있어서 굳이 할 필요는 없지만 쭈욱 읽어보면서 핵심 기능들에 대한 코드를 정리해볼 생각이다
전체 파일 소스는 위의 출처 링크에서 깃허브로 들어가면 볼 수 있다
각 코드 부분에 대한 설명도 잘 적혀있어서 참고하기 좋았다
@SpringBootApplication is a convenience annotation that adds all of the following:
- @Configuration: Tags the class as a source of bean definitions for the application context.
- @EnableAutoConfiguration: Tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings. For example, if spring-webmvc is on the classpath, this annotation flags the application as a web application and activates key behaviors, such as setting up a DispatcherServlet.
- @ComponentScan: Tells Spring to look for other components, configurations, and services in the com/example package, letting it find the controllers.
You also need a CommandLineRunner that injects the BookRepository and calls it several times with different arguments. The following listing (from src/main/java/com/example/caching/AppRunner.java) shows that class:
Spring Boot Reference Documentation
This section goes into more detail about how you should use Spring Boot. It covers topics such as build systems, auto-configuration, and how to run your applications. We also cover some Spring Boot best practices. Although there is nothing particularly spe
docs.spring.io
package com.example.caching;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class AppRunner implements CommandLineRunner {
private static final Logger logger = LoggerFactory.getLogger(AppRunner.class);
private final BookRepository bookRepository;
public AppRunner(BookRepository bookRepository) {
this.bookRepository = bookRepository;
}
@Override
public void run(String... args) throws Exception {
logger.info(".... Fetching books");
logger.info("isbn-1234 -->" + bookRepository.getByIsbn("isbn-1234"));
logger.info("isbn-4567 -->" + bookRepository.getByIsbn("isbn-4567"));
logger.info("isbn-1234 -->" + bookRepository.getByIsbn("isbn-1234"));
logger.info("isbn-4567 -->" + bookRepository.getByIsbn("isbn-4567"));
logger.info("isbn-1234 -->" + bookRepository.getByIsbn("isbn-1234"));
logger.info("isbn-1234 -->" + bookRepository.getByIsbn("isbn-1234"));
}
}
캐싱을 사용하지 않을 경우 똑같은 요청에도 같은 데이터를 여러번 가져오게 된다
간단한 주석처리만으로 데이터 캐싱이 된다
package com.example.caching;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
@Component
public class SimpleBookRepository implements BookRepository {
@Override
@Cacheable("books")
public Book getByIsbn(String isbn) {
simulateSlowService();
return new Book(isbn, "Some book");
}
// Don't do this at home
private void simulateSlowService() {
try {
long time = 3000L;
Thread.sleep(time);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
}
}
캐싱이 잘 관리되고 있는지 보려면 commandLineRunner 나 테스트를 아주 잘 작성할 필요가 있을 것 같다
'Web > spring' 카테고리의 다른 글
[Spring Framework core] 1.4. Dependencies (2) (0) | 2023.03.01 |
---|---|
[Spring Framework core] 1.4. Dependencies (1) (0) | 2023.03.01 |
[Spring Boot IO] 1. Caching (0) | 2023.02.28 |
[Spring Framework integration] 6.Cache Abstraction (2) (0) | 2023.02.28 |
[Spring Framework integration] 6.Cache Abstraction (1) (0) | 2023.02.27 |