skip to Main Content

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


  1. Chosen as BEST ANSWER

    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
    

  2. Check out Redis OM Python

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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search