def pop_stream(cache, user_id):
    """
    Pop an item off the stack in the cache. If stack
    is empty after pop, it deletes the stack.

    :param cache: werkzeug BasicCache-like object
    :param user_id: id of user, used as key in cache

    :return: top item from stack, otherwise None
    """
    stack = cache.get(user_id)
    if stack is None:
        return None

    result = stack.pop()

    if len(stack) == 0:
        cache.delete(user_id)
    else:
        cache.set(user_id, stack)

    return result