Skip to content

Instantly share code, notes, and snippets.

@aj07mm
Forked from swdevbali/savejsoninredis.py
Created May 18, 2018 03:05
Show Gist options
  • Save aj07mm/8b973874d47cd96042273c822c44697e to your computer and use it in GitHub Desktop.
Save aj07mm/8b973874d47cd96042273c822c44697e to your computer and use it in GitHub Desktop.
Simply json.dumps() => save to redis => redis.get => json.loads() => Python dict with proper data type created
__author__ = 'ekowibowo'
import json
import os
import redis
REDIS_HOST = os.getenv('SS_ABTEST_REDIS_HOST', '192.168.59.103')
r = redis.StrictRedis(host=REDIS_HOST)
for k in r.keys('abtest:experiments:*'):
r.delete(k)
exp1 = {
"id": 1,
"name": "testExperiment",
"device": "desktop",
"active": False,
"buckets": [
{
"name": "bucket1"
},
{
"name": "bucket2"
}
]
}
exp1_json_dumps = json.dumps(exp1)
print 'json.dumps:', exp1_json_dumps, ' type=', type(exp1_json_dumps)
r.set('abtest:experiments:1', exp1_json_dumps)
exp1_from_redis = r.get('abtest:experiments:1')
print 'exp1_from_redis', exp1_from_redis, ' type=', type(exp1_from_redis)
exp1_from_redis_json_loads = json.loads(exp1_json_dumps)
print 'exp1_from_redis_json_loads', exp1_from_redis_json_loads, 'type=', type(exp1_from_redis_json_loads)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment