-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a client side cache system, need more data before writing rules
- Loading branch information
vvmk
committed
Sep 3, 2018
1 parent
6656981
commit 3b8d1b5
Showing
3 changed files
with
87 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpResponse } from '@angular/common/http'; | ||
import { Observable, of } from 'rxjs'; | ||
import { tap } from 'rxjs/operators'; | ||
|
||
import { HttpCacheService } from './http-cache.service'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class CacheInterceptor implements HttpInterceptor { | ||
|
||
constructor( | ||
private cache: HttpCacheService, | ||
) {} | ||
|
||
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | ||
|
||
console.log('url: ', req.url); | ||
console.log('method: ', req.method); | ||
console.log('body: ', req.body); | ||
|
||
// SET UP AND TESTING ONLY | ||
// invalidate the cache and return if anything but a GET | ||
if (req.method !== 'GET') { | ||
console.log('invalidate chache'); | ||
this.cache.invalidateCache(); | ||
return next.handle(req); | ||
} | ||
|
||
// attempt to receive cached response | ||
const cachedResponse: HttpResponse<any> = this.cache.get(req.url); | ||
|
||
// return cached response | ||
// NO next.handle!!!!! | ||
if (cachedResponse) { | ||
console.log('returning cached response: ', cachedResponse.url); | ||
console.log(cachedResponse); | ||
return of(cachedResponse); | ||
} | ||
|
||
// send request to server and add response to cache | ||
return next.handle(req).pipe( | ||
tap(event => { | ||
if (event instanceof HttpResponse) { | ||
console.log('Adding item to chache: ', req.url); | ||
this.cache.put(req.url, event); | ||
} | ||
}) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { HttpResponse } from '@angular/common/http'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class HttpCacheService { | ||
|
||
private requests: any = {}; | ||
|
||
constructor() { } | ||
|
||
put(url: string, response: HttpResponse<any>): void { | ||
this.requests[url] = response; | ||
} | ||
|
||
get(url: string): HttpResponse<any> | undefined { | ||
return this.requests[url]; | ||
} | ||
|
||
invalidateUrl(url: string): void { | ||
this.requests[url] = undefined; | ||
} | ||
|
||
invalidateCache(): void { | ||
this.requests = {}; | ||
} | ||
} |