Skip to content

Instantly share code, notes, and snippets.

@Mustafa-Omran
Created February 13, 2023 11:15
Show Gist options
  • Save Mustafa-Omran/40b79793ebb857f4bc93ed26521d06a5 to your computer and use it in GitHub Desktop.
Save Mustafa-Omran/40b79793ebb857f4bc93ed26521d06a5 to your computer and use it in GitHub Desktop.
Caching - Passing Context to HTTP Interceptors
const CACHE_IT = new HttpContextToken<boolean>(() => false);
export function cacheIt() {
return new HttpContext().set(CACHE_IT, true);
}
@Injectable()
export class CacheInterceptor implements HttpInterceptor {
intercept(request: HttpRequest, next: HttpHandler): Observable<HttpEvent> {
if(request.context.get(CACHE_IT)) {
return;
}
return next.handle(request);
}
}
import { cacheIt } from './cache.interceptor';
@Injectable()
export class UsersService {
constructor(private http: HttpClient) {}
getUsers() {
return this.http.get('....', {
context: cacheIt()
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment