set, get or remove strings, integer and objects in localStorage.
Initialize the Storage and set a storage Prefix.
var s = new LocalStorageCache('myPrefix');
s.set('key', 123);
var value = s.get('key');
s.remove('key');
s.clear();
var object = {
string: 'HELO WORLD',
number: 100,
subObject: {
test: 1,
test2: 2,
test3: '3'
}
};
s.set('myObject', object);
The object myObject
is stored in localStorage with JSON.stringify().
The setter try to parse the stringifed value and return it with the original variabel type.
var object = s.get('myObject');
if(typeof object === 'object'){
console.log('it works');
}
/* output: it works */
s.remove('myObject');
Only the localStorage entries with the current prefix are remove. With an empty prefix, the whole localStorage will be cleared.
s.clear();