'How to write and read custom objects to and from Redis with Python?

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?



Solution 1:[1]

Check out Redis OM Python

It essentially turns Redis into a storage layer for Python classes.

Solution 2:[2]

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.

  1. 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.

  2. Msgpack is cross-language but many objects that are directly picklable they need to be defined with msgpack. For example datetime.datetime is directly serialized with pickle but msgpack needs to be told how to treat this object with a method similar to this:

    import msgpack from datetime import datetime

    def encode_datetime(timestamp): if isinstance(timestamp, datetime): timestamp = {'datetime': True, 'as_str': timestamp.strftime("%Y%m%dT%H:%M:%S.%f").encode()} return timestamp

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anton
Solution 2 KZiovas