简体   繁体   English

Django缓存类

[英]Django Cached Class

I have a class in my Django project that corresponds to a DB table that I don't expect to change. 我的Django项目中有一个类,它对应一个我不希望更改的数据库表。 There are 4 rows in this table and I would like to cache them when the application first opens and then just hit the cache from then on. 该表中有4行,我想在应用程序第一次打开时缓存它们,然后从那以后一直访问缓存。

Is my best bet using the Low Level Cache API ? 我最好的选择是使用低级缓存API吗? If so, how can I then call that from every other view in my application? 如果是这样,我该如何在应用程序的每个其他视图中调用它呢? I just access the same cache variable? 我只是访问相同的缓存变量?

Thanks! 谢谢!

If they are constant, why not hard code them? 如果它们是常数,为什么不对它们进行硬编码? Anyway, you can cache them in global variables (for example, in some module), that way you will have to pay for accessing them (network-wise) only once per app instance. 无论如何,您可以将它们缓存在全局变量中(例如,在某个模块中),这样,您将需要为每个应用程序实例仅一次访问(网络方式)付费。

eg something like: 例如:

# module global_vals
VAL1 = get_from_db("...")
VAL2 = get_from_db("...")

then access them 然后访问他们

import global_vals
def foo():
  v1 = global_vals.VAL1
  v2 = global_vals.VAL2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM