How to control when localstorage data expires?

6

I'm storing some data in the localstorage of the browser and would like to set an expiration date for this data, but I do not find a way to control when that data expires .

Does this data have any control over when it will be discarded?

When will the browser discard this data? If the user's HD gets full, do they spend a few months? If the HD burn? hehe ... forget the burning HD.

    
asked by anonymous 29.01.2014 / 20:20

4 answers

5

Apparently it is not possible to set expiration for data in localStorage .

Constant storage usage

Generally you will get a finite set of stored items and will update them as needed, keeping usage more or less constant. In this scenario, you would not have to worry about the limit.

Solution to simulate data expiration

If you want to check if the data is old, the best you can do is store the date and time you created them, and when to retrieve them, check if they are within the acceptable time. >

Example:

var object = {value: "value", timestamp: new Date().getTime()}
localStorage.setItem("key", JSON.stringify(object));

Then retrieve the item and check the date:

var object = JSON.parse(localStorage.getItem("key")),
    timestamp = object.timestamp,
    value = object.value;

verificarValidade(timestamp); 

Sample source: answer in StackOverflow.com

Removing items that are no longer used

Problems begin if you're just adding new items indiscriminately.

If you know that any item will no longer be used, you can use localStorage.removeItem(key) to free the storage space.

In addition, if you have stored the date as indicated in the previous topic, you can also iterate over the stored items using length and key(n) and remove old ones .

Temporary storage using sessionStorage

If the scope of stored items does not have to persist when the user closes the tab or the browser you can also use sessionStorage ". It has the same interface as the localStorage , however, according to the specification, the data is removed after the user exits the browser "session" or "context."

Here is an example usage:

//save a value
sessionStorage.setItem("name", "Seu nome");

//retrieve item
var name = sessionStorage.getItem("name");

//get the key name for the first item
var key = sessionStorage.key(0);

//remove the key
sessionStorage.removeItem(key);

//check how many key-value pairs are present
var count = sessionStorage.length;

Sample source here .

    
29.01.2014 / 20:26
1

Although it varies according to the Browser, LocalStorage has a limit size: link

p>

I do not know if there is any way to clean it automatically. I suspect it must be manual itself.

    
29.01.2014 / 20:27
0

The localstorage only deletes when it expires or the user cleans manually.

  

localStorage will keep the recorded data until it is removed directly, you can close the browser, restart the computer and the data will still be there. -    Font

The way to validate is the cookie, if it does not exist, it has been deleted.

    
29.01.2014 / 20:24
0

There really is no expiration date for this data, what you can do is to control by javascript when the user accesses the application you check if it's time to expire the data, and reload, or just remove the information .

You can JStorage that has the setTTL (key, ttl) method which in turn uses setTimeout, but if you want to use this "cookie expiration" would have to be developed.

    
30.01.2014 / 17:04