1

I'm new to python. I'm getting errors when running my code:

import redis

def redisSave(case, key, ob):
    dataBase = None
    if case == 'Product':
        dataBase = redis.Redis(db=0)
        dataBase.set(key, ob)
        dataBase.expire(key, time=600)
    else:
        pass

dictOb = {
    'price': '2000 $',
    'weight': '50 lb'
}
redisSave('Product', 'first', dictOb)

It says that input type is invalid (redis.exceptions.DataError)

CypherX
  • 7,019
  • 3
  • 25
  • 37

2 Answers2

2

Try this one:

def redis_save(case, key, obj):
    data_base = None

    if case == 'Product':
        data_base = redis.Redis(db=0)

        if isinstance(obj, dict):
            data_base.hmset(key, obj)
            data_base.expire(key, time=600)

Access the data: data_base.hgetall('first')

Result:

{
    b'price': b'2000 $',
    b'weight': b'50 lb'
}
inmate_37
  • 1,218
  • 4
  • 19
  • 33
0

You could also store the data as a JSON-dump or as a pickled object. See: how to store a complex object in redis (using redis-py).

CypherX
  • 7,019
  • 3
  • 25
  • 37
  • Please read the discussions in the comments in that thread as well, for a well informed decision on which option is best for you. – CypherX Oct 24 '19 at 10:10