问题
I have setup memcached (unix socket) for my django application. However it seems that some ajax requests do not work, as expected, while memcached is on. I am using memcached on my entire site.
For example in this javascript function the .load()
directive works the first time, but after that it keeps 'fetching' the same page from cache.
function placeBet(user, bet) {
var ajax_data = {
status:false,
message: ''
}
$.ajax({
url:'/place_bet/' + user + '/?ajax=&bet=' + bet,
type:"POST",
dataType:"json",
data:ajax_data,
success:function (data){
var message = "";
$('#user_open_bets').load('/ob/' + user + '/?ajax=');
if (data.status == false){
alert(data.message);
}
} // success
}); // ajax
}
How can I force those ajax request to reload from database instead from cache?
EDIT.
This is my settings.py
MIDDLEWARE classes
MIDDLEWARE_CLASSES = (
'django.middleware.cache.UpdateCacheMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'main.common.tz_middleware.TimezoneMiddleware',
'main.common.sslMiddleware.SSLRedirect',
'django.middleware.cache.FetchFromCacheMiddleware',
回答1:
It seems that the answer was simpler than I thought. I found the solution in the django docs.
https://docs.djangoproject.com/en/dev/topics/cache/#controlling-cache-using-other-headers
I copy paste (for other people to know)
from django.views.decorators.cache import never_cache
@never_cache
def myview(request):
# ...
Having said that, @Alex's suggestion seems interesting and I would like to try it. However now my server is down for migration reasons. I need to wait for some hours. I will report here, later.
回答2:
This is default browser behaviour (caching ajax requests). To avoid that pass addictional parameter to your request:
url:'/place_bet/' + user + '/?ajax=&bet=' + bet+'&t='+new Date().getTime()
EDIT: correct url to load is:
'/ob/' + user + '/?ajax=&t=' + new Date().getTime()
回答3:
Have you tried adding param
cache: false,
to your ajax request? Forexample:
$.ajax({
url:'/place_bet/' + user + '/?ajax=&bet=' + bet,
type:"POST",
dataType:"json",
data:ajax_data,
cache: false,
success:function (data){
var message = "";
$('#user_open_bets').load('/ob/' + user + '/?ajax=');
if (data.status == false){
alert(data.message);
}
} // success
});
来源:https://stackoverflow.com/questions/10650282/django-memcached-and-ajax-requests