简体   繁体   中英

Autocomplete performance improvement with caching the JSON response

I am using this autocomplete plugin (FCBautocomplete). with every character I enter to the field to get the results back, one request is sent to server. Since my data is too big, I am concerned about all the memory crashes that might happen and also the time. say if I have a contact named 'John Smith' there will be 10 requests sent (with all the result back and then result will be displayed based on my chars entered) which is too expensive. Now my question is how does cache help me? Would it be possible to cache the JSON response and get the rest of the search result from that cached response (ie user enters the first char and all the result will be generated as a JSON response, when the user enters the second char and the rest of chars, instead of sending another request, it searches from the cached JSON response).

Please let me know a good solution for this issue as I am concerned about the performance. Thanks

You try this code on the ajax call.

    var cache = {};
$.ajax({
    url : ajax.url + '&suggestions=' + $.trim(queryText),
    type : 'POST',
    dataType : 'json',
    success: function(data){
        if(data.value.length > 0){
            addItemFeed(data, etext);
            cache = data;
            bindEvents();
        }else{
            feed.hide();
        }
    }
});

just assign one object named as cache and put your data on the cache and try to access this result anywhere on the page to avoid unwanted ajax call.

Hope this helps...!!!

在javascript中定义全局变量可以解决您的查询。

window.AutoCompleteCacheVariable = "";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM