1

I am using serializer to get the related data for a particular resource like this:

SessionSerializer.py

def to_representation(self, instance):
  response = super().to_representation(instance)
  response["user"] = UserSerializer(instance.user).data
  if instance.experiment:
      response["experiment"] = ExperimentSerializer(instance.experiment).data
  response["last_event"] = EventSerializer(instance.last_global_event).data

  # fetch and return all session answers tied to this session
  response["session_answers"] = instance.sessionanswer_set.values()
  return response

I am also using DRF cache decorators in the SessionViewSet, which seems to be working fine. However, I cannot invalidate the cache data when there is an update to the instance and/or if there is an update to any related instance (like user).

Is there a standard practice or documentation for how to clear cache data when there is an update?

Vaibhav Rathore
  • 301
  • 2
  • 10

2 Answers2

1

To listen changes on database records and make changes according to them, you can use django signals:

https://docs.djangoproject.com/en/3.2/topics/signals/

lerime
  • 21
  • 1
  • will this update the nested relationships also? Like when a user is updated and Django signals update the user cache, it also updates the Session cache? – Vaibhav Rathore Sep 13 '21 at 08:57
  • There is a signal called **m2m_changed**. Also you can write a handler function to do what you want inside on it. This [link](https://stackoverflow.com/questions/27803633/django-signals-simple-examples-to-start) explains usage very good. – lerime Sep 13 '21 at 14:41
0

This is how I did, you find and learn more - I'm still learning. This is my views.py

from django.core.cache import cache

class stateViewSet(viewsets.ModelViewSet):
    ---
    ---
    def create(self, request, format=None):
        serializer = self.get_serializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            cache.delete('STATE_CACHE_KEY')
            q = state.objects.prefetch_related("states").all()
            cache.set('STATE_CACHE_KEY', q, 2 * 60)
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
    def list(self, request):
        queryset = cache.get('STATE_CACHE_KEY')
        if queryset is None:
            queryset = self.get_queryset()
            cache.set('STATE_CACHE_KEY', queryset, 2 * 60)
        serializer = self.get_serializer(queryset, many =True)
        return Response(serializer.data)
    def destroy(self, request, *args, **kwargs):
        try:
            instance = self.get_object()
            self.perform_destroy(instance)
            q = state.objects.prefetch_related("states").all()
            cache.delete('STATE_CACHE_KEY')
            cache.set('STATE_CACHE_KEY', q, 2 * 60)
        except Http404:
            pass
        return Response(status=status.HTTP_204_NO_CONTENT)
Ranu Vijay
  • 1,137
  • 9
  • 18