0

Am creating a web page that used ajax to load content and localCache technique to store loaded page for later use. Everything seems to work well except that i will like to ignore some page which i have also implemented a simple method to do that but the problem is when the URL is full https://example.com/view_item.php it won't ignore it but view_item.php it will ignore. and also some page that has url parameter it won't ignore it too view_item.php?item=123, view_item.php?item=123&title=abc or https://example.com/view_item.php?item=123 etc because i can't add those url in ignore array as it do change. I suggest that this can be archived using regex but am not show how.

var localCache = {
    data: {},
    remove: function (url) {
        delete localCache.data[url];
    },
    exist: function (url) {
        return localCache.data.hasOwnProperty(url) && localCache.data[url] !== null;
    },
    get: function (url) {
         console.debug('fatching data from URL ', url);
        return localCache.data[url];
    },
    set: function (url, check, cachedData, callback) {
         if(this.ignore(check)){
            console.debug('Need To Ignore URL ', check);
        }else{
            localCache.remove(url);
            localCache.data[url] = cachedData;
             console.debug('caching data from URL ', url);
            if ($.isFunction(callback)) callback(cachedData);
        }
    },
    ignore: function(url){
      var Arrays = [
          'cart.php',
          'favorites.php',
          'view_item.php'
      ];
      return ($.inArray( url, Arrays ) !== -1);
    }
};
5
  • 1
    This might be helpful, stackoverflow.com/questions/10152650/… Commented Nov 21, 2018 at 16:47
  • 1
    Just use what @VelimirTchatchevsky mentioned by replacing the usage of $.inArray with the code in the link. You will also need to replace the array contents. E.g.: instead of 'cart.php' something like /.*cart.php.*/. Regardless of that, controlling a "blacklist" of non-cacheable resources seems to be a code smell. Your server should use the proper headers and the requesting code should be able to read response headers and act accordingly Commented Nov 21, 2018 at 16:53
  • @GriffoGoes what am actually trying to do is to minimize the amount of request am sending to server. I want to abort the ajax request from client side once the file is in ignore list or use the old data, if i check for ignore in server side then no much need for my cache Commented Nov 21, 2018 at 17:01
  • @GriffoGoes please can you explain to me why /.*cart.php.*/ not this /cart.php/, i want to know the difference and how it can benefit me Commented Nov 21, 2018 at 17:37
  • @Peter Oops, no need for .*. It means to match any character (.) any number of times (* matches 0 to infinity). But without it will still works as it will match something in the middle. And regarding my suggestion, here's a link developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control . The idea is to inform the browser which url is "cacheable" and which is not. Commented Nov 22, 2018 at 21:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.