```python:inference/core/cache/air_gapped.py
import hashlib
import re
from functools import lru_cache

# Precompile regular expressions to avoid recompilation on every call.
_INVALID_RE = re.compile(r"[^A-Za-z0-9_-]+")
_SEQ_RE = re.compile(r"[_-]{2,}")

def _slugify_model_id(model_id: str) -> str:
    """Reproduce the slug used by inference-models for cache directory names.

    Must stay in sync with
    ``inference_models.models.auto_loaders.core.slugify_model_id_to_os_safe_format``.
    """
    # Use a tiny cache to speed up repeated calls with the same model_id.
    return _cached_slugify_model_id(model_id)


# Keep implementation in a separate function so we can cache results without
# changing the public function signature or behavior.
@lru_cache(maxsize=4096)
def _cached_slugify_model_id(model_id: str) -> str:
    slug = _INVALID_RE.sub("-", model_id)
    slug = _SEQ_RE.sub("-", slug)
    if not slug:
        slug = "special-char-only-model-id"
    if len(slug) > 48:
        slug = slug[:48]
    digest = hashlib.blake2s(model_id.encode("utf-8"), digest_size=4).hexdigest()
    return f"{slug}-{digest}"
```