Exemplo n.º 1
0
    def get(self, request, redis_name, value_db_id, key):
        from public.redis_api import get_cl
        from public.data_view import get_value
        logs.info('get value: redis_name={0}, db={1}, key={2}'.format(
            redis_name, value_db_id, key))
        cl = get_cl(redis_name, int(value_db_id))
        value_dict = {'code': 0, 'msg': '', 'data': ''}
        if cl.exists(key):
            value = ''
            if request.GET.get("type", None) == 'ttl':
                value = cl.ttl(key)
                if value is None:
                    value = -1
                logs.info(
                    'get key ttl: redis_name={0}, db={1}, key={2}, ttl={3}'.
                    format(redis_name, value_db_id, key, value))
            else:
                try:
                    value = get_value(key, int(value_db_id), cl)
                except Exception as e:
                    logs.error(
                        'get value is error: redis_name:{0}, key:{1}, db:{2}, cl:{3}, error_info:{4}'
                        .format(redis_name, key, value_db_id, cl, e))
                    value_dict['code'] = 1
            value_dict['data'] = value
        else:
            logs.warning(
                'key is not exists: redis_name={0}, db={1}, key={2}'.format(
                    redis_name, value_db_id, key))
            value_dict['code'] = 1

        return JsonResponse(value_dict, safe=False)
Exemplo n.º 2
0
def get_all_keys_tree(client=None, key='*', cursor=0, min_num=None, max_num=None):
    client = client or get_client()
    key = key or '*'
    if key == '*':
        data = client.scan(cursor=cursor, match=key, count=scan_batch)
    else:
        if key[0] == '$' :
            cmdList=key[1:].split();
            cmdList[0]=cmdList[0].upper();
            logs.info('execute: cmd={0}'.format(" ".join(cmdList)))
            try:
                res=client.execute_command(*cmdList);
            except Exception as e:
                res=str(e)
            logs.info('execute response={0}'.format(res))
            if isinstance(res,basestring) :
                res=res.replace("\r\n","\n").strip("\n").split("\n");
            resinfo={'cmd':" ".join(cmdList),'response':res};#{'$response':res}
            data_list = list()
            data_list.append(json.dumps(resinfo));
            return data_list[min_num:max_num];
        else:
            key = '*%s*' % key
            data = client.scan(cursor=cursor, match=key, count=scan_batch)
    if isinstance(data, tuple):
        data = data[1]
    elif isinstance(data, dict):
        data_list = list()
        for k, v in data.items():
            data_list.extend(v[1])
        data = data_list
    data = data[min_num:max_num]
    data.sort()
    return data
Exemplo n.º 3
0
 def post(self, request, redis_name, value_db_id, key):
     """
     修改TTL
     """
     from public.redis_api import get_cl
     cl = get_cl(redis_name, int(value_db_id))
     value_dict = {'code': 0, 'msg': '', 'data': ''}
     ttl = request.POST.get("ttl", None)
     if cl.exists(key) and ttl:
         try:
             cl.expire(key, ttl)
             logs.info(
                 'change key tll: redis_name={0}, db={1}, key={2}, ttl={3}'.
                 format(redis_name, value_db_id, key, ttl))
             value_dict['msg'] = "修改成功"
         except Exception as e:
             logs.error(e)
             value_dict['msg'] = '修改失败,请联系管理员'
     return JsonResponse(value_dict)
Exemplo n.º 4
0
def check_redis_connect(name):
    redis_conf = get_redis_conf(name)
    try:
        logs.info(
            "host:{0},port:{1},password:{2},timeout:{3}, socket: {4}".format(
                redis_conf.host, redis_conf.port, redis_conf.password,
                socket_timeout, redis_conf.socket))
        if redis_conf.socket is not None and redis_conf.socket != "":
            redisconn = redis.Redis(host=redis_conf.host,
                                    port=redis_conf.port,
                                    password=redis_conf.password,
                                    unix_socket_path=redis_conf.socket)
        else:
            redisconn = redis.Redis(host=redis_conf.host,
                                    port=redis_conf.port,
                                    password=redis_conf.password)
        return redisconn.ping()
    except Exception as e:
        logs.error(e)
        error = dict(
            redis=redis_conf,
            message=e,
        )
        return error
Exemplo n.º 5
0
    def get(self, request, redis_name, value_db_id):
        from public.redis_api import get_cl
        from public.data_view import get_value
        key = request.GET.get('key', None)
        logs.info('get value: redis_name={0}, db={1}, key={2}'.format(
            redis_name, value_db_id, key))
        cl = get_cl(redis_name, int(value_db_id))
        value_dict = {'code': 0, 'msg': '', 'data': ''}
        keyobj = None
        isCmd = False
        try:
            keyobj = json.loads(key)
            if keyobj.has_key('cmd'):
                isCmd = True
        except Exception as e:
            pass
        finally:
            pass
        if isCmd:
            cmdList = keyobj.get('cmd').split()
            cmdList[0] = cmdList[0].upper()
            logs.info('execute: cmd={0}'.format(" ".join(cmdList)))
            try:
                res = cl.execute_command(*cmdList)
            except Exception as e:
                res = str(e)
            logs.info('execute response={0}'.format(res))
            if isinstance(res, basestring):
                res = res.replace("\r\n", "\n").strip("\n").split("\n")
            resinfo = {
                "db": int(value_db_id),
                "key": " ".join(cmdList),
                "value": json.dumps(res),
                "ttl": -1,
                "type": "string"
            }
            #{'$response':res}
            #{"encoding":"","db":0,"value":"aa","key":"a a","ttl":-1,"type":"string","size":2}
            value_dict['data'] = resinfo
            return JsonResponse(value_dict, safe=False)
        if cl.exists(key):
            value = {}
            if request.GET.get("type", None) == 'ttl':
                ttl = cl.ttl(key)
                if ttl is None:
                    ttl = -1
                logs.info(
                    'get key ttl: redis_name={0}, db={1}, key={2}, ttl={3}'.
                    format(redis_name, value_db_id, key, ttl))
                value = {
                    "db": value_db_id,
                    "key": key,
                    "value": ttl,
                    "ttl": ttl,
                    "type": "long"
                }
            else:
                try:
                    value = get_value(key, int(value_db_id), cl)
                except Exception as e:
                    logs.error(
                        'get value is error: redis_name:{0}, key:{1}, db:{2}, cl:{3}, error_info:{4}'
                        .format(redis_name, key, value_db_id, cl, e))
                    value_dict['code'] = 1
            if value.has_key('value'):
                value['value'] = json.dumps(value.get('value'))
            value_dict['data'] = value
        else:
            logs.warning(
                'key is not exists: redis_name={0}, db={1}, key={2}'.format(
                    redis_name, value_db_id, key))
            value_dict['code'] = 1

        return JsonResponse(value_dict, safe=False)