1.1.1 • Published 1 month ago
ng-cache v1.1.1
ng-cache
A cache system for Angular application.
Getting started
Install ng-cache
npm install ng-cache
Usage
import {ngCache } from 'ng-cache';
...
//Http request
@ngCache()
public getUsers() {
return this.http.get<User[]>('user')
}
//Value is stored in session storage if set a key
@ngCache('user')
public getUsers() {
return this.http.get<User[]>('user')
}
//Dynamic composit key.
@ngCache('user{}') //key = user1 id id=1;
public getUserById(id: number) {
return this.http.get<User>('user/'+ id)
}
//Sync method
@ngCache()
public method() {
console.log("Call method")
const length = 50000000;
let item = 0;
for(let index=0; index < length; index++) {
item = item + 2;
}
return item;
}
Configuration
//Local configuration.
@ngCache(null, {expirationTime: 2*60000}) // in milliseconds
//If you want the data is cached in local storage and not in session one
@ngCache("user", {isInLocalStorage: true})
//Global configuration.
import {BzCacheModule} from 'ng-cache';
@NgModule({
.....
BzCacheModule.forRoot(
{
expirationTime: 2*60000,
isInLocalStorage: true //If you want the data is cached in local storage and not in session one
}
)
......
Demo
Please give to repo a star :star:.