0
A
回答
1
如果他們是DB查詢花費時間,那麼除了停止數據庫服務器之外基本上什麼都不會阻止他們,這當然不是有形的。例如,如果它是嵌套循環中的計算,則可以使用緩存來檢測是否有另一個請求已從同一用戶提交。基本上是:
from django.core.cache import cache
def view(request):
start_time = timestamp # timezone.now() etc.
cache.set(request.session.session_key + 'some_identifier', start_time)
for q in werty:
# Very expensive computation with millions of loops
if start_time != cache.get(request.session.session_key + 'some_identifier'):
break
else:
# Continue the nasty computations
else:
cache.delete(request.session.session_key + 'some_identifier')
但Django的一部分一邊 - 我會做什麼:在JS添加一個條件,當搜索詞小於3個字符,然後等待0.5秒(或更少,不管你喜歡)前搜索。如果添加了另一個字符,則立即搜索。
I.e.
var timeout;
function srch(param) {
timeout = false;
if (param.length < 3) {
timeout = true;
setTimeout(function() {
if (timeout) {
$.ajax({blah: blah});
}
}, 500);
} else {
$.ajax({blah: blah});
}
}
什麼,什麼實際上設置緩存中的值? –
@PaulTomblin哎呀,更新了答案。它無論如何都是隨機的僞代碼來開始你的想法,但是這樣的事情應該實際上也起作用。 – makaveli