# CLIENT CACHING

> Choose whether the keys read by the next command are tracked, for connections in OPTIN or OPTOUT tracking mode.

URL: https://docs.hotkv.com/commands/client-caching/

```
CLIENT CACHING YES | NO
```

- Group: Connection
- Complexity: O(1)
- HotKV Cloud: all plans
- On-Premise: no license needed

With [CLIENT TRACKING](https://docs.hotkv.com/commands/client-tracking/index.md) in `OPTIN` or `OPTOUT` mode,
the connection decides command by command which keys are tracked. `CLIENT
CACHING YES` turns tracking on for the keys of the next command, in `OPTIN` mode,
where nothing is tracked otherwise. `CLIENT CACHING NO` leaves the keys of the
next command out, in `OPTOUT` mode, where everything is tracked otherwise.

The choice covers the next command only. When it is sent before
[MULTI](https://docs.hotkv.com/commands/multi/index.md), it covers the commands queued up to `EXEC`. It can also
be queued inside the transaction.

## Return value

Simple string `OK`, in RESP2 and RESP3.

Errors:

| Error | Cause |
| --- | --- |
| `ERR CLIENT CACHING can be called only when the client is in tracking mode with OPTIN or OPTOUT mode enabled` | Tracking is off. |
| `ERR CLIENT CACHING YES is only valid when tracking is enabled in OPTIN mode.` | `YES`, with tracking on in another mode. |
| `ERR CLIENT CACHING NO is only valid when tracking is enabled in OPTOUT mode.` | `NO`, with tracking on in another mode. |
| `ERR syntax error` | The argument is neither `YES` nor `NO`. |
| `ERR wrong number of arguments for 'client\|caching' command` | The argument is missing or there are more than one. |

## Examples

```
127.0.0.1:6379> CLIENT CACHING YES
(error) ERR CLIENT CACHING can be called only when the client is in tracking mode with OPTIN or OPTOUT mode enabled
127.0.0.1:6379> CLIENT TRACKING ON OPTIN
OK
127.0.0.1:6379> CLIENT CACHING NO
(error) ERR CLIENT CACHING NO is only valid when tracking is enabled in OPTOUT mode.
127.0.0.1:6379> CLIENT CACHING YES
OK
127.0.0.1:6379> CLIENT TRACKINGINFO
1) "flags"
2) 1) "on"
   2) "optin"
   3) "caching-yes"
3) "redirect"
4) (integer) 0
5) "prefixes"
6) (empty array)
```

`caching-yes` (or `caching-no` in `OPTOUT` mode) shows in
[CLIENT TRACKINGINFO](https://docs.hotkv.com/commands/client-trackinginfo/index.md) until the next command has
run.

## Compatibility

The replies and error texts follow Redis 8 and Valkey 9.

## See also

[CLIENT TRACKING](https://docs.hotkv.com/commands/client-tracking/index.md),
[CLIENT TRACKINGINFO](https://docs.hotkv.com/commands/client-trackinginfo/index.md),
[Client-side caching](https://docs.hotkv.com/develop/client-side-caching/index.md#optin-and-optout)