Loading...
Spring Framework Reference Documentation 7.0.2의 HTTP Caching의 한국어 번역본입니다.
아래의 경우에 피드백에서 신고해주신다면 반영하겠습니다.
감사합니다 :)
See equivalent in the Servlet stack
HTTP 캐싱은 웹 애플리케이션의 성능을 크게 향상시킬 수 있습니다. HTTP 캐싱은 Cache-Control 응답 헤더와 Last-Modified, ETag 같은 이후의 조건부 요청 헤더를 중심으로 이루어집니다. Cache-Control은 private(예: 브라우저) 캐시와 public(예: 프록시) 캐시에 응답을 어떻게 캐시하고 재사용할지 알려 줍니다.
ETag 헤더는 콘텐츠가 변경되지 않은 경우 body 없이 304 (NOT_MODIFIED)를 반환할 수도 있는 조건부 요청을 만드는 데 사용됩니다. ETag는 Last-Modified 헤더의 더 정교한 후속 버전으로 볼 수 있습니다.
이 섹션에서는 Spring WebFlux에서 사용 가능한 HTTP 캐싱 관련 옵션을 설명합니다.
CacheControlSee equivalent in the Servlet stack
CacheControl은 Cache-Control 헤더와 관련된 설정을 구성하는 기능을 제공하며, 여러 곳에서 인수로 사용됩니다:
RFC 7234는 Cache-Control 응답 헤더에 대한 모든 가능한 지시어를 설명하지만, CacheControl 타입은 다음 예시와 같이 일반적인 시나리오에 초점을 맞춘 사용 사례 지향적 접근 방식을 취합니다:
1// Cache for an hour - "Cache-Control: max-age=3600" 2CacheControl ccCacheOneHour = CacheControl.maxAge(1, TimeUnit.HOURS); 3 4// Prevent caching - "Cache-Control: no-store" 5CacheControl ccNoStore = CacheControl.noStore(); 6 7// Cache for ten days in public and private caches, 8// public caches should not transform the response 9// "Cache-Control: max-age=864000, public, no-transform" 10CacheControl ccCustom = CacheControl.maxAge(10, TimeUnit.DAYS).noTransform().cachePublic();
1// Cache for an hour - "Cache-Control: max-age=3600" 2val ccCacheOneHour = CacheControl.maxAge(1, TimeUnit.HOURS) 3 4// Prevent caching - "Cache-Control: no-store" 5val ccNoStore = CacheControl.noStore() 6 7// Cache for ten days in public and private caches, 8// public caches should not transform the response 9// "Cache-Control: max-age=864000, public, no-transform" 10val ccCustom = CacheControl.maxAge(10, TimeUnit.DAYS).noTransform().cachePublic()
See equivalent in the Servlet stack
Controller는 HTTP 캐싱에 대한 명시적인 지원을 추가할 수 있습니다. lastModified나 ETag 값은 조건부 요청 헤더와 비교되기 전에 리소스에 대해 계산되어야 하므로 그렇게 할 것을 권장합니다.
Controller는 다음 예시와 같이 ResponseEntity에 ETag와 Cache-Control 설정을 추가할 수 있습니다:
1@GetMapping("/book/{id}") 2public ResponseEntity<Book> showBook(@PathVariable Long id) { 3 4 Book book = findBook(id); 5 String version = book.getVersion(); 6 7 return ResponseEntity 8 .ok() 9 .cacheControl(CacheControl.maxAge(30, TimeUnit.DAYS)) 10 .eTag(version) // lastModified is also available 11 .body(book); 12}
1@GetMapping("/book/{id}") 2fun showBook(@PathVariable id: Long): ResponseEntity<Book> { 3 4 val book = findBook(id) 5 val version = book.getVersion() 6 7 return ResponseEntity 8 .ok() 9 .cacheControl(CacheControl.maxAge(30, TimeUnit.DAYS)) 10 .eTag(version) // lastModified is also available 11 .body(book) 12}
앞의 예시는 조건부 요청 헤더와의 비교 결과 콘텐츠가 변경되지 않은 것으로 나타나면 body가 비어 있는 304 (NOT_MODIFIED) 응답을 전송합니다. 그렇지 않으면 ETag와 Cache-Control 헤더가 응답에 추가됩니다.
다음 예시와 같이 controller에서 조건부 요청 헤더에 대한 체크를 수행할 수도 있습니다:
1@RequestMapping 2public String myHandleMethod(ServerWebExchange exchange, Model model) { 3 4 long eTag = ...; (1) 5 6 if (exchange.checkNotModified(eTag)) { 7 return null; (2) 8 } 9 10 model.addAttribute(...); (3) 11 return "myViewName"; 12}
| 1 | 애플리케이션별 계산. |
| 2 | 응답이 304 (NOT_MODIFIED)로 설정되었습니다. 추가 처리 없음. |
| 3 | 요청 처리를 계속합니다. |
1@RequestMapping 2fun myHandleMethod(exchange: ServerWebExchange, model: Model): String? { 3 4 val eTag: Long = ... (1) 5 6 if (exchange.checkNotModified(eTag)) { 7 return null (2) 8 } 9 10 model.addAttribute(...) (3) 11 return "myViewName" 12}
| 1 | 애플리케이션별 계산. |
| 2 | 응답이 304 (NOT_MODIFIED)로 설정되었습니다. 추가 처리 없음. |
| 3 | 요청 처리를 계속합니다. |
eTag 값, lastModified 값 또는 둘 다에 대해 조건부 요청을 확인하는 세 가지 변형이 있습니다. 조건부 GET 및 HEAD 요청의 경우 응답을 304 (NOT_MODIFIED)로 설정할 수 있습니다. 조건부 POST, PUT, DELETE의 경우 동시 수정(concurrent modification)을 방지하기 위해 대신 응답을 412 (PRECONDITION_FAILED)로 설정할 수 있습니다.
See equivalent in the Servlet stack
최적의 성능을 위해 Cache-Control 및 조건부 응답 헤더와 함께 정적 리소스를 제공해야 합니다. 설정에 대한 섹션은 Static Resources를 참조하십시오.
Web Security
View Technologies