settings.py
为我的所有用户充当一种…伪造的,临时的数据缓存。然而,我注意到它并没有按照我所希望的方式运行,并且似乎是随机地重置回它的默认值。
设置.py
# initialize with a fake key:value for ease in logging
STORE_DATA = {'hello': 'goodbye'}
views.py
def get_data(name):
from apiclient.discovery import build
search_q = name
service = build('youtube', 'v3', developerKey='<key-here>')
results = service.search().list(
part='snippet',
channelId='<channel-here>',
type='video',
q=search_q,
).execute()
settings.STORE_DATA[name] = results['items']
result = settings.STORE_DATA[name]
return result
page.videos = []
if page.name in settings.STORE_DATA:
page.videos = settings.STORE_DATA[page.name]
else:
page.videos = get_data(page.name)
这是唯一引用此全局变量的代码。它只是简单地存储一个youtube api调用的结果,这样我们就不必每次有人访问页面时都进行查询。
然而,有时这是可行的,有时则不然。有时存储在dict中的key:value对会重置回初始状态。这是我日志上的一个小打印-
cache before the call - [u'hello']
"page1" - 2018-08-22 23:23:47
cache after the call - [u'page1', u'hello']
cache before the call - [u'hello']
"page1" - 2018-08-22 23:24:13
cache after the call - [u'page1', u'hello']
cache before the call - [u'page1', u'hello']
page1 - found in cache!
cache after the call - [u'page1', u'hello']
cache before the call - [u'page1', u'hello']
page1 - found in cache!
cache after the call - [u'page1', u'hello']
cache before the call - [u'hello']
"page1" - 2018-08-22 23:27:50
cache after the call - [u'page1', u'hello']
您可以第一次看到,它在缓存中没有正确的数据,所以它存储了它。很好。第二次…等等。数据不在那里。再储存一次。第三次和第四次-就在那里!很好!第五次-wtf。又走了。
这太奇怪了,对我来说毫无意义。有人知道会发生什么吗?