Beispiel #1
0
def request_is_limited(red: Redis, redis_key: str, redis_limit: int, redis_period: timedelta):
    if red.setnx(redis_key, redis_limit):
        red.expire(redis_key, int(redis_period.total_seconds()))
    bucket_val = red.get(redis_key)
    if bucket_val and int(bucket_val) > 0:
        red.decrby(redis_key, 1)
        return False
    return True
def request_is_limited(r: Redis, key: str, limit: int, period: timedelta):
    if r.setnx(key, limit):
        r.expire(key, int(period.total_seconds()))
    bucket_val = r.get(key)
    if bucket_val and int(bucket_val) > 0:
        r.decrby(key, 1)
        return False
    return True
def blocked_request(r: Redis, username: str, limit: int, period: timedelta):
    if r.setnx(username, limit):
        r.expire(username, int(period.total_seconds()))

    value = r.get(username)

    if value and int(value) > 0:
        r.decrby(username, 1)
        return False
    return True
Beispiel #4
0
    print DIVIDING

    # 键均不存在时才批量赋值
    mapping = dict(uxs1='vs1', uxs2='vs2')
    print rc.msetnx(mapping)
    print rc.mget('uxs1', 'uxs2')
    print DIVIDING

    # 键为name的value增值操作,默认为1,键不存在则被创建并设为amount
    rc.set('num', 1)
    print rc.incr('num', amount=2)
    print rc.incrby('num', amount=3)

    # 键为name的value减值操作,默认为1,键不存在则被创建并设为-amount
    print rc.decr('num', amount=3)
    print rc.decrby('num', amount=3)
    print DIVIDING

    # 键为name的string的值追加value
    rc.set('append', 'asd')
    print rc.append('append', 'asdasdasd')
    print rc.get('append')
    print DIVIDING

    # 返回键为name的string的子串
    print rc.substr('append', 0, 5)

    # 获取键的value值从start到end的子字符串
    print rc.getrange('append', 0, 5)

    rc.flushdb()