DISCLAIMER: This post assumes that the machine where the code is executed and the Redis server is in the same machine or located within a secure premises.
You can clone the code from: https://github.com/pinczakko/py_obj_redis_seralization.git
The principle used by the code is simple:
- Serialize the object as string by using Python pickle.dumps() from the pickle module (https://docs.python.org/2/library/pickle.html)
- Store the object as "string" data type in Redis (https://redis.io/topics/data-types) and use the following formula for the key to address the object in Redis: "test-meta-webhook-" + object.subs_id , where subs_id acts as a unique identifier for the object.
- Retrieve the object string by using the same key used in 2.
- Deserialize the object by using pickle.loads() from the pickle module.
The approach explained above is just for playground code because it probably doesn't scale as expected or is not well suited for latency sensitive application (the pickle.dumps() and pickle.loads() took too much time). However, for simple experimental code it's a nice to have "brute force" solution ;-). Below is sample output of the code:
Python Object Serialization/Deserialization to/from Redis |