Django REST framework serializers speedup
Simple short-living cache implementation for DRF serializers. It solves two issues with DRF serializers:
Serializer.fieldsproperty can be extremely slow in some cases. Such cases include complexModelSerializer, serializer hierarchies with repeated serializers and recursive serializers.fieldsis cached and computed once per class for each serialization run. This leads to limitation -fieldsshould not be dynamically adjusted inside of serializer, which inherits fromSerializerCacheMixin;- By default serializers will re-compute object representation event if they encounter same object with same serializer twice. This can turn into an issue, if multiple object include same dependent hard-to-serialize objects. Cache avoid re-computing instance representation if it encounters same instances multiple times.
Performance results
Cache life-time and invalidation
Cache is designed to be simple and non-error-prone.
Cache is created when top-level serializer to_representation starts
and is cleaned up once this method is finished. Thus there is no need
for timeouts or complex cache invalidation. No data is shared between
requests.
Thus, following cases will work fine:
user = User.objects.get(name='Lee')
data_1 = UserSerializer(user).data
# OrderedDict([('name', 'Lee')])
user.name = 'Bob'
user.save()
data_2 = UserSerializer(user).data
# OrderedDict([('name', 'Bob')])
Usage
Usage should be pretty simple - inject SerializerCacheMixin before
inheriting from Serializer or ModelSerializer:
from drf_serializer_cache import SerializerCacheMixin
from rest_framework import serializers
class UserSerializer(SerializerCacheMixin, serializers.ModelSerializer):
class Meta:
model = User
fields = ('id', 'name')
Common pitfalls
Too often cache cleanup
Cache lives in serializer hierarchy root, but it's life-time is defined
by nearest tree node, which inherits from SerializerCacheMixin.
Ideal situation is when root inherits from SerializerCacheMixin.
That's why SerializerCacheMixin uses custom list_serializer_class,
which also inherits from SerializerCacheMixin.
If you use custom list as root of serializer hierarchy - it's recommended
to use SerializerCacheMixin as it's base class.
Too many hierarchies
By default serializers build nice hierarchy by calling bind on their
child serializers. This case can be broken for serializers.SerializerMethodField
if it uses some serializer without calling bind on it:
from drf_serializer_cache import SerializerCacheMixin
from rest_framework import serializers
class ResultSerializer(SerializerCacheMixin, serializers.Serializer):
results = serializers.SerializerMethodField()
def get_results(self, instance):
# recursive serializer
serializer = self.__class__(instance.results, many=True)
serializer.bind('*', self) # bind call is essential for efficient cache !
return serializer.data
Metadata
Release files for drf-serializer-cache 0.3.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distributions (wheels)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| drf_serializer_cache-0.3.4-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
| drf_serializer_cache-0.3.4-py2.7.egg | Legacy Egg format | - | - | Details |
Total release size: 33.9 kB
Release files / drf_serializer_cache-0.3.4-py2.py3-none-any.whl
| Download URL | drf_serializer_cache-0.3.4-py2.py3-none-any.whl |
|---|---|
| Size | 11.4 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
b4b9347b2bb28882ce577b236f6dd5008ad66baf0466e0aee3cdda6224b06764
|
|
BLAKE2b-256 checksum How to use checksums |
a40e9ec87f2d813ad2ed7c40d96ba29b373a2619ebe64f70db393e98a37c0ed2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.1
|
Release files / drf_serializer_cache-0.3.4-py2.7.egg
| Download URL | drf_serializer_cache-0.3.4-py2.7.egg |
|---|---|
| Size | 22.6 kB |
| Tags | Egg |
|
SHA-256 checksum How to use checksums |
66b5d31afa3229e300499b2c230d76ab5be125bee6594bbc826fe51edbbc2fb5
|
|
BLAKE2b-256 checksum How to use checksums |
302de5ccec6e12adf23d94e54607bfd3e3f52ca3a7235a6fdf5aa6c7eeb574b6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.1
|