Skip to content
Permalink
Browse files

interceptors for caching

  • Loading branch information
ignatandrei committed Jan 19, 2020
1 parent 66e3588 commit 2bde8130add59c84d11d7348ad0a88bb576e356a
@@ -16,7 +16,8 @@ import { FormsModule } from '@angular/forms';
import { BankComponent } from './bank/bank.component';
import { DashboardComponent } from './dashboard/dashboard.component';
import { ProgrammerComponent } from './programmer/programmer.component';

import { CachingInterceptor } from 'src/utils/CachingInterceptor';
import {HTTP_INTERCEPTORS} from '@angular/common/http';
@NgModule({
declarations: [
AppComponent,
@@ -38,7 +39,11 @@ import { ProgrammerComponent } from './programmer/programmer.component';
MatIconModule,
MatListModule
],
providers: [],
providers: [ {
provide: HTTP_INTERCEPTORS,
useClass: CachingInterceptor,
multi: true
}, ],
bootstrap: [AppComponent]
})
export class AppModule { }
@@ -4,7 +4,7 @@

export const environment = {
production: false,
url: 'http://localhost:44385/'
url: 'https://localhost:44385/'
};

/*
@@ -7,21 +7,25 @@ import { tap, startWith } from 'rxjs/operators';

@Injectable()
export class CachingInterceptor implements HttpInterceptor {
cache: Map<string, any> = new Map<string, any>();
static cache: Map<string, any> = new Map<string, any>();
constructor() {}

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// continue if not cachable.
if (this.isCachable(req)) { return next.handle(req); }

if (!this.isCachable(req)) { return next.handle(req); }
console.log(`start intercept ${req.url} ${this.isCachable(req)}`);
let obsCache: HttpResponse<any> = null;
if (this.cache.has(req.url)) {
if (CachingInterceptor.cache.has(req.url)) {
console.log('exists in cache');
const cachedResponse = this.cache.get(req.url);
const cachedResponse = CachingInterceptor.cache.get(req.url);
obsCache = new HttpResponse({ body: cachedResponse });
}

let sendDataAndCache$ = this.sendRequestAndCache(req, next);
if(obsCache) {
const duplicate = req.clone();
console.log(`${req.url} exists in cache ${CachingInterceptor.cache.has(req.url)} ${obsCache != null}`);
let sendDataAndCache$ = this.sendRequestAndCache(duplicate, next);
if (obsCache != null) {
sendDataAndCache$ = sendDataAndCache$.pipe(
tap(e => {
console.log('from cache');
@@ -36,12 +40,14 @@ export class CachingInterceptor implements HttpInterceptor {
req: HttpRequest<any>,
next: HttpHandler,
): Observable<HttpEvent<any>> {

return next.handle(req).pipe(
const url = req.url;
return next.handle(req).pipe(
tap(event => {
console.log(`in the tap for ${url} ${event instanceof HttpResponse}`);
// There may be other events besides the response.
if (event instanceof HttpResponse) {
this.cache[req.url] = event.body; // Update the cache.
CachingInterceptor.cache.set(url,event.body); // Update the cache.
//console.log(`added cache for ${url} with value ${event.body}`);
}
})
);

0 comments on commit 2bde813

Please sign in to comment.
You can’t perform that action at this time.