A cache system for Angular application.
Install ng-cache
npm install ng-cache
import {ngCache } from 'ng-cache';
...
//Http request
@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('key')
public method() {
const length = 50000000;
let item = 0;
for(let index=0; index < length; index++) {
item = item + 2;
}
return item;
}
//If the method has like argument the string NG_CACHE_DISABLED the cache is disabled.
@ngCache('key')
public method(NG_CACHE_DISABLED) {
const length = 50000000;
let item = 0;
for(let index=0; index < length; index++) {
item = item + 2;
}
return item;
}
//Local configuration.
@ngCache('key', {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,
isInSessionStorage: true //If you want the data is cached in session storage.
isInLocalStorage: true //If you want the data is cached in local storage and not in session one
}
)
......
//To delete cache of that key.
@ngCacheEvict("user")
update()
//The third argument deleteBySeed set to true is used to remove cache starting from a certain word common a more keys, which is equal the one set as the key.
@ngCacheEvict("user", null, true)
update()
Please give to repo a star ⭐.