| 1 |
.. _serializers: |
| 2 |
|
| 3 |
Serializers |
| 4 |
=========== |
| 5 |
|
| 6 |
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. |
| 7 |
|
| 8 |
To use a specific serializer:: |
| 9 |
|
| 10 |
>>> from aiocache import Cache |
| 11 |
>>> from aiocache.serializers import PickleSerializer |
| 12 |
cache = Cache(Cache.MEMORY, serializer=PickleSerializer()) |
| 13 |
|
| 14 |
Currently the following are built in: |
| 15 |
|
| 16 |
|
| 17 |
.. _nullserializer: |
| 18 |
|
| 19 |
NullSerializer |
| 20 |
-------------- |
| 21 |
.. autoclass:: aiocache.serializers.NullSerializer |
| 22 |
:members: |
| 23 |
|
| 24 |
|
| 25 |
.. _stringserializer: |
| 26 |
|
| 27 |
StringSerializer |
| 28 |
---------------- |
| 29 |
|
| 30 |
.. autoclass:: aiocache.serializers.StringSerializer |
| 31 |
:members: |
| 32 |
|
| 33 |
.. _pickleserializer: |
| 34 |
|
| 35 |
PickleSerializer |
| 36 |
---------------- |
| 37 |
|
| 38 |
.. autoclass:: aiocache.serializers.PickleSerializer |
| 39 |
:members: |
| 40 |
|
| 41 |
.. _jsonserializer: |
| 42 |
|
| 43 |
JsonSerializer |
| 44 |
-------------- |
| 45 |
|
| 46 |
.. autoclass:: aiocache.serializers.JsonSerializer |
| 47 |
:members: |
| 48 |
|
| 49 |
.. _msgpackserializer: |
| 50 |
|
| 51 |
MsgPackSerializer |
| 52 |
----------------- |
| 53 |
|
| 54 |
.. autoclass:: aiocache.serializers.MsgPackSerializer |
| 55 |
:members: |
| 56 |
|
| 57 |
In case the current serializers are not covering your needs, you can always define your custom serializer as shown in ``examples/serializer_class.py``: |
| 58 |
|
| 59 |
.. literalinclude:: ../examples/serializer_class.py |
| 60 |
:language: python |
| 61 |
:linenos: |
| 62 |
|
| 63 |
You can also use marshmallow as your serializer (``examples/marshmallow_serializer_class.py``): |
| 64 |
|
| 65 |
.. literalinclude:: ../examples/marshmallow_serializer_class.py |
| 66 |
:language: python |
| 67 |
:linenos: |
| 68 |
|
| 69 |
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``. |