Skip to content

Commit

Permalink
Merge pull request #1 from K0Te/fix-list-performance
Browse files Browse the repository at this point in the history
Fix cache setup for non-empty list serializer Meta.
  • Loading branch information
K0Te committed May 13, 2020
2 parents 261923c + c27c594 commit 1126087
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
@@ -1,8 +1,8 @@
language: python
python:
- "3.4"
- "3.5"
- "3.6"
- "3.7"
- "3.8"
script:
- pip install coveralls
- python setup.py test
Expand Down
18 changes: 14 additions & 4 deletions drf_serializer_cache/cache.py
Expand Up @@ -56,10 +56,20 @@ def fields(self):
cache[self.__class__] = super().fields
return cache[self.__class__]

@classmethod
def many_init(cls, *args, **kwargs):
"""Use cached list serializer if possible."""
meta = getattr(cls, 'Meta', None)
if meta is not None and not hasattr(meta, 'list_serializer_class'):
# Meta has no custom list serializer, it's safe to use optimized
meta.list_serializer_class = CachedListSerializer
elif meta is None:
cls.Meta = type(
'Meta',
tuple(),
{'list_serializer_class': CachedListSerializer})
return super().many_init(*args, **kwargs)


class CachedListSerializer(SerializerCacheMixin, ListSerializer):
pass


SerializerCacheMixin.Meta = type(
'Meta', tuple(), {'list_serializer_class': CachedListSerializer})
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -11,7 +11,7 @@

setup(
name='drf-serializer-cache',
version='0.3.3',
version='0.3.4',
description='Django REST framework (DRF) serializer speedup',
license='BSD',
long_description=long_description,
Expand Down
3 changes: 2 additions & 1 deletion tests/performance/list_simple.py
@@ -1,6 +1,7 @@
import os
import django

import sys
sys.path.append(os.getcwd())
os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.settings' # noqa
django.setup() # noqa

Expand Down
10 changes: 8 additions & 2 deletions tests/performance/list_with_reused_instance.py
@@ -1,6 +1,7 @@
import os
import django

import sys
sys.path.append(os.getcwd())
os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.settings' # noqa
django.setup() # noqa

Expand Down Expand Up @@ -30,7 +31,9 @@ class PointSerializer(Serializer):


class CachedPointSerializer(SerializerCacheMixin, PointSerializer):
pass

class Meta:
"""Just some empty meta info."""


class LineSerializer(Serializer):
Expand All @@ -42,6 +45,9 @@ class CachedLineSerializer(SerializerCacheMixin, Serializer):
start = CachedPointSerializer()
end = CachedPointSerializer()

class Meta:
"""Just some empty meta info."""


start_point = Point(0, 0, 0)
end_point = Point(10, 10, 10)
Expand Down
4 changes: 2 additions & 2 deletions tests/performance/recursive_model_serializer.py
@@ -1,8 +1,8 @@
import os
import django
import timeit


import sys
sys.path.append(os.getcwd())
os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.settings' # noqa
django.setup() # noqa

Expand Down

0 comments on commit 1126087

Please sign in to comment.