如何在内存中缓存Django模型

时间:2022-03-23 02:27:47

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 init method 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中使用Managers。

Thanks in advance and sorry for my bad english.

提前谢谢,抱歉我的英语不好。

2 个解决方案

#1


8  

You can use Django's provided caching.

您可以使用Django提供的缓存。

This answer to a very similar question gets you on the right track.

对一个非常相似的问题的答案可以让你走上正轨。

#2


1  

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/jmoiron/johnny-cache for 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/jmoiron/johnny-cache但发现它与django> 1.6不兼容,所以我们切换到https://github.com/vijaykatam/django-cache -经理

#1


8  

You can use Django's provided caching.

您可以使用Django提供的缓存。

This answer to a very similar question gets you on the right track.

对一个非常相似的问题的答案可以让你走上正轨。

#2


1  

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/jmoiron/johnny-cache for 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/jmoiron/johnny-cache但发现它与django> 1.6不兼容,所以我们切换到https://github.com/vijaykatam/django-cache -经理