2

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?

0

2 Answers 2

3

Check out Redis OM Python

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

Sign up to request clarification or add additional context in comments.

1 Comment

I dont see any reference to working directly with dataclasses, do you know if it is possible?
1

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

1 Comment

Please put your code snippets between a pair of lines containing just three backticks ```

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.