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);
}
};
$.inArraywith 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/.*cart.php.*/not this/cart.php/, i want to know the difference and how it can benefit me.*. 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.