# Configure cache and compatibility

Tune memory usage, enable unreleased `google/textfsm#151` behaviour, and run CLI template checks in CI.

## Size the compiled-template cache

`textfsm2` keeps compiled programs in a process-wide LRU cache. The default size (`128`) uses roughly 30 MB when full.

Increase the cache if your workload cycles through hundreds of templates (for example, parsing across all `ntc-templates` platforms):

```sh
export TEXTFSM_CACHE_SIZE=2048
```

Or adjust the limit and inspect hit/miss counts at runtime:

```python
from textfsm2 import _core

_core.set_cache_size(2048)
hits, misses, size, maxsize = _core.cache_info()
_core.cache_clear()
```

Disable caching when memory matters more than repeated template lookups:

```sh
export TEXTFSM_CACHE_SIZE=0
```

## Free warm search buffers after each parse

Each cached template retains a pool of warm regex scratch buffers (`1` per template by default, \~130 KB once used). Release buffers immediately after each parse by setting:

```sh
export TEXTFSM_SCRATCH_POOL=0
```

Negative values for `TEXTFSM_CACHE_SIZE` or `TEXTFSM_SCRATCH_POOL` raise `ValueError` when importing `textfsm2`.

## Opt in to `google/textfsm#151` behaviour

`textfsm2` defaults to **textfsm 2.1.1** behaviour for non-participating alternation groups (see [Compatibility](<https://ryanmerolle.github.io/textfsm2/explanation/compatibility/index.md>)). Opt in to upstream main branch behaviour per class/instance or process-wide:

```python
import textfsm2

textfsm2.TextFSM.SKIP_NONPARTICIPATING_GROUPS = True  # class, subclass, or instance
```

Or set the environment variable before starting Python:

```sh
export TEXTFSM_SKIP_NONPARTICIPATING_GROUPS=1
```

## Validate templates in CI from the CLI

Compare a template and sample input against a golden `FSM Table:` output file:

```sh
uvx textfsm2 template.textfsm input.txt expected_table.txt
```

Inside a `uv` project or installed environment:

```sh
uv run textfsm2 template.textfsm input.txt expected_table.txt
textfsm2 template.textfsm input.txt expected_table.txt
```

The command prints `Data match!` and exits `0` on success, prints `Data mis-match!` and exits `1` on mismatch, and exits `2` on invalid usage or template errors.
