# CDN Cache Control

## LL-HLS Origin Server Cache 제어하기

HTTP Response에 `Cache-Control` Header를 추가하여 Edge Server 또는 CDN Cache Server에서 콘텐츠를 Cache에 보관하는 시간을 지정 할 수 있습니다.

다음과 같이 `Server.xml`의 `<Publishers><LLHLS><CacheControl>`에서 설정 할 수 있습니다:

```xml
<?xml version="1.0" encoding="UTF-8"?>
<Server version="8">
  ...
  <VirtualHosts>
    <VirtualHost>
      <Applications>
        <Application>
          <Publishers>
            ...
            <LLHLS>
              <OriginMode>true</OriginMode>
              <CacheControl>
                <MasterPlaylistMaxAge>0</MasterPlaylistMaxAge>
                <ChunklistMaxAge>0</ChunklistMaxAge>
                <ChunklistWithDirectivesMaxAge>60</ChunklistWithDirectivesMaxAge>
                <SegmentMaxAge>-1</SegmentMaxAge>
                <PartialSegmentMaxAge>-1</PartialSegmentMaxAge>
              </CacheControl>
            </LLHLS>
          </Publishers>
        </Application>
      </Applications>
    </VirtualHost>
  </VirtualHosts>
</Server>
```

<table><thead><tr><th width="335">Element</th><th>Description</th></tr></thead><tbody><tr><td><code>MasterPlaylistMaxAge</code></td><td>Master Playlist의 max-age를 지정합니다. 이것은 Stream이 생성되어 있는 동안 변경되지 않습니다.</td></tr><tr><td><code>ChunklistMaxAge</code></td><td>Media Playlist의 max-age를 지정합니다. 이것은 항상 최신 상태의 Media Playlist를 응답해야 하는 URL이며 LL-HLS의 Delivery Directives가 없습니다. 따라서 이것은 가능한 짧게 Cache하거나 또는 Cache하지 않아야 합니다.</td></tr><tr><td><code>ChunklistWithDirectivesMaxAge</code></td><td><a href="https://developer.apple.com/documentation/http-live-streaming/enabling-low-latency-http-live-streaming-hls#Add-Low-Latency-HLS-Delivery-Directives">Low-Latency HLS Delivery Directives</a>가 포함 된 Media Playlist의 max-age를 지정합니다. 현재 지원되는 Delivery Directives는 <code>_HLS_msn</code>과 <code>_HLS_part</code>입니다. 이것은 Segment 번호와 Partial Segment 번호가 지정된 Playlist이며, 이 URL은 Stream이 생성되어 있는 동안 유일하며, 같은 URL의 콘텐츠는 변경되지 않기 때문에 오랜 시간 Cache할 수 있습니다.</td></tr><tr><td><code>SegmentMaxAge</code></td><td>Segment File의 max-age를 지정합니다. Segment File의 URL은 Stream이 생성되어 있는 동안 유일하며, 같은 URL의 콘텐츠는 변경되지 않기 때문에 오랜 시간 Cache할 수 있습니다.</td></tr><tr><td><code>PartialSegmentMaxAge</code></td><td>Partial Segment File의 max-age를 지정합니다. Partial Segment File의 URL은 Stream이 생성되어 있는 동안 유일하며, 같은 URL의 콘텐츠는 변경되지 않기 때문에 오랜 시간 Cache할 수 있습니다.</td></tr></tbody></table>

#### `<CacheControl>` Element의 의미

<table><thead><tr><th width="335">Element</th><th>Description</th></tr></thead><tbody><tr><td>0</td><td><p><code>CacheControl: no-cache, no-store</code><br></p><p>콘텐츠를 절대 캐시하지 말 것을 지시하여 매 요청마다 새로운 데이터를 Server에서 받아옵니다.</p></td></tr><tr><td>양수 (0 초과)</td><td><p><code>CacheControl: max-age=&#x3C;seconds></code><br></p><p>지정된 시간 동안 Cache가 유효하도록 지시합니다.</p></td></tr><tr><td>-1</td><td><p><code>CacheControl</code> 헤더 미포함</p><p></p><p>Origin에서 명시적인 Cache Header를 보내지 않도록 지시합니다. 이 값을 통해 Cache를 사용하지 않거나, CDN이나 Cache 서버의 자체 정책을 적용하거나, Client나 Browser의 기본 Cache 동작을 따를 수 있습니다.</p></td></tr></tbody></table>

`<CacheControl>`에 0 값 사용 시 서버 부하가 증가할 수 있고, -1 값 사용 시 예상치 못한 캐싱 동작이 발생할 수 있으므로 테스트가 필요합니다.

## Cache 유효성 검사

이 기능을 활성화 하면 캐시된 리소스 버전이 OvenMediaEngine의 리소스와 동일한지 여부를 식별 할 수 있습니다. HTTP 요청에 `If-None-Match: "<etag_value>"` 헤더가 포함되어 있을 때 OvenMediaEngine의 리소스 Etag 와 비교하여 값이 일치 할 경우 `304 Not-Modified`를 반환합니다.

다음과 같이 `Server.xml`의 `<Modules><ETag>`에서 활성화 할 수 있습니다.

```xml
<Server>
  <Modules>
    <ETag>
      <Enable>true</Enable>
    </ETag>
  </Modules>
</Server>
```

{% hint style="info" %}
활성화 시 모든 HTTP 응답에 Etag 헤더가 추가됩니다.
{% endhint %}
