@nwx/http-cache
A simple http cache module for Angular applications
![download-image](https://img.shields.io/npm/dm/@nwx/http-cache.svg)
How to install
npm i @nwx/http-cache |OR| yarn add @nwx/http-cache
How to use
import { AppCfg, TargetPlatform } from '@nwx/cfg';
import { LogLevels } from '@nwx/logger';
export const environment: AppCfg = {
appName: '@nwx/http-cache',
target: TargetPlatform.web,
production: false,
log: {
level: LogLevels.debug
},
http-cache: {
ttl: 60,
}
};
import { CfgModule } from '@nwx/cfg';
import { LoggerModule } from '@nwx/logger';
import { http-cacheModule } from '@nwx/http-cache';
import { environment } from '../environments/environment';
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
CfgModule.forRoot(environment),
LoggerModule,
HttpCacheModule
],
bootstrap: [AppComponent]
})
export class AppModule {}
import { Component, OnInit, OnDestroy } from '@angular/core';
import { Subject } from 'rxjs';
@Component({
selector: 'app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent implements OnInit, OnDestroy {
title = 'Neekware';
options = {};
destroy$ = new Subject();
constructor(
private http: HttpClient,
private httpCache: HttpCacheService
) {
this.title = this.cfg.options.appName;
console.log('AppComponent loaded ...');
}
ngOnInit() {
const userStatePath = new OrderedStatePath().append('user', 1).toString());
this.httpCache.store
.select<User>(userStatePath)
.pipe(takeUntil(this.destroy$))
.subscribe({
next: user => {
console.log('User via Select', user);
},
});
this.makeUserRequest(1);
setTimeout(() => {
this.makeUserRequest(1);
}, 4)
setTimeout(() => {
this.makeUserRequest(1);
}, 6)
}
makeUserRequest(id: string) {
const cacheKey = new OrderedStatePath().append('user', id).toString();
const httpHeaders = addMetaToHttpHeaders({
policy: HttpCacheFetchPolicy.CacheFirst,
key: cacheKey,
ttl: 5,
});
this.http.get<User>(`/api/user/${id}`, { headers: httpHeaders })
.pipe(takeUntil(this.destroy$))
.subscribe(user => {
console.log('User via Http', user);
});
}
ngOnDestroy() {
this.destory$.next();
this.destroy$.complete();
}
}
Running the tests
To run the tests against the current environment:
npm run ci:all
License
Released under a (MIT) license.
Version
X.Y.Z Version
`MAJOR` version -- making incompatible API changes
`MINOR` version -- adding functionality in a backwards-compatible manner
`PATCH` version -- making backwards-compatible bug fixes
Surge