PageSourceSearch

https://biifsweden.github.io/javascripts/api-cache.js

js biifsweden.github.io collected 2026-10-03 08:42:45 UTC 1,743 bytes, 50 lines download raw bytes

1var cacheKey = "api-cache";
2
3function supportsHtml5Storage() {
4    try {
5        return 'localStorage' in window && window['localStorage'] !== null;
6    } catch (e) {
7        return false;
8    }
9}
10
11function getApiValue(endpointUrl, cacheTime, callback) {
12    try {
13        var cacheData = supportsHtml5Storage() ? JSON.parse(localStorage[cacheKey]) : null;
14        // This does an exact match for the URL given. Different spacing, duplicate slashes,
15        // alternate caps, etc. will result in the cache being bypassed.
16        if (cacheData && cacheData[endpointUrl] && Date.now() - cacheData[endpointUrl].dateRetrieved < cacheTime) {
17            callback(cacheData[endpointUrl].requestResult);
18            return;
19        }
20    }
21    catch (e) {
22        // Ignore the error; if the saved JSON is invalid, we'll just request it from the server.
23    }
24    
25    console.log('No cached copy of API data for endpoint "' + endpointUrl  + '" found. Downloading from remote server.');
26    $.ajax(endpointUrl).done(function (apiData) {
27        if (supportsHtml5Storage()) {
28            var cacheData = {};
29            
30            try {
31                cacheData = JSON.parse(localStorage[cacheKey]);
32            }
33            catch(e) {
34                // Ignore error; either the cache doesn't exist or it is invalid
35            }
36            
37            cacheData[endpointUrl] = {
38                dateRetrieved: Date.now(),
39                requestResult: apiData
40            };
41            
42            localStorage.setItem(cacheKey, JSON.stringify(cacheData));
43        }
44        
45        callback(apiData);
46    }).fail(function (xhr, error) {
47        console.error("Error getting API data: " + error);
48        callback(null, error);
49    });
50}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.