What is it to you?
FileSystem
Local storage
Session storage
Cookies
WebSQL
Cache
IndexedDB
Note : Local and session storage work the same way but session storage is erased when window is closed
One tool for all storage options
import { NgForageConfig } from 'ngforage';
...
@NgModule({
...
})
export class AppModule {
public constructor(
ngfConfig: NgForageConfig
) {
ngfConfig.configure({
name: 'myProject',
driver: [ // optional - defaults to indexedDB -> webSQL -> localStorage -> sessionStorage
NgForageConfig.DRIVER_INDEXEDDB,
NgForageConfig.DRIVER_LOCALSTORAGE
]
});
}
}app.module.ts
import { Injectable, OnInit } from '@angular/core';
import { CachedItem, NgForage, NgForageCache, NgForageConfig } from 'ngforage';
import { Observable, from } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class StorageService implements OnInit {
constructor (
private readonly ngf: NgForage,
private readonly cache: NgForageCache
) { }
public getItem<T> (key: string): Observable<T> {
return from(this.ngf.getItem<T>(key));
}
public setItem<T> (key: string, data: T): Observable<T> {
return from(this.ngf.setItem<T>(key, data));
}
public removeItem (key: string): Observable<void> {
return from(this.ngf.removeItem(key));
}
public removeAll (): Observable<void> {
return from(this.ngf.clear());
}
public getCachedItem<T> (key: string): Observable<T | null> {
return from(this.cache.getCached<T>(key)
.then((r: CachedItem<T>) => {
if (!r.hasData || r.expired) {
return null;
}
return r.data;
}));
}
public setCachedItem<T> (key: string, data: T): Observable<T> {
return from(this.cache.setCached<T>(key, data));
}
public ngOnInit (): void {
this.ngf.name = 'myApp';
this.cache.driver = NgForageConfig.DRIVER_SESSIONSTORAGE;
}
}
Storage.service.ts