Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

angular cache interceptor

import {
  HttpEvent,
  HttpHandler,
  HttpInterceptor,
  HttpRequest,
  HttpResponse,
} from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable, of, tap } from 'rxjs';

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

  intercept(
    request: HttpRequest<any>,
    next: HttpHandler
  ): Observable<HttpEvent<any>> {
    if (request.method !== 'GET') {
      return next.handle(request);
    }
    const cachedResponse = this.cache.get(request.url);
    if (cachedResponse) {
      return of(cachedResponse);
    }

    return next.handle(request).pipe(
      tap((response) => {
        if (response instanceof HttpResponse) {
          this.cache.set(request.url, response);
        }
      })
    );
  }
}
Comment

how to delete cache in angular using http interceptor?

import { Injectable } from '@angular/core';
import { environment } from 'src/environments/environment';

@Injectable({ providedIn: 'root' })
export class TimerService {
  private isTimerStarted = false;
  public dateNow = new Date();
  public dDay = new Date();
  milliSecondsInASecond = 1000;
  hoursInADay = 24;
  minutesInAnHour = 60;
  SecondsInAMinute = 60;

  public timeDifference: any;

  constructor() {}

  private getTimeDifference() {
    this.timeDifference = this.dDay.getTime() - new Date().getTime();    
  }
  
  startTimer() {
    if (!this.isTimerStarted) {
   
      this.dDay.setMinutes(
        this.dDay.getMinutes() + 30 //+environment.cacheTimeInMinutes // you can put this time in environment file to make it configurable.
      );
      this.getTimeDifference();
      this.isTimerStarted = true;
    }
  }

  resetTimer() {
    this.isTimerStarted = false;
    this.getTimeDifference();
  }

  getRemainingTime(): number {
    this.getTimeDifference();
    return this.timeDifference;
  }
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: nodejs express mongodb boilerplate 
Javascript :: mdn javascript console.log(Math.random()); 
Javascript :: find duplicate objects in array js 
Javascript :: javascript slider elementor 
Javascript :: discord-buttons collector 
Javascript :: function last character return 
Javascript :: easyui treegrid check if row exists 
Javascript :: sempole reguler expretion 
Javascript :: convert an array to other array 
Javascript :: discord js kick command 
Javascript :: promise.all to send emails 
Javascript :: Javacript code that delays, based on Milliseconds 
Javascript :: 2495016599 
Javascript :: amcharts 3d column chart export 
Javascript :: color blur in echart 
Javascript :: refresh mathjax 
Javascript :: ceil function js but 1.1 as 2 
Javascript :: como fazer um array em javascript stackoverflow 
Javascript :: how to rmeove white space in a string with jquery 
Javascript :: How to determine dropdown should show upward or downward direction 
Javascript :: geojson polygon mongoose 
Javascript :: how to square a number in html 
Javascript :: android MediaController audio example 
Javascript :: Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse 
Javascript :: concatenate state with previous state in react redux 
Javascript :: what is react headroom 
Javascript :: js variable delete during loading page 
Javascript :: Callback after forEach completed 
Javascript :: extending classes javascript 
Javascript :: vscode nestjs ignore node_modules 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =