Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中实现缓存_Python_Django_Caching - Fatal编程技术网

在Python中实现缓存

在Python中实现缓存,python,django,caching,Python,Django,Caching,我想用Python实现一个基本缓存 是这样的。我有两个文件名为:-cache.py,main.py 在my cache.py中,我希望每24小时生成一个列表。因此,我每24小时运行一次cronjobcache.py,返回最新列表 cache.py def get_hubcode_threshold_time(): global threshold_time hub_alarm_obj = HubAlarm.objects.all().values('time').distinct

我想用Python实现一个基本缓存

是这样的。我有两个文件名为:-
cache.py
main.py

在my cache.py中,我希望每24小时生成一个列表。因此,我每24小时运行一次cronjob
cache.py
,返回最新列表

cache.py

def get_hubcode_threshold_time():
    global threshold_time
    hub_alarm_obj = HubAlarm.objects.all().values('time').distinct()
    for obj in hub_alarm_obj:
        threshold_time.append(obj.time)


if __name__ == "__main__":
    get_hubcode_threshold_time()
    print threshold_time
import cache
print threshold_time
我每分钟运行
main.py
(另一个cronjob)。我想从缓存中打印阈值时间

main.py

def get_hubcode_threshold_time():
    global threshold_time
    hub_alarm_obj = HubAlarm.objects.all().values('time').distinct()
    for obj in hub_alarm_obj:
        threshold_time.append(obj.time)


if __name__ == "__main__":
    get_hubcode_threshold_time()
    print threshold_time
import cache
print threshold_time
说错话

NameError: global name 'threshold_time' is not defined

如何在Python中实现基本缓存:?

如果希望多个进程能够访问相同的数据(例如,
threshold\u time
的值),则需要某种持久性存储或协议来获取和设置数据。更具体地说,在
cache.py
文件中,计算完
threshold\u time
后,需要将其保存在某个位置。然后您的
main.py
可以从保存该值的任何位置检索该值

从您列出的内容来看,这似乎是redis的一个特别好的用例,但sqlite甚至文本文件也可以工作。每种持久性策略都有自己的数据完整性和复杂性级别