0
我想做一個緩存流,在用戶請求緩存一個870記錄的大字典,它應該留在緩存一段時間。當定義的時間在下一次請求時傳遞時,字典應該在na緩存中更新。Django緩存,丟失密鑰
所以我創造了這樣一個功能:
from django.core.cache import get_cache
def update_values_mapping():
cache_en = get_cache('en')
values_dict = get_values_dict() <- this make a request to obtain the dict with values
cache_en.set_many(values_dict, 120) # 120s for testing
cache_en.set('expire', datetime.datetime.now() + datetime.timedelta(seconds=120))
然後在第二個功能,我嘗試從緩存
from django.core.cache import get_cache
def get_value_details(_id):
cache = get_cache('en')
details = cache.get(_id, {}) # Values in cache has expire date so they should eventually be gone
expire = cache.get('expire', None)
if not details and expire and expire < datetime.now():
update_values_mapping()
value = cache.get(_id, {})
return details
得到的值在渲染視圖get_value_details()被調用多次獲取所有需要的值。
問題是某些值缺失,例如cache.get('b',{})返回{}即使值'b'被保存到緩存(並且過期日期還沒有通過)。缺失值正在改變,有時是'a',有時'b',其他時間'c'等。
我一直在LocMemCache和DummyCache上測試它到目前爲止。 我的例子緩存設置:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': 'cache-default'
},
'en': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': 'cache-en'
},
'pl': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': 'cache-pl'
}
}
當我與打在控制檯一些值從緩存update_values_mapping()的下一次調用後消失,但有些人從一開始就下落不明。
有沒有人有任何線索可能是什麼? 或者,也許如何解決另一種方式描述的流程?