Exemplo n.º 1
0
def key_set():
    redis_config = ['key', 'value']
    if not request.json:
        return vo.fail(406)
    for one in redis_config:
        if one not in request.json or request.json[one] == '':
            return vo.fail(406)
    redis = mainConfig.getRedis()
    redis.set(request.json['key'], request.json['value'])
    return vo.success()
Exemplo n.º 2
0
def key_get(key):
    redis = mainConfig.getRedis()
    print('key get key=', key, 'redis=', redis)
    re = redis.type(key)
    if re == b'string':
        result = redis.get(key)
        return vo.single(result.decode())
    elif re == b'none':
        return vo.fail(404)
    else:
        return vo.fail(405)
Exemplo n.º 3
0
def list_keys():
    """ 列出 所有键"""
    redis = mainConfig.getRedis()
    all_key = redis.keys()
    result = []
    for one in all_key:
        result.append(one.decode())
    return vo.multiple(result)
Exemplo n.º 4
0
def list_keys_by_len(length):
    """列出所有, 按键的长度"""
    redis = mainConfig.getRedis()
    key_list = redis.keys('?' * length)
    result = []
    for one in key_list:
        result.append(one.decode())
    return vo.multiple(result)
Exemplo n.º 5
0
def show_code():
    result_code = {
        '404': "资源找不到",
        '405': "键类型错误",
        '406': "参数缺失",
        '407': "redis连接未初始化",
        '408': "redis连接失败"
    }
    return vo.multiple(result_code)
Exemplo n.º 6
0
def init_redis():
    config_list = ['host', 'port', 'password', 'db']
    if not request.json:
        return vo.fail(406)
    for one in config_list:
        if one not in request.json:
            return vo.fail(406)
    global redis
    redis = RedisConfig(request.json['host'], request.json['port'],
                        request.json['password'],
                        request.json['db']).getConnection()
    try:
        redis.ping()
    except Exception as e:
        print('Failed to connect', e)
        redis = None
        mainConfig.setRedis(None)
        return vo.fail(408)
    print('Successfully initialized Redis ', redis)
    mainConfig.setRedis(redis)
    return vo.success()
Exemplo n.º 7
0
def check():
    global redis
    path = request.path
    print('listen : ', path)
    # 如果路径中包含以下, 就全部忽略校验
    ignore = ['init_redis', 'show_code', '/static/']
    isIgnore = False
    for one in ignore:
        if one in path:
            isIgnore = True
            break
        if path == '/':
            isIgnore = True
            break
    if redis is None and not isIgnore:
        return vo.fail(407)
    else:
        redirect(path)
Exemplo n.º 8
0
def get_most_key_with_num(length=7, offset=0, top=5):
    """组装前端需要的数据结构"""
    result, days = most_key(length, offset, top)
    lines = []
    for key in result:
        data = []
        for day in days:
            score = redis.zscore(day, key)
            if score is None:
                data.append(0)
            else:
                data.append(int(score))
        key_name = redis.hget('key_map', key)
        if key_name is None:
            continue
        line = Line(key_name.decode(), data).to_json_self()
        lines.append(line)
    return vo.multiple(lines)
Exemplo n.º 9
0
def get_most_key(length=7, offset=0, top=5):
    result = most_key(length, offset, top)[0]
    return vo.multiple(most_key_name(result))
Exemplo n.º 10
0
def get_recent_day(length=7, offset=0):
    days = period_key_with_total(length, offset)
    return vo.multiple(days)