Cache-Control
์์ฆ ํ์ฌ์์ ์ฌ๋ฌ ์ด์๋ฅผ ํด๊ฒฐํ๋ค๋ณด๋ "Cache-Control" ์ด๋ผ๋ ๊ฒ์ ์๊ฒ ๋์๋ค.
์ค์ ๋ก ๋ค์ด๋ฒ ๋ํํ์ด์ง์์ ๋คํธ์ํฌ ์ฐฝ์ ๋ณด๋ฉด ์์ฒญ, ์๋ต๊ฐ์ "Cache-Control" ๋ฅผ ๋ณผ ์ ์๊ณ value ๊ฐ์ผ๋ฃ no-cache, no-store, must-revalidate ๋ฅผ ๋ณผ ์ ์๋ค.
๊ทธ๋ ๋ค๋ฉด ์ ํํ๊ฒ "Cache-Control" ์ด ๋ฌด์์ผ๊น? ์์๋ณด์
Cache-Control ์ด๋?
Cache-Control์ ์น ๋ธ๋ผ์ฐ์ ์ ์๋ฒ ๊ฐ์ ์บ์ฑ ์ ์ฑ ์ ์ค์ ํ๊ธฐ ์ํ HTTP ํค๋์ด๋ค. ์ด๋ฅผ ํตํด ํด๋ผ์ด์ธํธ์ ์๋ฒ ๊ฐ์ ๋ถํ์ํ ๋ฐ์ดํฐ ์ ์ก์ ์ค์ฌ ์ฑ๋ฅ์ ํฅ์์ํฌ ์ ์๋ค.
์ฃผ์ Cache-Control ๋๋ ํฐ๋ธ
- no-cache: ํญ์ ์๋ฒ์์ ์ต์ ๋ฆฌ์์ค๋ฅผ ๊ฐ์ ธ์ค๋๋ก ๊ฐ์ ํ๋ค.
- no-store : ๋ฆฌ์์ค๋ฅผ ์ ํ ์บ์ํ์ง ์๋๋ก ์ค์ ํ๋ค. ๋ฏผ๊ฐํ ๋ฐ์ดํฐ์ ์ฃผ๋ก ์ฌ์ฉ๋๋ค.
- public : ๋ฆฌ์์ค๋ฅผ ๋ชจ๋ ์บ์(๊ณต์ฉ, ๊ฐ์ธ)์์ ์บ์ํ ์ ์๋๋ก ํ์ฉํ๋ค.
- private : ํน์ ์ฌ์ฉ์์ ๋ํ ๊ฐ์ธ ์ ๋ณด๊ฐ ํฌํจ๋ ๋ฆฌ์์ค๋ฅผ ํด๋ผ์ด์ธํธ์์๋ง ์บ์ํ ์ ์๋๋ก ์ ํํ๋ค.
- max-age : ์บ์๋ ๋ฆฌ์์ค๋ฅผ ์ง์ ๋ ์๊ฐ(์ด) ๋์ ์ ํจํ๊ฒ ์ค์ ํ๋ค.
- s-maxage : ํ๋ก์ ์๋ฒ ์บ์์๋ง ์ ์ฉ๋๋ max-age ๋ก, CDN ๋๋ ๋ฆฌ๋ฒ์ค ํ๋ก์์ ์ ์ฉํ๋ค.
- must-revalidate : ๋ง๋ฃ๋ ๋ฆฌ์์ค๋ฅผ ์ฌ์ฉํ๊ธฐ ์ ์ ์๋ฒ์์ ์๋ก ๊ฐ์ ธ์ค๋๋ก ๊ฐ์ ํ๋ค.
Spring์์ Cache-Control ์ฌ์ฉํ๊ธฐ
Spring์์๋ @GetMapping๊ณผ ํจ๊ป CacheControl์ ์ค์ ํ์ฌ ํน์ ์๋ํฌ์ธํธ์ ์บ์ฑ ๋์์ ์ ์ดํ ์ ์๋ค.
์์์ฝ๋
๋ค์์ Cache-Control ์ ์ค์ ํ์ฌ max-age ๊ฐ 60์ด์ธ ์บ์ ์ ์ฑ ๊ณผ ํด๋ผ์ด์ธํธ๊ฐ ํญ์ ์๋ฒ์์ ์ต์ ๋ฒ์ ์ ๊ฐ์ ธ์ค๋ ์ ์ฑ ์ ์ ์ฉํ๋ ์์
import org.springframework.http.CacheControl;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.TimeUnit;
@RestController
@RequestMapping("/api")
public class ExampleController {
@GetMapping("/cache")
public ResponseEntity<String> cacheExample() {
return ResponseEntity.ok()
.cacheControl(CacheControl.maxAge(60, TimeUnit.SECONDS)) // ์บ์ ์ ํจ ์๊ฐ ์ค์
.body("This response is cached for 60 seconds.");
}
@GetMapping("/no-cache")
public ResponseEntity<String> noCacheExample() {
return ResponseEntity.ok()
.cacheControl(CacheControl.noCache()) // ํญ์ ์ต์ ๋ฆฌ์์ค๋ฅผ ๊ฐ์ ธ์ค๋๋ก ์ค์
.body("This response is not cached.");
}
}
์ค๋ช
- CacheControl.maxAge(60, TimeUnit.SECONDS) : ์บ์๋ฅผ 60์ด ๋์ ์ ํจํ๊ฒ ์ ์งํ๋ค.
- CacheControl.noCache() : ํด๋ผ์ด์ธํธ๊ฐ ํญ์ ์๋ฒ์์ ์ต์ ๋ฒ์ ์ ๊ฐ์ ธ์ค๋๋ก ํ๋ค.
Cache-Control ์ฌ์ฉ์ ์ฅ์
1. ์๋ฒ ๊ฐ์ ๋คํธ์ํฌ ํธ๋ํฝ์ ์กฐ์ ํ์ฌ ์๋ฒ ์ฑ๋ฅ์ ์ต์ ํ ํ ์ ์๋ค.
2. ๋์ ์ผ๋ก ๋ณํํด์ผ ํ๋ ๋ฐ์ดํฐ๋ฅผ ํจ์จ์ ์ผ๋ก ๊ฐ์ ธ์ฌ ์ ์๋ค.
'DevSecOps > ๋คํธ์ํฌ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
X-Forwarded-For ์ด๋ ? (0) | 2024.11.18 |
---|---|
ERR_CACHE_MISS (0) | 2024.11.11 |
๋ฒํผ๋ง์ด๋? (5) | 2024.11.03 |
fiddler ์ https ์ธ์ฆ์ ๊ผฌ์ ๋ฌธ์ (1) | 2024.10.18 |
๋คํธ์ํฌ DMZ ๋? ์์ฑ์ค (0) | 2024.10.12 |
๋๊ธ