Krukov / cashews

Cache with async power
MIT License
371 stars 22 forks source link

JSON serializer #211

Open cyberbudy opened 3 months ago

cyberbudy commented 3 months ago

It there any json serializer?

Krukov commented 2 months ago

Hello,

No, there are not json serializer, only pickle supported. Can you tell a little bit more why do you need it ?

cyberbudy commented 2 months ago

We have a microservices product with multiple languages, mainly python, go and rust. We’d like to use cashews on python side, but the same cache might be available on other languages for optimization or migration purposes. Pickle is great for python but it’s hard/impossible in other languages

Krukov commented 2 months ago

Got you, thanks.

Originally one of a key feature of cashews is an ability to cache almost any object in code.

I have a one dirty recipe that can change serialization to json

import json
from cashews.picklers import Pickler

class JsonPickler(Pickler):
    @staticmethod
    def loads(value: bytes):
        return json.loads(value)

    @staticmethod
    def dumps(value) -> bytes:
        return json.dumps(value).encode()

...
backend = cache.setup(...)
backend._serializer.set_pickler(JsonPickler)

# 1714933479.152822 [0 172.17.0.1:48892] "SET" "key" "{\"test\": \"raw\"}" "PX" "300000"

As you can see cashews don't have public api to set your own serializer, I gonna extend it in a future ) Probably we need to add custom json encoder/decoder for some types like datetime because some features in cashews use it internally ( like early cache )

In general, I cannot guarantee that this will work well, and probably you know that aiocache have different serialization backends, so it may be a good choice for your case.

cyberbudy commented 2 months ago

Thanks for detailed answer. I'll try to use it

What if I create a PR with a new serializer?

Krukov commented 2 months ago

What if I create a PR with a new serializer?

it would be nice. I'll be happy so