在我的意見,我有以下Django的緩存得到過期項
@require_POST
def loadVals(request):
result = //do some heavy calculations
return HttpResponse(json.dumps({"data": result}), content_type="application/json")
現在我已經添加了一個緩存,這樣,我沒有執行「重calclations」所有的時間。因此,新的代碼看起來像
settings.py
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': 'unique-snowflake'
}
}
views.py
from django.core.cache import get_cache
@require_POST
def loadVals(request):
cache = get_cache('default')
result = cache.get('res')
if result == NONE:
result = //do some heavy calculations
cache.set('res', result, 30)
return HttpResponse(json.dumps({"data": result}), content_type="application/json")
else:
return HttpResponse(json.dumps({"data": result}), content_type="application/json")
我想要做的卻是,即使是高速緩存已經過期,我想保存前端用戶一些等待時間(因爲繁重的計算),並且只返回最後過期的值。 然後刷新緩存。
我如何
1)獲取過期緩存的值?因爲如果緩存已過期,cache.get('res')將返回NONE
2)在返回HttpResponse語句之後進行調用以刷新緩存值並執行大量計算(其中返回statmenet剛剛返回了過期值)或者可能通過異步調用來做到這一點?
尼斯使用螺紋。不幸的是,wsgi沒有保證這個過程將在下一個請求中出現。所以使用線程永久緩存是一種反模式。 – Wtower 2016-12-23 08:36:20