How do I know if a feature is in the browser cache?

7

Is it possible to discover, via JavaScript and without any additional HTTP requests, whether or not a particular feature is in the browser cache? And if it is, get it also without this requisition?

In my understanding, when the server submits a "long-running" feature (ie a header Expires with a very high value) browser checks - in a second query - if this resource exists in its own cache and its validity has not yet expired, that no further requests are made. However, if such a resource is not found or is "outdated", the request is made immediately - without giving the programmer the chance to cancel it.

I'd like to have a little more control over this process: If the feature is in the cache, use it, otherwise adopt an alternate strategy. For this it would take a sort of JavaScript API to query the state of the cache, I believe, and as far as I know this is not possible - since the browser abstracts its programmer cache.

Is there something - preferably cross-browsers - that meets this requirement? Or maybe an alternative way to get the same result?

Note: I'm not interested in solutions involving local storage - this would require taking the resources already in the cache and saving (and managing) a copy of them in that already limited space.

    
asked by anonymous 13.09.2014 / 01:25

1 answer

2

Although you renegotiate solutions with local storage, I think this would be the only alternative to check the cache without generating a request on the server. So here is an idea of how to do it, and you do not need to save all the content of the element, just the src string, and at 2mb you can put at least 50,000.

var storage = window.localStorage;
if (!storage.ElementosNoCache) {
    storage.ElementosNoCache = "";
}

function RegistrarCache(src) {
    if (!VerificarCache(src)) {
        storage.ElementosNoCache += '[' + src + ']';
    }
}

function RemoverCache(src) {
    if (VerificarCache(src)) {
        storage.ElementosNoCache = storage.ElementosNoCache.replace('[' + src + ']', '');
    }
}

function VerificarCache(src) {
    return (storage.ElementosNoCache.indexOf('[' + src + ']', 0) >= 0);
}

function LimparCache() {
    storage.ElementosNoCache = "";
}

Log automation can be done with jquery, see working on JSFiddle .

    
13.09.2014 / 03:57