import hashlib


def _compute_hash_of_file(path, _last_modified=None, algorithm="sha256"):
    """Compute the hash of a file.

    The function uses caching to avoid computing the same hash twice if the same file is
    requested and has not been modified in the meantime.

    Taken from https://stackoverflow.com/a/44873382/7523785.

    See Also
    --------
    _load_hashes_helper

    """
    h = hashlib.new(algorithm)

    byte_array = bytearray(128 * 1024)
    memory_view = memoryview(byte_array)
    with open(path, "rb", buffering=0) as f:
        for n in iter(lambda: f.readinto(memory_view), 0):
            h.update(memory_view[:n])

    return h.hexdigest()