This repo was inspired by: django-rest-framework-cache
Implemented the feature to auto-discover serializer related models(by bfs
), and clear serializer cache which be
changed.
Not yet packaged to pip.
Add 'drf_serializer_data_cache' to your INSTALLED_APPS setting.
INSTALLED_APPS = (
...
'drf_serializer_data_cache',
)
You need to let your ModelSerializer
to inherit the CachedSerializerMixin
(Tips:CachedSerializerMixin
must on left,
because of mro
.):
from rest_framework import serializers
from drf_serializer_data_cache.mixins import SerializerCacheMixin
class YourModelSerializer(SerializerCacheMixin, serializers.ModelSerializer):
pass
To the cache successfully work you must configure the Django CACHES setting. We recomend that you take a look on Django cache docs here: https://docs.djangoproject.com/en/stable/topics/cache/#setting-up-the-cache
If you need use a cache backend different of the default you can specify it on the SERIALIZER_CACHE_BACKEND
.
To do this edit your settings.py
like this:
# ...
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.redis.RedisCache',
'LOCATION': 'redis://127.0.0.1:6379',
},
'local_memory': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': 'unique-snowflake',
}
}
REST_FRAMEWORK_CACHE = {
'SERIALIZER_CACHE_BACKEND': 'local_memory',
}
# ...
You can set the cache timeout using SERIALIZER_CACHE_TIMEOUT
.
REST_FRAMEWORK_CACHE = {
'SERIALIZER_CACHE_TIMEOUT': 86400, # Default is 1 day
}
By default, This library will automatically connect to serializer related model signals.
When serializer related model instance changed, related cache will be deleted.
You can set it by AUTO_DELETE_RELATED_CACHES
.
REST_FRAMEWORK_CACHE = {
'AUTO_DELETE_RELATED_CACHES': False # Default is True
}