When do items in HTML5 local storage expire?
https://stackoverflow.com/questions/2326943
السؤال الكامل
- javascript - html5 - local-storage |
- |
المحلول
????? ???? ?? ?????? ????? ?????? ????????. ???? ????? ????? ????????.
??????? ?????? ???? ?? ?????? ?? ???? ?? ?? ????? ??????? ??? ?????? ?? ?? ???? ???? ??? ????. ???? ????? ???????? ?????? ?? ??????? ??????? ?? ?? ??? ????? ??????? ??? ???????? ??????. ??? ?? ????? ?? ?????? ????????. ????? ??? ?????? ???? "??? ?????" ??????? ??? ??? ??????? ?????? ???? ??????.
????? ?> - ?? ??????? ?? ??????? ????? ?? ????? ????? ??????? ??? ?? ???? ???? ????? ???. ???? ??? ????? ????? ????? ???? ?? ?????? ?????? ?? ?? ??? ?? ???? ??? ?????? ??? ?? ??????? ??? ?? ??? ???? ?? ???? ?? ??? ??? ??? ?? ??? ??? ?????????.
نصائح أخرى
???? ?? ????? ??? ?????? ?????? ???? ?? ?????? ?> ?? ????? ?? ??????? ??????
var object = {value: "value", timestamp: new Date().getTime()}
localStorage.setItem("key", JSON.stringify(object));
?????? ????? ????? ??????? ??? ?????? ?????? ??????? ?? ??????? ??????? ???? ??? ?????? ?????? ???? ??????.
var object = JSON.parse(localStorage.getItem("key")),
dateString = object.timestamp,
now = new Date().getTime().toString();
compareTime(dateString, now); //to implement
?????? ??????? lscache . ??? ?????? ?? ??? ??????? ?? ???????? ??? ?? ??? ??????? ???? ?????? ??? ??????? ????. ???? ??? ???? ???? ???? ????? ??????? ???? ?? ?????? ??? ?????? ???????? ??????.
??? readme
:
lscache.set
Stores the value in localStorage. Expires after specified number of minutes.
Arguments
key (string)
value (Object|string)
time (number: optional)
???? ?? ????? ??????? ??? ??? ?????? ??????? ???????. ???? ??? ????? ?????? ???? ??? ?????.
??? ??? ?? ????? ??? ?? ?????? ?? ???????? ????? ?????? ????? ???? ???? ????? (??? JSON) ??????? ?? ??? ????????.
??????? ??????? ???? ??? ???? ????? ???? ??????? ?????? ??? ??? ????????. ????? ?????? ??? lscache ?? ???? ?? ?????? ?????? ??? ?????? ??? ????? ?????? ????? ??????.
????
?Brynner ??????? ???? ???? ????: ????? ????? ?????? ??? ????????? ?????? ????. ???? ???????? ??? ??? ??? ???? ???? ????? ?? ????????? ?? ??? ???? ????? JSON ???????? ? ?? ?? ????? ??? ????? ??? ?????? ??? ?????? ??????.
???? ???? ???? ?????:
/* removeStorage: removes a key from localStorage and its sibling expiracy key
params:
key <string> : localStorage key to remove
returns:
<boolean> : telling if operation succeeded
*/
function removeStorage(name) {
try {
localStorage.removeItem(name);
localStorage.removeItem(name + '_expiresIn');
} catch(e) {
console.log('removeStorage: Error removing key ['+ key + '] from localStorage: ' + JSON.stringify(e) );
return false;
}
return true;
}
/* getStorage: retrieves a key from localStorage previously set with setStorage().
params:
key <string> : localStorage key
returns:
<string> : value of localStorage key
null : in case of expired key or failure
*/
function getStorage(key) {
var now = Date.now(); //epoch time, lets deal only with integer
// set expiration for storage
var expiresIn = localStorage.getItem(key+'_expiresIn');
if (expiresIn===undefined || expiresIn===null) { expiresIn = 0; }
if (expiresIn < now) {// Expired
removeStorage(key);
return null;
} else {
try {
var value = localStorage.getItem(key);
return value;
} catch(e) {
console.log('getStorage: Error reading key ['+ key + '] from localStorage: ' + JSON.stringify(e) );
return null;
}
}
}
/* setStorage: writes a key into localStorage setting a expire time
params:
key <string> : localStorage key
value <string> : localStorage value
expires <number> : number of seconds from now to expire the key
returns:
<boolean> : telling if operation succeeded
*/
function setStorage(key, value, expires) {
if (expires===undefined || expires===null) {
expires = (24*60*60); // default: seconds for 1 day
} else {
expires = Math.abs(expires); //make sure it's positive
}
var now = Date.now(); //millisecs since epoch time, lets deal only with integer
var schedule = now + expires*1000;
try {
localStorage.setItem(key, value);
localStorage.setItem(key + '_expiresIn', schedule);
} catch(e) {
console.log('setStorage: Error setting key ['+ key + '] in localStorage: ' + JSON.stringify(e) );
return false;
}
return true;
}
???? ????? ??????? ?????? ?? ??? ????? ???? ?????? ????????? ??????? ?????? ??. ?????? ???????? ????? ??????? ?????? ?? ?????? ???? ????? ???? ????? ??????? ?????? ???? ??????? ?? ???????? ?????? ??? ??????.
???? ???? ?????? ?? ???:
if (!$.cookie('your_key') || !localStorage.getItem('your_key')) {
//get your_data from server, then...
localStorage.setItem('your_key', 'your_data' );
$.cookie('your_key', 1);
} else {
var your_data = localStorage.getItem('your_key');
}
// do stuff with your_data
???? ?????? ???? ?????? ?? ??????? ?????????? ?????? ??? ????? ???????. ?????? ????? ??????? ?? ????? ?????? ???? ????? ??? ??????? ???? ???????? ?????? ?your_data ?? ???? ?????? ?? ???? ??????.
?????? ?? ??? ???? ?????? ??? > ????? ?> ???? ???????? ??????? ???? ???? ?? ????? ????? ???????? ??????? ???? ?????? ??? ????? ????????. ??? ???? ??? ????? ??????? ????? ?? ?? ?? ???? ????? ??? ????? ???? ?? 4K ?? ???? ?????? (???? ???? ??????)? ???? ???????? ?? ?????? ???????? ?????? ??? ?????? ??? ????? ??? ????? ???? ???????? ???????? ?????? ??? ?????? .
????? ????>
???? ???? ???? ???????? ??? sessionStorage ???>
?????? ????>- ??? ??? ??? ??????? ?????? ???> ???? ????? ???? ????? ?????? / ????? ??????? ??>
- ?sessionStorage ???? ???? ???? ?? ???? ???? ?????? ?? ?????? ??>
?????????? ?????? ???????
?????? ????>sessionStorage.setItem("key","my value");
????? ????>
?????????? ???? ?????? ???
?????? ????>var value = sessionStorage.getItem("key");
????? ????>
??? ????? ???????? ??
?????? ????> sessionStorage.key = "my val";
sessionStorage["key"] = "my val";
sessionStorage.setItem("key","my value");
????? ????>
??? ??? ?????? ??? ?
?????? ????> var value = sessionStorage.key;
var value = sessionStorage["key"];
var value = sessionStorage.getItem("key");
???? ?????? ?? ???? ???? ?? ????? / ????????.
????? :
????? ????>????? ????? ??? ?? ????? ???????? ?? ????? ??????? ??????? ??? ?????? ????? ?? ????? ???? ???? ??? ?? ??? ????????. ??? ?? ???? ???????? ????? ???? ??? ???????? ?? ??? ?? ????????? ???? ???? ?????? ??? ??? ?????? ??? ???????.
?????? ????>
??? ????? W3C:
????? ????>????? ????? ??? ?? ????? ???????? ?? ????? ??????? ??????? ??? ?????? ????? ?? ????? ???? ???? ??? ?? ??? ????????. ??? ?? ???? ???????? ????? ???? ??? ???????? ?? ??? ?? ????????? ???? ???? ?????? ??? ??? ?????? ??? ???????.
?????? ????>???? ???? ?? ???? ????????? ?????? ?? ??? ?????? ?????? ????? ?? ???????? setItem (?????? ????)? ???? ?? ????? ??? ????? ?? ????? ????? ???? ?? ???????? ???????.
// Functions
function removeHtmlStorage(name) {
localStorage.removeItem(name);
localStorage.removeItem(name+'_time');
}
function setHtmlStorage(name, value, expires) {
if (expires==undefined || expires=='null') { var expires = 3600; } // default: 1h
var date = new Date();
var schedule = Math.round((date.setSeconds(date.getSeconds()+expires))/1000);
localStorage.setItem(name, value);
localStorage.setItem(name+'_time', schedule);
}
function statusHtmlStorage(name) {
var date = new Date();
var current = Math.round(+date/1000);
// Get Schedule
var stored_time = localStorage.getItem(name+'_time');
if (stored_time==undefined || stored_time=='null') { var stored_time = 0; }
// Expired
if (stored_time < current) {
// Remove
removeHtmlStorage(name);
return 0;
} else {
return 1;
}
}
// Status
var cache_status = statusHtmlStorage('cache_name');
// Has Data
if (cache_status == 1) {
// Get Cache
var data = localStorage.getItem('cache_name');
alert(data);
// Expired or Empty Cache
} else {
// Get Data
var data = 'Pay in cash :)';
alert(data);
// Set Cache (30 seconds)
if (cache) { setHtmlStorage('cache_name', data, 30); }
}
??? ??? ?? ???????? jStorage ??????? ????? ????? ?? ???? ????? ?????? ?? ????? setTTL ??? jStorage ???????
$.jStorage.set('myLocalVar', "some value");
$.jStorage.setTTL("myLocalVar", 24*60*60*1000); // 24 Hr.
???? ??? ?? ??? ?? ???? ???? ?? ?? ???? ??? ?????? ?????? ??? ??? ??? ???? ??? ??????? ???? ????? ??? ?????? ??????? / ?????? / ????? ??????? ????? ?????? ????? ?? ?????? ???:
????? ???????? ?????? ?localforage:
angular.module('app').service('cacheService', function() {
return {
set: function(key, value, expireTimeInSeconds) {
return localforage.setItem(key, {
data: value,
timestamp: new Date().getTime(),
expireTimeInMilliseconds: expireTimeInSeconds * 1000
})
},
get: function(key) {
return localforage.getItem(key).then(function(item) {
if(!item || new Date().getTime() > (item.timestamp + item.expireTimeInMilliseconds)) {
return null
} else {
return item.data
}
})
}
}
})
? @sebarmeli ?? ????? ?? ?????? ?? ????? ???? ??? ??? ???? ??? ???????? ???? ????? ????? ?????? ?? sessionStorage
?? ??????? ?? ???? ?????? ??????:
???? ?? ???? ??????? (sessionStorage) ???? ????? ??? ????? ????? ?? ?? ???? ???? ?????? ??????. ???? ?????? ????? ?????? ????? ????? ???? ??? ?????? ??? ?????? ??????? ??????????. ???? ???? ?? ????? ????? ????? ?? ????? ???? ???? ????? ??? ????.
?????? ????>
?????? ????????:
???? ???????? ?? ??? ???? ?? ???? ????? ?? ????? ????? ???? ????? ??????? ?????. ??? ??? ????? ??? ???? ??? ??????? UI ??????? ??? ???????? ??? ????? (??? ????? ??????? ???????? ?????? ??????? ??????????? ??? ??????).
???? ?? ???? ?????? ??????? ???? ???? ?? ???? ???? ????? + ??? ????? ???? ?????? ????? ??? ???.
??????: ??? ?? ???? ???? ??? ??? ????? ??? ??????? ??????? ???? ????
// Addition
if(window.localStorage){
localStorage.setItem('myapp-' + new Date().getTime(), 'my value');
}
// Removal of all expired items
if(window.localStorage){
// two mins - (1000 * 60 * 20) would be 20 mins
var expiryTime = new Date().getTime() - (1000 * 60 * 2);
var deleteRows = [];
for(var i=0; i < localStorage.length; i++){
var key = localStorage.key(i);
var partsArray = key.split('-');
// The last value will be a timestamp
var lastRow = partsArray[partsArray.length - 1];
if(lastRow && parseInt(lastRow) < expiryTime){
deleteRows.push(key);
}
}
// delete old data
for(var j=0; j < deleteRows.length; j++){
localStorage.removeItem(deleteRows[j]);
}
}
?????? ?? ???? ??? ????.
var hours = 24; // Reset when storage is more than 24hours
var now = new Date().getTime();
var setupTime = localStorage.getItem('setupTime');
if (setupTime == null) {
localStorage.setItem('setupTime', now)
} else {
if(now-setupTime > hours*60*60*1000) {
localStorage.clear()
localStorage.setItem('setupTime', now);
}
}