Last active
June 1, 2022 19:24
-
-
Save swdevbali/f4adcacd402216256dd8 to your computer and use it in GitHub Desktop.
Redis store data as string, getting it back and trying to dump it as Json will still create json with string data type. We need to convert it back to correct data type using our own dict of types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'ekowibowo' | |
import os | |
import redis | |
from flask import jsonify, Flask | |
import json | |
REDIS_HOST = os.getenv('SS_ABTEST_REDIS_HOST', '192.168.59.103') | |
hkey = 'redisobject:whyweneedit' | |
r = redis.StrictRedis(host=REDIS_HOST) | |
r.delete(hkey) | |
dict_to_store = { | |
"id": 2, | |
"name": "desktopCatalogueFeedColumning", | |
"device": "desktop", | |
"active": True, | |
"buckets": [ | |
{ | |
"name": "TWO-COLUMN" | |
}, | |
{ | |
"name": "THREE-COLUMN" | |
} | |
] | |
} | |
r.hmset(hkey, dict_to_store) | |
dict_from_redis = r.hgetall(hkey) | |
print 'Python dict_to_store', dict_to_store | |
print 'Python dict_from_redis', dict_from_redis | |
print 'Converted with json.dumps() ', json.dumps(dict_from_redis) | |
DEFAULT_VALUE = {str(int): 0, str(float): 0.0, str(list): [], str(dict): {}, str(bool): False, str(str): ''} | |
def decode(key_types, redis_dict): | |
result = {} | |
# construct initial Python dict result with default value | |
for k in key_types: | |
type = key_types[k] | |
result[k] = DEFAULT_VALUE[str(type)] | |
for k in redis_dict.keys(): | |
v = redis_dict[k] | |
if k in key_types: | |
type = key_types[k] | |
if type == list or type == bool: | |
result[k] = eval(v) | |
else: | |
if v == '' and (type == int or type == float): | |
v = '0' | |
result[k] = type(v) | |
else: | |
result[k] = v | |
return result | |
types = {} | |
types['experiment'] = {'id': int, 'active': bool, 'buckets': list, 'device': str} | |
print 'Converting it the right way...' | |
redis_dict_in_python = decode(types['experiment'], dict_from_redis) | |
print 'redis_dict_in_python', redis_dict_in_python | |
redis_dict_in_python_to_json = json.dumps(redis_dict_in_python) | |
print 'redis_dict_in_python_to_json', redis_dict_in_python_to_json | |
app = Flask(__name__) | |
@app.route('/') | |
def json(): | |
return jsonify(data=dict_from_redis, success=True, error={}) | |
app.run(host='0.0.0.0', port=9999, debug=True) |
I want to store a python dict in redis but as i am getting data every 15 minutes so i also need to update the redis every 15 minutes. Can you help me how I can achieve that? I mean, How i can dynamically assign a key every time i write data in redis as when i am using the same key.. it is overwriting the old data.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yep, got it. My bad.
Wil test it first