Skip to content

Instantly share code, notes, and snippets.

@singledigit
Created January 7, 2016 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save singledigit/1fc604942484cec9413c to your computer and use it in GitHub Desktop.
Save singledigit/1fc604942484cec9413c to your computer and use it in GitHub Desktop.
/**
* Created by ericjohnson on 10/6/15.
*/
import moment from 'moment';
export class Storage {
constructor() {
this.index = {};
this.storage = window.localStorage;
this.session = window.sessionStorage;
// reload index if needed
this.loadIndex();
}
loadIndex() {
let x = Object.keys(this.storage);
for (var i = 0; i < x.length; i++) {
this.index[x[i]] = true;
}
}
store(key, value, expiration = (60*60*24*14), session = true) { // expiration default is 14 days multiplied by seconds
let item = JSON.stringify({stamp: moment().add(expiration, 'seconds'), data: value});
if(!session){
this.index[key] = true;
this.storage.setItem(key, item);
}
else{
this.session.setItem(key, item);
}
}
retrieve(key){
let returnItem = JSON.parse(this[this.index[key] ? 'storage': 'session'].getItem(key));
// if exists and not expired then return value
if(returnItem && moment() <= moment(returnItem.stamp)){
return returnItem.data;
}
// else, clear from storage and return null
this.remove(key);
return null;
}
remove(key){
this[this.index[key] ? 'storage': 'session'].removeItem(key);
delete this.index[key];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment