Skip to content

Commit

Permalink
feat(lib): add inline cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Netanel Basal committed Jan 8, 2020
1 parent 7096375 commit 3f0935e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
9 changes: 7 additions & 2 deletions projects/ngneat/http-cache/src/lib/httpCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ export class HttpCacheFacade {
this.ttlManager.set(request, ttl);
}

isCached(request: HttpRequest<any>) {
return this.storage.has(request) && this.ttlManager.isValid(request);
validate(request: HttpRequest<any>) {
const has = this.storage.has(request);
const isValid = this.ttlManager.isValid(request);
if (has && isValid) return true;

this.storage.delete(request);
return false;
}

get(request: HttpRequest<any>) {
Expand Down
27 changes: 11 additions & 16 deletions projects/ngneat/http-cache/src/lib/httpCacheInterceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,28 @@ export class HttpCacheInterceptor implements HttpInterceptor {
constructor(private cacheFacade: HttpCacheFacade) {}

intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const allowCacheGlobal = this.cacheFacade.isCacheable(request);
const allowCacheLocal = request.params.get('cache$') as any;
const isRequestCacheable = this.cacheFacade.isCacheable(request);

const filteredParams = filterParams(request);
const clone = request.clone({
params: new HttpParams({
fromObject: filteredParams
})
});

// allowCacheLocal !== false => because it can be null which means we allow it
if (allowCacheLocal === true || (allowCacheGlobal && allowCacheLocal !== false)) {
const ttl = request.params.get('ttl$');

if (this.cacheFacade.isCached(request)) {
if (isRequestCacheable && !!request.params.get('cache$') === true) {
if (this.cacheFacade.validate(request)) {
return of(this.cacheFacade.get(request));
}

const filteredParams = filterParams(request);
const clone = request.clone({
params: new HttpParams({ fromObject: filteredParams })
});

return next.handle(clone).pipe(
tap(event => {
if (event instanceof HttpResponse) {
const ttl = request.params.get('ttl$');
this.cacheFacade.set(request, event, +ttl);
}
})
);
} else {
return next.handle(clone);
}

return next.handle(request);
}
}
15 changes: 11 additions & 4 deletions projects/ngneat/http-cache/src/lib/httpCacheStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export abstract class HttpCacheStorage {
abstract has(request: HttpRequest<any>): boolean;
abstract get(request: HttpRequest<any>): HttpResponse<any>;
abstract set(request: HttpRequest<any>, response: HttpResponse<any>): void;
abstract delete(key: string | RegExp): void;
abstract delete(key: string | RegExp | HttpRequest<any>): void;
}

@Injectable()
Expand All @@ -27,13 +27,20 @@ export class DefaultHttpCacheStorage implements HttpCacheStorage {
this.cache.set(this.keySerializer.serialize(request), response);
}

delete(url: string | RegExp): void {
delete(url: string | RegExp | HttpRequest<any>): void {
let _url = url;

if (url instanceof HttpRequest) {
_url = this.keySerializer.serialize(url);
}

if (typeof url === 'string') {
this.cache.delete(url);
this.cache.delete(_url as string);
return;
}

for (const [key] of Array.from(this.cache)) {
if ((url as RegExp).test(key)) {
if ((_url as RegExp).test(key)) {
this.cache.delete(key);
break;
}
Expand Down

0 comments on commit 3f0935e

Please sign in to comment.