1/** 2 * Local Storage com data de expiração 3 * 4 * @link https://javascript.plainenglish.io/implement-localstorage-with-expiry-time-a2af662f7705 5 * 6 */ 7 8window.timedLocalStorage = { 9 /** 10 * Separar as chaves conforme o idioma atual, para o cache não fique o mesmo entre idiomas. 11 * 12 */ 13 getKey(rawkey){ 14 let lang = document.getElementsByTagName('html')[0].getAttribute('lang'); 15 return `${rawkey}-${lang}`; 16 }, 17 getItem(rawkey){ 18 let key = this.getKey(rawkey); 19 20 // getting the data from localStorage using the key 21 let result = JSON.parse(window.localStorage.getItem(key)); 22 23 if(result){ 24 /** 25 * if data expireTime is less then current time 26 * means item has expired, 27 * in this case removing the item using the key 28 * and return the null. 29 */ 30 if(result.expireTime <= Date.now()){ 31 window.localStorage.removeItem(key); 32 return null; 33 } 34 // else return the data. 35 return result.data; 36 } 37 //if there is no data provided the key, return null. 38 return null; 39 }, 40 /** 41 * accepting the key, value and expiry time as a parameter 42 * default expiry time is 1 day in milliseconds. 43 */ 44 setItem(rawkey, value, maxAge = 24 * 60 * 60 * 1000){ 45 let key = this.getKey(rawkey); 46 47 // Storing the value in object. 48 let result = { 49 data:value 50 } 51 52 /** 53 * setting the expireTime currentTime + expiry Time 54 * provided when method was called. 55 */ 56 if(maxAge){ 57 result.expireTime=Date.now() + maxAge; 58 } 59 window.localStorage.setItem(key,JSON.stringify(result)); 60 }, 61 removeItem(rawkey){ 62 let key = this.getKey(rawkey); 63 window.localStorage.removeItem(key); 64 }, 65 clear(){ 66 window.localStorage.clear(); 67 } 68}
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.