"""Cache helper functions.""" import logging from typing import List, Tuple logger = logging.getLogger(__name__) DELIMITER_PREFIX = "@" DELIMITER_ITEM = "|" DELIMITER_KEY_VAL = "#" def make_complicated_cache_key( prefix: str, key_vals: List[Tuple[str, str]], ) -> str: """Return a cache key delimiting multiple attribute/value pairs. For example: make_complicated_cache_key( prefix="tenant_hierarchy", key_vals=[ ("tenant_id", "123"), ("tenant_type", "account"), ("tenant_uuid", "uuid1"), ], ) returns: tenant_hierarchy@tenant_id#123|tenant_type#account|tenant_uuid#uuid1 """ if not key_vals: raise ValueError("key_vals cannot be empty") key_val_joined = [DELIMITER_KEY_VAL.join(kv) for kv in key_vals] return f"{prefix}{DELIMITER_PREFIX}{DELIMITER_ITEM.join(key_val_joined)}"