# Cache Channels

Cache channels remember the latest server-triggered event and send it to new subscribers before future live events.

Use cache channels for "current state" surfaces such as locations, prices, counters, dashboards, and document metadata.

## Channel names

| Channel behavior | Prefix                     |
| ---------------- | -------------------------- |
| Public cache     | `cache-`                   |
| Private cache    | `private-cache-`           |
| Presence cache   | `presence-cache-`          |
| Encrypted cache  | `private-encrypted-cache-` |

## Subscribe

```js
const channel = pusher.subscribe('cache-location.42');

channel.bind('location.updated', (event) => {
    console.log(event);
});

channel.bind('pusher:cache_miss', () => {
    console.log('No cached event exists yet.');
});
```

Private, presence, and encrypted cache channels use the same auth flow as their non-cache equivalents.

## Populating the cache

Only server-triggered events populate cache channels.

```text
POST https://api.vask.dev/apps/{app_key}/events
```

```json
{
    "name": "location.updated",
    "channel": "cache-location.42",
    "data": "{\"lat\":51.5,\"lng\":-0.12}"
}
```

The next subscriber to `cache-location.42` receives that event after subscription succeeds.

## Cache miss handling

When a cache channel has no cached value, Vask can notify both client and server:

- Client: `pusher:cache_miss`
- Webhook: `cache_miss`

The usual pattern is:

1. Client subscribes to a cache channel.
2. Vask has no cached event.
3. Your backend receives a `cache_miss` webhook or your client reacts to `pusher:cache_miss`.
4. Your backend triggers a fresh event to the cache channel.

## Querying cache state

Use the HTTP API with `info=cache`.

```text
GET https://api.vask.dev/apps/{app_key}/channels/cache-location.42?info=cache
```

```json
{
    "occupied": true,
    "cache": {
        "event": "location.updated",
        "data": "{\"lat\":51.5,\"lng\":-0.12}",
        "ttl": 60
    }
}
```

If no cached value exists, `cache` is `null`.

## Related docs

- [Channels](/docs/channels)
- [HTTP API reference](/docs/http-api)
- [Webhooks](/docs/webhooks)
