0

what i need

  • i need like when user hits url say local/abc , then in this case xhr call works.

  • then again open local/abc2 page xhr call goes .

  • this would enhance extra xhr call on server.

  • i need like when user hits first page then only xhr call could be saved in cache.

  • if user again hits another url no xhr call should be called from cache itself.

javascript code

       if(/Googlebot/i.test(navigator.userAgent)){}else{
        !function(e,t){var n=function(){return n.get.apply(n,arguments)},r=n.utils={isArray:Array.isArray||function(e){return Object.prototype.toString.call(e)==="[object Array]"},isPlainObject:function(e){return!!e&&Object.prototype.toString.call(e)==="[object Object]"},toArray:function(e){return Array.prototype.slice.call(e)},getKeys:Object.keys||function(e){var t=[],n="";for(n in e)e.hasOwnProperty(n)&&t.push(n);return t},escape:function(e){return String(e).replace(/[,;"\\=\s%]/g,function(e){return encodeURIComponent(e)})},retrieve:function(e,t){return e==null?t:e}};n.defaults={},n.expiresMultiplier=86400,n.set=function(n,i,s){if(r.isPlainObject(n))for(var o in n)n.hasOwnProperty(o)&&this.set(o,n[o],i);else{s=r.isPlainObject(s)?s:{expires:s};var u=s.expires!==t?s.expires:this.defaults.expires||"",a=typeof u;a==="string"&&u!==""?u=new Date(u):a==="number"&&(u=new Date(+(new Date)+1e3*this.expiresMultiplier*u)),u!==""&&"toGMTString"in u&&(u=";expires="+u.toGMTString());var f=s.path||this.defaults.path;f=f?";path="+f:"";var l=s.domain||this.defaults.domain;l=l?";domain="+l:"";var c=s.secure||this.defaults.secure?";secure":"";e.cookie=r.escape(n)+"="+r.escape(i)+u+f+l+c}return this},n.remove=function(e){e=r.isArray(e)?e:r.toArray(arguments);for(var t=0,n=e.length;t<n;t++)this.set(e[t],"",-1);return this},n.empty=function(){return this.remove(r.getKeys(this.all()))},n.get=function(e,n){n=n||t;var i=this.all();if(r.isArray(e)){var s={};for(var o=0,u=e.length;o<u;o++){var a=e[o];s[a]=r.retrieve(i[a],n)}return s}return r.retrieve(i[e],n)},n.all=function(){if(e.cookie==="")return{};var t=e.cookie.split("; "),n={};for(var r=0,i=t.length;r<i;r++){var s=t[r].split("=");n[decodeURIComponent(s[0])]=decodeURIComponent(s[1])}return n},n.enabled=function(){if(navigator.cookieEnabled)return!0;var e=n.set("_","_").get("_")==="_";return n.remove("_"),e},typeof define=="function"&&define.amd?define(function(){return n}):typeof exports!="undefined"?exports.cookie=n:window.cookie=n}(document);

         var cookie_val = parseInt(cookie.get("notification_after_clicked_detailmobile"), 10);
         var cookie_vals= parseInt(cookie.get("notification_before_clicked_detailmobile"), 10);

            if(cookie_val!=1)
            {
                var mobile="/app.php/notificationdetail";
                var cache = {};
                var http=new XMLHttpRequest
                url=mobile,params=url;http.open("POST",url,!0),

                http.setRequestHeader("Content-type","application/json; charset=UTF-8"),
                http.onreadystatechange=function()
                {
                    if(4==http.readyState&&200==http.status)
                    {

                    cache[url] = http.responseText;
                    var obj=http.responseText;
                    var parsed=JSON.parse(obj);
                    var notification_count=1;
                    $(".notification1").append(notification_count);
                    setTimeout(function(){
                    document.getElementById('myimage').style.display='block';
                    document.getElementById('myimage').style.float='left';},10);
                    for(var x=0;x<parsed.length;x++)
                    {
                        var objs=parsed[x];
                        var eventname=objs.abbr_name;
                        var date=objs.startDate;
                        var event_url=objs.event_url;
                        var city=objs.city;
                        var country=objs.country;
                        var time=objs.endDate;
                        var monthNames=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
                        var dateObj=new Date(date);
                        var months=dateObj.getUTCMonth()+1;var month=monthNames[dateObj.getMonth()];
                        var day=dateObj.getUTCDate();
                    if(x==0)
                    {
                        var htmlcontent='<a href="/'+event_url+'" > <div class="row row_color"><div class="col-xs-12"><p class="grey_heading_notification"><strong>Similar Events from your industry in '+country+'</strong></p></div><div class="col-xs-2"><div class="circle_image">'+month+' <br><span class="notification_black">'+day+'</span></div></div><div class="col-xs-10"><p>'+eventname+'</p><div class="col-xs-9"><div class="row"><p class="small"><span class="notification_light_grey"><i class="fa fa-map-marker orange12"></i> '+city+', '+country+'</span></p></div></div><div class="col-xs-3"><div class="row"><span class=" btn_orange_small pull-right">Attend</span></div></div></div></div></a>';

                    }
                    else if(x %2==0)
                    {
                        var htmlcontent='<a href="/'+event_url+'" > <div class="row row_color"><div class="col-xs-2"><div class="circle_image">'+month+' <br><span class="notification_black">'+day+'</span></div></div><div class="col-xs-10"><p>'+eventname+'</p><div class="col-xs-9"><div class="row"><p class="small"><span class="notification_light_grey"><i class="fa fa-map-marker orange12"></i> '+city+', '+country+'</span></p></div></div><div class="col-xs-3"><div class="row"><span class=" btn_orange_small pull-right">Attend</span></div></div></div></div></a>';

                    }
                    else
                    {
                    var htmlcontent='<a href="/'+event_url+'" ><div class="row row_color1"><div class="col-xs-2"><div class="circle_image">'+month+'<br><span class="notification_black">'+day+'</span></div></div><div class="col-xs-10"><p>'+eventname+'</p><div class="col-xs-9"><div class="row"><p class="small"><span class="notification_light_grey"><i class="fa fa-map-marker orange12"></i> '+city+', '+country+'</span></p></div></div><div class="col-xs-3"><div class="row"> <span class=" btn_orange_small pull-right">Attend</span> </div></div></div></div></a>';
                    }
                $('#notify_bar').append(htmlcontent);}}},http.send(JSON.stringify(params));
            }
        }

it says cache:true only works with GET and HEAD request.

  • i need to know cache :true is implemented in javscript.

i have tried

      params = "action=" + action 
     + "&domain=" + encodeURIComponent(domain) 
     + "&preventCache="+new Date();
Community
  • 1
  • 1
afeef
  • 547
  • 3
  • 9
  • 25

1 Answers1

1

You can create an object to memoize the ajax responses: You need to serialize your string request and use it as a key for the object you will use for caching the requests, if the key exists then you will return the value from the cache if it doesn't then you will do an ajax request.

NON-JS Solution: You can use Etags for this, check your web server documentation.

Felix
  • 3,058
  • 6
  • 43
  • 53