Skip to content

Commit

Permalink
interceptors for caching
Browse files Browse the repository at this point in the history
  • Loading branch information
ignatandrei committed Jan 19, 2020
1 parent 66e3588 commit 2bde813
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
9 changes: 7 additions & 2 deletions InfoValutar/InfovalutarWebAng/src/app/app.module.ts
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import { FormsModule } from '@angular/forms';
import { BankComponent } from './bank/bank.component'; import { BankComponent } from './bank/bank.component';
import { DashboardComponent } from './dashboard/dashboard.component'; import { DashboardComponent } from './dashboard/dashboard.component';
import { ProgrammerComponent } from './programmer/programmer.component'; import { ProgrammerComponent } from './programmer/programmer.component';

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


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


/* /*
Expand Down
24 changes: 15 additions & 9 deletions InfoValutar/InfovalutarWebAng/src/utils/CachingInterceptor.ts
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,21 +7,25 @@ import { tap, startWith } from 'rxjs/operators';


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


intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// continue if not cachable. // 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; let obsCache: HttpResponse<any> = null;
if (this.cache.has(req.url)) { if (CachingInterceptor.cache.has(req.url)) {
console.log('exists in cache'); console.log('exists in cache');
const cachedResponse = this.cache.get(req.url); const cachedResponse = CachingInterceptor.cache.get(req.url);
obsCache = new HttpResponse({ body: cachedResponse }); obsCache = new HttpResponse({ body: cachedResponse });
} }


let sendDataAndCache$ = this.sendRequestAndCache(req, next); const duplicate = req.clone();
if(obsCache) { 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( sendDataAndCache$ = sendDataAndCache$.pipe(
tap(e => { tap(e => {
console.log('from cache'); console.log('from cache');
Expand All @@ -36,12 +40,14 @@ export class CachingInterceptor implements HttpInterceptor {
req: HttpRequest<any>, req: HttpRequest<any>,
next: HttpHandler, next: HttpHandler,
): Observable<HttpEvent<any>> { ): Observable<HttpEvent<any>> {

const url = req.url;
return next.handle(req).pipe( return next.handle(req).pipe(
tap(event => { tap(event => {
console.log(`in the tap for ${url} ${event instanceof HttpResponse}`);
// There may be other events besides the response. // There may be other events besides the response.
if (event instanceof HttpResponse) { 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}`);
} }
}) })
); );
Expand Down

0 comments on commit 2bde813

Please sign in to comment.