Last active
October 7, 2018 07:55
-
-
Save nsrau/141e9351ab47fd9d6111236d6027d026 to your computer and use it in GitHub Desktop.
Local Storage html5 - service angular
This file contains 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
export class LocalstorageService { | |
private _localstorage: Storage = window.localStorage; | |
private _cache_duration = 15; | |
constructor() { | |
if (!this.supportsLocalStorage()) { | |
console.error('LocalstorageService: No HTML5 localStorage Support'); | |
} else { | |
this.AutoClear(); | |
} | |
} | |
supportsLocalStorage(): boolean { | |
return typeof(this._localstorage) !== 'undefined'; | |
} | |
Set(key: string, val: string): void { | |
this._localstorage.setItem(key, val); | |
} | |
Get(key: string): any { | |
return this._localstorage.getItem(key); | |
} | |
Remove(key: string): void { | |
this._localstorage.removeItem(key); | |
} | |
SetJSON(key: string, val: Object): void { | |
try { | |
this.Set(key, JSON.stringify(val)); | |
} catch (e) { | |
console.error(e); | |
} | |
} | |
GetJSON(key: string): any { | |
try { | |
return JSON.parse(this.Get(key)); | |
} catch (e) { | |
console.error(e); | |
return null; | |
} | |
} | |
ClearAll(): void { | |
this._localstorage.clear(); | |
} | |
private AutoClear(): void { | |
// When was it last updated? | |
const m = 60000; | |
const t = this._cache_duration * m; | |
const timestamp = this.Get('check_duration'); | |
const now = new Date().getTime(); | |
// Is the timestamp valid? | |
const invalid = !timestamp || now - timestamp > t; | |
if (invalid) { | |
// Flush the cache. | |
this.ClearAll(); | |
// New timestamp. | |
this.Set('check_duration', now); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment