0

I have an existing Django app that doesn't do any Database caching. I am working to implement memcached to get a performance boost and reduce the amount of expensive database hits.

So far I have installed and am running memcache on my server, installed pymclib, django-memcache-admin and modified my settings.py file as I described here. I have not yet modified any of my actual application code. But I can still see that caching is in effect when I look at the django-memcache-admin dashboard. The caching is also evident because when I load some of the views, the data displayed is out of date. IE: updated data is not getting into the cache. I need advice on how to fix this. More detailed explanation is given below.

Here is are my models:

class myObjectA(models.Model):  
    field1 = models.CharField(max_length=255)

    def modify(self):
        newB = myObjectB(fk_myObjectA=self, field2="Blah Blah")
        newB.save()

    def getBChildren(self):
        return myObjectB.objects.filter(fk_myObjectA=self)


class myObjectB(models.Model):  
    fk_myObjectA = models.ForeignKey(myObjectA, related_name="Blah_Blah") 
    field2 = models.CharField(max_length=255)

Here is the url path:

url(
    r'^api/myObjectA_Modify/(?P<myObjectA_ID>\d+)/?$', 
    myObjectA_Modify.as_view()
),

Here is the API view that modifies an instance of myObjectA by adding a new myObjectB child record:

class myObjectA_Modify(mixins.UpdateModelMixin, generics.GenericAPIView):
    queryset = myObjectA.objects.all()
    serializer_class = myObjectA_Serializer   

    def put(self, request, *args, **kwargs):

        retrieved_myObjectA = get_object_or_404(
            myObjectA, 
            pk=request.POST["myObjectA_ID"],
        )

        retrieved_myObjectA.modify()

        return Response(
            myObjectA_Serializer(retrieved_myObjectA.getBChildren()).data,
            status=status.HTTP_200_OK,
        )

The call to myObjectA_Modify can be with any arbitrary ID. I don't know in advance which ID will be used. myObjectA can have an indeterminate number of myObjectB children. Furthermore there are other separate APIs that returns the whole list of all myObjectAs and myObjectBs.

How can I modify this application code to work with memcache? What should my insertion keys be? I need to make sure that if any model has a newly-inserted or updated child record, the parent record in the cache is updates. Currently once something gets into the cache it doesn't get updated so the webpages display out-of-date information. If you can show me the actual code changes to the above snippet, it would be most helpful.

Community
  • 1
  • 1
Saqib Ali
  • 11,931
  • 41
  • 133
  • 272

1 Answers1

1

A relatively simple way is to attach a function to the post_save signal of the model, and invalidate the cache if the model instance is updated.

knbk
  • 52,111
  • 9
  • 124
  • 122
  • This looks like the solution I was looking for! I will give it a try and report back. Thanks! – Saqib Ali Jun 06 '14 at 04:46
  • Thanks @knbk. That was very helpful! But I have a bit more complex question now. Please see here: http://stackoverflow.com/questions/24127997/how-to-name-arrange-my-memcached-keys-when-i-have-django-objects-with-multiple-f – Saqib Ali Jun 09 '14 at 20:00