import hashlib


def get_cache_key(*args, **kwargs):
    """
    Get MD5 encoded cache key for given arguments.

    Note:
        We convert arguments and keyword arguments to their string form to build the cache key. So do not pass
        arguments that can't be converted to strings.  Also, don't pass arguments that may not consistently
        be converted to the same string, like an unsorted dict.
        Here is the format of key before MD5 encryption `key1:value1__key2:value2`

    Example:
        >>> get_cache_key(site_domain="example.com", resource="catalogs")
        # Here is key format for above call
        # "site_domain:example.com__resource:catalogs"
        a54349175618ff1659dee0978e3149ca

    Arguments:
        *args: Positional arguments that need to be present in the cache key
        **kwargs: Key word arguments that need to be present in the cache key.

    Returns:
         An MD5 encoded key uniquely identified by the key word arguments.
    """
    key = '__'.join(args)
    key += '__'.join([u'{}:{}'.format(k, v) for k, v in sorted(kwargs.items())])

    return hashlib.md5(key.encode('utf-8')).hexdigest()