aisk / rust-memcache

memcache client for rust
MIT License
130 stars 39 forks source link

Add automatic json serialization and deserialization #143

Open Shahab96 opened 3 months ago

aisk commented 3 months ago

The original idea to automatically load and dump JSON/msgpack values is from the Python memcache client. The pymemcache can set a normal Python object with its set method, serialize it with Python's pickle library, and use a bit flag stored in the memcache flag to indicate it. When getting a key with such a value, pymemcache will check the flag and use pickle to deserialize the value.

I haven't dug too much to see if Rust can implement this too, but I guess maybe we can try to see if we can let Serialize / Deserialize implement FromMemcacheValue / ToMemcacheValue to achieve the goal.

And If we can implment this, I think we can just implment one serialization protocol like JSON, and add a hook to let user change the default behavior to change the serialization protocol and compress format.

Shahab96 commented 3 months ago

Looking through the code, this should be possible to implement. There is a catch though, right now we don't actually set flags for anything anywhere so we would need to add that first. This would unfortunately need changes to the arguments passed into set in ProtocolTrait to allow the caller to pass flags in. The drawback there is that once we allow that there wouldn't be a way to tell whether a flag was set by us or by the user, and honestly us setting a flag doesn't seem like good behavior as we could possibly be overwriting user data.

An alternative is to allow the user to select a serialization type for the connection, and simply have everything serialized to the selected type, however this would mean the consumer wouldn't be able to use different ser/de types for different keys.

What are your thoughts on this?

Shahab96 commented 3 months ago

I do think we should adjust the arguments for set either way, as currently consumers of this library have no way to set flags on their data unless I am mistaken in my understanding of the code

aisk commented 3 months ago

Sorry late for the reply. Already commented on the new issue. I think it's fine to add a mechanism to let users specify the flags.