在使用子对象更新和检索模型实例时,如何在Django中使用memcache? [英] How to use memcache with Django when updating and retrieving model instances with children?

查看:95
本文介绍了在使用子对象更新和检索模型实例时,如何在Django中使用memcache?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的Django应用,不执行任何数据库缓存.我正在努力实现memcached,以提高性能并减少昂贵的数据库命中次数.

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.

到目前为止,我已经在服务器上安装并正在运行内存缓存,并已安装 pymclib django-memcache-admin 并按照我

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.

这是我的模特:

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)

这是网址路径:

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

以下是通过添加新的myObjectB子记录来修改myObjectA实例的API视图:

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,
        )

对myObjectA_Modify的调用可以具有任何任意ID.我事先不知道将使用哪个ID. myObjectA可以具有不确定数量的myObjectB子级.此外,还有其他单独的API可返回所有myObjectAs和myObjectB的完整列表.

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.

如何修改此应用程序代码以与memcache配合使用?我的插入键应该是什么?我需要确保,如果任何模型都有新插入或更新的子记录,则缓存中的父记录是更新.当前,一旦某些内容进入缓存,它就不会更新,因此该网页显示了过时的信息.如果您可以向我展示上述代码段的实际代码更改,那将是最有帮助的.

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.

推荐答案

一种相对简单的方法是将函数附加到

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.

这篇关于在使用子对象更新和检索模型实例时,如何在Django中使用memcache?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆