ng-cache
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

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('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;
}

Configuration

 //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
    }
  )
   
   ......

Evict

//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 ⭐.

Package Sidebar

Install

npm i ng-cache

Weekly Downloads

1

Version

1.1.1

License

MIT

Unpacked Size

84.2 kB

Total Files

24

Last publish

Collaborators

  • 68ociredef