I have an ordered dictionary where the values are of a custom type object (for example datetime.datetime
) and I want to cache it to Redis. What is a good and secure way to store it because, as far as I am aware, there is no way to store custom objects to Redis?
An basic example of my ordered dictionary and my object could be this :
import datetime
from dataclasses import dataclass
from collections import OrderedDict
@dataclass(frozen=True)
class Prediction:
_id: int
risk: str
timestamp: datetime.datetime
history =OrderedDict([("old",Prediction(_id=1,risk="low",timestamp=datetime.datetime(2022, 5, 13, 10, 10, 30, 568388))),("new",Prediction(_id=2,risk="high",timestamp=datetime.datetime(2022, 5, 13, 12, 4, 9, 568388))) ])
how can this be processed, stored and retrieved from Redis?
2
Answers
I found another option. Basically you can use a serializer and serialize your object before storing it to
Redis
. Such a serialized can be for example Pickle or Msgpack and Msgpack for Python.Pickle
is described as python specific which means it wont open in another language after serialization BUT it allows for pickling almost all common python objects without having to do any additional steps.Msgpack
is cross-language but many objects that are directly picklable they need to be defined with msgpack. For exampledatetime.datetime
is directly serialized withpickle
but msgpack needs to be told how to treat this object with a method similar to this:Check out Redis OM Python
It essentially turns Redis into a storage layer for Python classes.