How to use memcache with Django when updating and retrieving model instances with children?

戏子无情 提交于 2020-01-25 10:00:21

问题


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.


回答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.



来源:https://stackoverflow.com/questions/24072925/how-to-use-memcache-with-django-when-updating-and-retrieving-model-instances-wit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!