| 1 | --- |
| 2 | title: Caching in ANTA |
| 3 | hide: |
| 4 | - tags |
| 5 | tags: |
| 6 | - Python |
| 7 | - Inventory |
| 8 | --- |
| 9 | |
| 10 | <!-- |
| 11 | ~ Copyright (c) 2023-2026 Arista Networks, Inc. |
| 12 | ~ Use of this source code is governed by the Apache License 2.0 |
| 13 | ~ that can be found in the LICENSE file. |
| 14 | --> |
| 15 | |
| 16 | ANTA is a streamlined Python framework designed for efficient interaction with network devices. This section outlines how ANTA incorporates caching mechanisms to collect command outputs from network devices. |
| 17 | |
| 18 | ## Configuration |
| 19 | |
| 20 | The `_init_cache()` method of the [AntaDevice](../api/device.md#anta.device.AntaDevice) abstract class initializes the cache. Child classes can override this method to tweak the cache configuration: |
| 21 | |
| 22 | ## Cache key design |
| 23 | |
| 24 | The cache is initialized per `AntaDevice` and uses the following cache key design: |
| 25 | |
| 26 | `<device_name>:<uid>` |
| 27 | |
| 28 | The `uid` is an attribute of [AntaCommand](../api/commands.md#anta.models.AntaCommand), which is a unique identifier generated from the command, version, revision and output format. |
| 29 | |
| 30 | Each UID has its own asyncio lock. This design allows coroutines that need to access the cache for different UIDs to do so concurrently. The locks are managed by the `AntaCache.locks` dictionary. |
| 31 | |
| 32 | ## Mechanisms |
| 33 | |
| 34 | By default, once the cache is initialized, it is used in the `collect()` method of `AntaDevice`. The `collect()` method prioritizes retrieving the output of the command from the cache. If the output is not in the cache, the private `_collect()` method will retrieve and then store it for future access. |
| 35 | |
| 36 | ## How to disable caching |
| 37 | |
| 38 | Caching is enabled by default in ANTA following the previous configuration and mechanisms. |
| 39 | |
| 40 | There might be scenarios where caching is not wanted. You can disable caching in multiple ways in ANTA: |
| 41 | |
| 42 | 1. Caching can be disabled globally, for **ALL** commands on **ALL** devices, using the `--disable-cache` global flag when invoking anta at the [CLI](../cli/overview.md#invoking-anta-cli): |
| 43 | |
| 44 | ```bash |
| 45 | anta --disable-cache --username arista --password arista nrfu table |
| 46 | ``` |
| 47 | |
| 48 | 2. Caching can be disabled per device, network or range by setting the `disable_cache` key to `True` when defining the ANTA [Inventory](../usage-inventory-catalog.md#device-inventory) file: |
| 49 | |
| 50 | ```yaml |
| 51 | anta_inventory: |
| 52 | hosts: |
| 53 | - host: 172.20.20.101 |
| 54 | name: DC1-SPINE1 |
| 55 | tags: ["SPINE", "DC1"] |
| 56 | disable_cache: True # Set this key to True |
| 57 | - host: 172.20.20.102 |
| 58 | name: DC1-SPINE2 |
| 59 | tags: ["SPINE", "DC1"] |
| 60 | disable_cache: False # Optional since it's the default |
| 61 | |
| 62 | networks: |
| 63 | - network: "172.21.21.0/24" |
| 64 | disable_cache: True |
| 65 | |
| 66 | ranges: |
| 67 | - start: 172.22.22.10 |
| 68 | end: 172.22.22.19 |
| 69 | disable_cache: True |
| 70 | ``` |
| 71 | |
| 72 | This approach effectively disables caching for **ALL** commands sent to devices targeted by the `disable_cache` key. |
| 73 | |
| 74 | 3. For tests developers, caching can be disabled for a specific [`AntaCommand`](../api/commands.md#anta.models.AntaCommand) or [`AntaTemplate`](../api/commands.md#anta.models.AntaTemplate) by setting the `use_cache` attribute to `False`. That means the command output will always be collected on the device and therefore, never use caching. |
| 75 | |
| 76 | By default, caching is enabled with `disable_cache=False` at the device level and `use_cache=True` at the command or template level. |
| 77 | |
| 78 | Run-wide and inventory cache settings are combined. The `--disable-cache` flag or `ANTA_DISABLE_CACHE` environment variable is a global control that disables caching for every device in the run. Inventory `disable_cache: true` is scoped to the matching host, network, or range only: |
| 79 | |
| 80 | ```python |
| 81 | effective_disable_cache = global_disable_cache or inventory_disable_cache |
| 82 | ``` |
| 83 | |
| 84 | Command-level `use_cache=False` only affects that command or template. It bypasses cache reads and writes without disabling the device cache. |
| 85 | |
| 86 | ### Disable caching in a child class of `AntaDevice` |
| 87 | |
| 88 | Since caching is implemented at the `AntaDevice` abstract class level, all subclasses will inherit that default behavior. As a result, if you need to disable caching in any custom implementation of `AntaDevice` outside of the ANTA framework, you must initialize `AntaDevice` with `disable_cache` set to `True`: |
| 89 | |
| 90 | ```python |
| 91 | class AnsibleEOSDevice(AntaDevice): |
| 92 | """ |
| 93 | Implementation of an AntaDevice using Ansible HttpApi plugin for EOS. |
| 94 | """ |
| 95 | def __init__(self, name: str, connection: ConnectionBase, tags: set = None) -> None: |
| 96 | super().__init__(name, tags, disable_cache=True) |
| 97 | ``` |