2

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',
xpanta
  • 6,812
  • 14
  • 51
  • 90
  • I am using memcached too.I can't find any problems specific to ajax requests.Is it specific to some browsers? – Vivek S May 18 '12 at 10:09
  • No. It happens with every browser. Even in different computers. I paste my `settings.py` `MIDDLEWARE` classes – xpanta May 18 '12 at 10:55

3 Answers3

1

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() 
rafek
  • 607
  • 5
  • 8
  • 1
    No, it's a POST. Browsers don't cache POSTs. – Jordan May 18 '12 at 12:28
  • 1
    I believe this is the correct idea but on the wrong line. `$('#user_open_bets').load('/ob/' + user + '/?ajax=');` does the GET which is likely being cached. – Mark Lavin May 18 '12 at 12:30
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.

xpanta
  • 6,812
  • 14
  • 51
  • 90
0

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
});
m03geek
  • 2,429
  • 1
  • 19
  • 38