Created
January 7, 2016 08:20
-
-
Save singledigit/1fc604942484cec9413c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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