# How to enforce HLS cache for live streaming delivery

import DocButton from '~/components/webkit/DocButton.vue';
import Tabs from '~/components/tabs/Tabs'
import Code from '~/components/Code/Code.astro'

Azion Web Platform allows you to [deliver live streaming content in HLS format](https://www.azion.com/en/solutions/live-streaming-delivery/), being able to easily self-provision and configure your cache policies. This guide covers the step-by-step to enforce HLS cache, manage chunks and playlists caching, set up rules engines rules, and utilize Azion Object Storage for optimal video delivery.

---

## Requirements

- An existing Azion application, or [create a new one](/en/documentation/products/guides/build/build-an-application/).
- [A domain associated with the application](/en/documentation/products/guides/configure-a-domain/).
- [Tiered Cache enabled](/en/documentation/products/guides/billing-and-subscriptions/) in your account.
- Access to [Azion Object Storage](/en/documentation/products/store/object-storage/) for storing video content.

---

## Enforcing cache policies for HLS

To enforce cache policies for HLS in the application, follow the steps as explained next.

In this example, an application and a domain linked to it has been previously created.

### Creating a cache policy for the chunks

First, you must create a cache policy for the chunks:

<Tabs client:visible>
    <Fragment slot="tab.cli">CLI </Fragment>
    <Fragment slot="tab.console">Console </Fragment>

<Fragment slot="panel.cli">
1. Open [Azion CLI](/en/documentation/products/azion-cli/overview/) in your terminal.
2. Get the details of an existing application using the [list command](/en/documentation/devtools/cli/list/): `azion list edge-application --details`
3. Enable Tiered Cache for your application: `$ azion update edge-application --application-id 1234 --l2-caching true`
4. Create an origin: `$ azion create origin --application-id 1234 --name "origin-edge" --origin-type single_origin --addresses "example.com" --host-header "example.com"`
5. Configure the cache policy for the chunks: `$ azion create cache-setting --application-id 1234 --name "chunks-policy" --browser-cache-settings "override" --browser-cache-settings-maximum-ttl 0 --cdn-cache-settings "override" --cnd-cache-settings-maximum-ttl 60`
- Define **Advanced Cache Key** behaviors according to your needs. Check the optional flags for the [create command](/en/documentation/devtools/cli/create/#optional-flags-3).
- The recommendation is selecting **Content does not vary**, for **Query String** and/or **Cookies**, according to your needs.
</Fragment>

<Fragment slot="panel.console">
1. Access [Azion Console](https://console.azion.com).
2. Go to **Products menu** > **Applications**.
3. Select your application.
4. Open the **Cache Settings** tab and click the **+ Cache Setting** button to create the new cache policy:
- Give it a unique name.
- In **Cache Expiration Policies**:
    - For **Browser Cache Settings**, select **Override Cache Setting** and define a **Maximum TTL** of `0`.
    - For **Cache Settings**, select **Override Cache Setting** and define a **Maximum TTL** of `60`.
- Enable the **Tiered Cache** switch.
- In the **Advanced Cache Key** section, define the behavior of your application toward cache segmentation of objects.
    - The recommendation is selecting **Content does not vary**, for **Query String** and/or **Cookies**, according to your needs.
5. Click the **Save** button.
</Fragment>
</Tabs>
### Creating cache policies for the playlist

<Tabs client:visible>
    <Fragment slot="tab.cli">CLI </Fragment>
    <Fragment slot="tab.console">Console </Fragment>

<Fragment slot="panel.cli">
Now configure the cache policy for the playlist: `$ azion create cache-setting --application-id 1234 --name "playlist-policy" --browser-cache-settings "override" --browser-cache-settings-maximum-ttl 0 --cdn-cache-settings "override" --cnd-cache-settings-maximum-ttl 5`
- Define **Advanced Cache Key** behaviors according to your needs. Check the optional flags for the [create command](/en/documentation/devtools/cli/create/#optional-flags-3).
- The recommendation is selecting **Content does not vary**, for **Query String** and/or **Cookies**, according to your needs.
</Fragment>

<Fragment slot="panel.console">
Still on the **Cache Settings** tab:

1. Click the **+ Cache Setting** button again to create a new cache policy for the playlist:
- For **Browser Cache Settings**, select **Override Cache Setting** and define a **Maximum TTL** of `0`.
- For **Cache Settings**, select **Override Cache Setting** and define a **Maximum TTL** of `5`.
- Enable the **Tiered Cache** switch.
- In the **Advanced Cache Key** section, define the behavior of your application toward cache segmentation of objects.
    - The recommendation is selecting **Content does not vary**, for **Query String** and/or **Cookies**, according to your needs.
2. Click the **Save** button.
</Fragment>

</Tabs>

:::tip
Read more about setting up [cache settings](/en/documentation/products/build/applications/cache-settings/) for your applications.
:::

### Utilizing Azion Object Storage for HLS video delivery

To deliver MP4 videos using HLS and Azion Object Storage:

1. Upload your MP4 video files to Azion Object Storage.
2. Configure your encoder to convert MP4 files to HLS format and point to Azion Object Storage as the origin.
3. Follow the cache policy setup steps outlined above to ensure efficient video delivery.

### Creating Rules Engine rules

<Tabs client:visible>
    <Fragment slot="tab.cli">CLI </Fragment>
    <Fragment slot="tab.console">Console </Fragment>

<Fragment slot="panel.cli">
1. First, create a rule for the chunks: 
`$ azion create rules-engine --application-id 1234 --phase "request" --file ./chunks-rule.json`

In the `chunks-rule.json` file, include:

<Code lang="json" code={`
{
    "name": "chunks-rule",
    "description": "This is a description for your chunks rule",
    "criteria": [
        [
            {
                "conditional": "if",
                "variable": "\${uri}",
                "operator": "matches",
                "input_value": ".\*.ts"
            }
        ]
    ],
    "behaviors": [
        {
            "name": "set_cache_policy",
            "target": "chunks-policy"
        }
    ]
}
`} />

2. Now, create a rule for the playlist:
`$ azion create rules-engine --application-id 1234 --phase "request" --file ./playlist-rule.json`

In the `playlist-rule.json` file, include:

```json
{
    "name": "playlist-cache-rule",
    "description": "This is a description for your playlist rule",
    "criteria": [
        [
            {
                "conditional": "if",
                "variable": "${uri}",
                "operator": "matches",
                "input_value": "\*.m3u8"
            }
        ]
    ],
    "behaviors": [
        {
            "name": "set_cache_policy",
            "target": "playlist-policy"
        }
    ]
}
```
</Fragment>

<Fragment slot="panel.console">
Still on the application page, open the **Rules Engine** tab:

First, create a rule for the chunks: 
1. Click the **+ Rule** button.
2. Give a name and a description (optional) to your rule.
3. Select the **Request Phase** option.
4. In **Criteria**, define `if ${uri}` *matches* `.\*.ts`.
5. In **Behaviors**, select **Set Cache Policy** and add the policy for the chunks you created in the previous step.
6. Click the **Save** button.

Now, create a rule for the playlist: 
1. Click the **+ Rule** button.
2. Give a name and a description (optional) to your rule.
3. Select the **Request Phase** option.
4. In **Criteria**, define `if ${uri}` *matches* `\*.m3u8`.
5. In **Behaviors**, select **Set Cache Policy** and add the policy for the playlist you created in the previous step.
6. Click the **Save** button.
</Fragment>

</Tabs>

Done. Now you can configure your source and encoder pointing to Azion and stream your content, enforcing HLS cache.

:::tip
Go to the [Rules Engine for Applications](/en/documentation/products/build/applications/rules-engine/) documentation for more details.
:::

<DocButton href="/en/documentation/architectures/live-streaming-delivery/live-streaming-delivery-with-hls/" label="Go to Live Streaming Delivery architecture" kind="secondary" size="medium" />