(request_ip: str, queue_key: int, amount: int)
return allowed
If the user can not vote any more for the song into the given direction, return False.
    Otherwise, perform the vote and returns True.
If the user can not vote any more for the song into the given direction, return False.
    Otherwise, perform the vote and returns True.
If the user can not vote any more for the song into the given direction return False . Otherwise perform the vote and returns True .
def try_vote(request_ip: str, queue_key: int, amount: int) -> bool:
    """If the user can not vote any more for the song into the given direction, return False.
    Otherwise, perform the vote and returns True."""
    # Votes are stored as individual (who, what) tuples in redis.
    # A mapping who -> [what, ...] is not used,
    # because each modification would require deserialization and subsequent serialization.
    # Without such a mapping we cannot easily find all votes belonging to a session key,
    # which would be required to update the view of a user whose client-votes got desynced.
    # This should never happen during normal usage, so we optimize for our main use case:
    # looking up whether a single user voted for a single song, which is constant with tuples.
    entry = str((request_ip, queue_key))
    allowed = True

    # redis transaction: https://github.com/Redis/redis-py#pipelines
    def check_entry(pipe) -> None:
        nonlocal allowed
        vote = pipe.get(entry)
        if vote is None:
            new_vote = amount
        else:
            new_vote = int(vote) + amount
        if new_vote < -1 or new_vote > 1:
            allowed = False
            return
        allowed = True
        # expire these entries to avoid accumulation over long runtimes.
        pipe.multi()
        pipe.set(entry, new_vote, ex=24 * 60 * 60)

    redis.transaction(check_entry, entry)
    return allowed
def try_vote ( request_ip : str , queue_key : int , amount : int ) -> bool : # Votes are stored as individual (who, what) tuples in redis. # A mapping who -> [what, ...] is not used, # because each modification would require deserialization and subsequent serialization. # Without such a mapping we cannot easily find all votes belonging to a session key, # which would be required to update the view of a user whose client-votes got desynced. # This should never happen during normal usage, so we optimize for our main use case: # looking up whether a single user voted for a single song, which is constant with tuples. entry = str ( ( request_ip , queue_key ) ) allowed = True # redis transaction: https://github.com/Redis/redis-py#pipelines def check_entry ( pipe ) -> None : nonlocal allowed vote = pipe . get ( entry ) if vote is None : new_vote = amount else : new_vote = int ( vote ) + amount if new_vote < - 1 or new_vote > 1 : allowed = False return allowed = True # expire these entries to avoid accumulation over long runtimes. pipe . multi ( ) pipe . set ( entry , new_vote , ex = 24 * 60 * 60 ) redis . transaction ( check_entry , entry ) return allowed