Skip to content

Latest commit

 

History

History
69 lines (43 loc) · 1.88 KB

serializers.rst

File metadata and controls

69 lines (43 loc) · 1.88 KB

Serializers

Serializers can be attached to backends in order to serialize/deserialize data sent and retrieved from the backend. This allows to apply transformations to data in case you want it to be saved in a specific format in your cache backend. For example, imagine you have your Model and want to serialize it to something that Redis can understand (Redis can't store python objects). This is the task of a serializer.

To use a specific serializer:

>>> from aiocache import Cache
>>> from aiocache.serializers import PickleSerializer
cache = Cache(Cache.MEMORY, serializer=PickleSerializer())

Currently the following are built in:

NullSerializer

aiocache.serializers.NullSerializer

StringSerializer

aiocache.serializers.StringSerializer

PickleSerializer

aiocache.serializers.PickleSerializer

JsonSerializer

aiocache.serializers.JsonSerializer

MsgPackSerializer

aiocache.serializers.MsgPackSerializer

In case the current serializers are not covering your needs, you can always define your custom serializer as shown in examples/serializer_class.py:

../examples/serializer_class.py

You can also use marshmallow as your serializer (examples/marshmallow_serializer_class.py):

../examples/marshmallow_serializer_class.py

By default cache backends assume they are working with str types. If your custom implementation transform data to bytes, you will need to set the class attribute encoding to None.