Python 如何在内存中缓存 Django 模型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19328172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to cache a Django Model in Memory
提问by Luan Fonseca
i have a Model in my app, that is used so much in my application, some views made something like 100 queries in that Table, I already have a tons of optimisations to make a better use of this Model, but the great part of that amount is caused by the Django initmethod of other classes, for personal reasons... So actual i just put a global variable to store all the objects of that Model in there, so when i create or delete a one object from that Model I just reload that List... but i dont want to use cache machines like Memcached or Redis... i just want to store that QuerySet result in the Memory... And i want to still use the Managers in this cached Queryset.
我的应用程序中有一个模型,在我的应用程序中使用了很多,有些视图在该表中进行了 100 次查询,我已经进行了大量优化以更好地利用该模型,但其中很大一部分数量是由其他类的 Django init方法引起的,出于个人原因......所以实际上我只是放了一个全局变量来存储该模型的所有对象,所以当我从该模型中创建或删除一个对象时,我只需重新加载该列表...但我不想使用像 Memcached 或 Redis 这样的缓存机器...我只想将该 QuerySet 结果存储在内存中...而且我仍然想在这个缓存的 Queryset 中使用管理器。
Thanks in advance and sorry for my bad english.
在此先感谢并为我的英语不好而感到抱歉。
采纳答案by Joseph
You can use Django's provided caching.
您可以使用Django 提供的缓存。
This answerto a very similar question gets you on the right track.
这个答案对一个非常类似的问题让你在正确的轨道上。
回答by Vijay Katam
I would suggest implementing a custom model manager and a query set. A django cache backend doesn't have to be memcached or redis it can be localmemory also, although I would advise you to stand up a real cache backend. We mostly have been using https://github.com/jmtheitroadon/johnny-cachefor this purpose but found it is not compatible with django > 1.6 so we are switching over to https://github.com/vijaykatam/django-cache-manager
我建议实现自定义模型管理器和查询集。django 缓存后端不必是 memcached 或 redis,它也可以是 localmemory,尽管我建议您建立一个真正的缓存后端。为此,我们主要使用https://github.com/jmtheitroadon/johnny-cache但发现它与 django > 1.6 不兼容,因此我们切换到https://github.com/vijaykatam/django-cache -经理