NgHttpCaching is an Angular library designed to cache HTTP requests, preventing redundant server calls for the same data. It operates as an HTTP interceptor, automatically caching responses and serving them for subsequent identical requests. The current stable version is 21.0.10, closely aligning with Angular's major version releases (indicated by peer dependency versions and the default cache invalidation strategy). Key differentiators include built-in handling for simultaneous/parallel requests, an automatic garbage collector for cache entries, support for various storage mechanisms (LocalStorage, SessionStorage, MemoryStorage, or custom), and intelligent cache invalidation based on `Cache-Control`/`Expires` headers or automatically for mutation requests (POST, PUT, DELETE, PATCH). It offers granular configuration for cache lifetime, allowed HTTP methods, and custom cache strategies, making it a flexible solution for optimizing network performance in Angular applications.
npm install ng-http-cachingVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to integrate `ng-http-caching` into an Angular standalone application, providing custom configuration for cache lifetime and methods. It shows the basic setup required to enable HTTP request caching through the `provideNgHttpCaching` function and sets up a simple component to make an HTTP request that will be cached.
Consult the `ng-http-caching` release notes for the specific major version you are upgrading to. Ensure peer dependencies on `@angular/common` and `@angular/core` are met. For Angular 15+, adapt to `provideNgHttpCaching()` instead of `NgHttpCachingModule`.
If you require caching to persist across Angular major version upgrades, you can explicitly set the `version` property in `NgHttpCachingConfig` to a static string or a custom versioning scheme. Be cautious, as this might lead to compatibility issues if cached data structures change with Angular versions.
Ensure that `provideHttpClient(withInterceptorsFromDi())` is included in your application's root `providers` array (e.g., in `bootstrapApplication` or `app.config.ts`) when using `provideNgHttpCaching()`.
Migrate from `imports: [NgHttpCachingModule.forRoot(config)]` to `providers: [provideNgHttpCaching(config)]` in your application configuration or component providers, following Angular's standalone component best practices.
Add `provideHttpClient()` (or `provideHttpClient(withInterceptorsFromDi())` if using interceptors) to the `providers` array in your `bootstrapApplication` call or `app.config.ts`.
Ensure both `provideNgHttpCaching()` and `provideHttpClient(withInterceptorsFromDi())` are present in your application's root `providers` array. If you are using NgModules, verify `NgHttpCachingModule.forRoot()` is imported.