Question

So I am working on a simple webserver based on Flask which stores and later displays some information I recieve with a post call.

anyway I the information I recieve is stored like this:

@app.route('client/<client_id>/', methods=['POST'])
def status(client_id):

    r_server=redis.Redis("127.0.0.1")

    jsonobj=request.data
    data=json.loads(jsonobj)

    result = {
        'client_id': client_id
        'current test': data[0]
        'status': data[1]
    }

    return

now what I want is save this into redis while keeping the client_id current test and status linked together.

is there an easy way to store it in redis and recieve it later and I can look op with client_id?

regards

Was it helpful?

Solution

If the info of a specific client_id is overwritable, you can use Redis Hashes (http://redis.io/commands/hmset). Example:

>>> r=redis.Redis('')
>>> r.hmset('ClientID:127.0.0.1', {'test':'1', 'status':'success'})
True
>>> r.hmget('ClientID:127.0.0.1',['test','status'])
['1', 'success']

If records must be appended, you can dumps each record as string, and save in a Redis List. Example:

>>> r.rpush('ClientID:127.0.0.1', json.dumps({'test':'1', 'status':'success'}))
1L
>>> r.rpush('ClientID:127.0.0.1', json.dumps({'test':'2', 'status':'failure'}))
2L
>>> [json.loads(x) for x in r.lrange('ClientID:127.0.0.1', 0, -1)]
[{u'test': u'1', u'status': u'success'}, {u'test': u'2', u'status': u'failure'}]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top