Skip to content

utils

dandy.cache.utils

generate_cache_key

Source code in dandy/cache/utils.py
def generate_cache_key(func: object, *args, **kwargs) -> str:
    # Todo: Why are the args all over the map
    # hashable_args = tuple([convert_to_hashable_str(arg) for arg in args])

    hashable_kwargs = tuple(
        sorted(
            (key, convert_to_hashable_str(value)) for key, value in kwargs.items()
        )
    )

    hashable_tuple = (
        func.__module__,
        func.__qualname__,
        # Todo: Why are the args all over the map
        # hashable_args,
        hashable_kwargs,
    )

    hash_key = hashlib.sha256(
        str(hashable_tuple).encode()
    ).hexdigest()

    return hash_key

convert_to_hashable_str

Source code in dandy/cache/utils.py
def convert_to_hashable_str(obj: Any) -> str:
    if isinstance(obj, type):
        if issubclass(obj, BaseModel):
            return str(obj.model_json_schema())
        else:
            return str(obj)
    elif isinstance(obj, BaseModel):
        return str(obj)
    elif hasattr(obj, '__dict__'):
        return f'{obj.__dict__}'
    elif hasattr(obj, '__str__'):
        return str(obj)
    else:
        raise CacheCriticalException(f'Object "{obj}" is not hashable')