示例#1
0
def _do(p: redis.client.Pipeline, number):
    p.watch(number)
    if p.get(number):
        raise AlreadyExistException('Number already exists')
    elif p.get(number + 1):
        raise OneLessThatItWasException('Number is 1 less than required')
    p.execute()
    p.unwatch()
    p.set(number, 'True')
    p.execute()
示例#2
0
文件: cache.py 项目: talkenson/ForcAD
def cache_helper(
        pipeline: redis.client.Pipeline,
        cache_key: str,
        cache_func: Callable[..., Any],
        cache_args: ArgsType = None,
        cache_kwargs: KwargsType = None,
):
    if cache_args is None:
        cache_args = tuple()
    if cache_kwargs is None:
        cache_kwargs = dict()

    # FIXME: there's a possible race condition in caching
    # if cache is reset at the moment the round is updated,
    # we could override the correct cache state with the state
    # of the previous round if caching is started earlier

    was_changed = False
    while True:
        try:
            pipeline.watch(cache_key)
            cached = pipeline.exists(cache_key)

            pipeline.multi()

            if not cached:
                was_changed = True
                cache_func(*cache_args, **cache_kwargs)

            pipeline.execute()
        except redis.WatchError:
            time.sleep(0.05)
        else:
            break

    return was_changed
示例#3
0
def transaction_func(pipeline: redis.client.Pipeline) -> None:
    pipeline.multi()
    pipeline.set('my_pet', 'cat')
    pipeline.set('my_pet', 'tiger')
    pipeline.execute()