DEV Community

Emad Hassan Khan for AWS Community Builders

Posted on

Cache API calls in Angular using Interceptor

Developing web applications can be a challenging task, and among the many issues we face, one of the most common ones is the repetitive API calls that occur every time a component initializes. This can lead to a great deal of pain and frustration, as well as excessive calls that can slow down the performance of your application.

But fear not, there is a strategy that can help you save multiple API hits for the same endpoint. By making use of an Interceptor in your Angular application, you can streamline your code and improve the overall efficiency of your web app.

In the code snippet below, we will demonstrate how to implement this strategy by simply mentioning the endpoints we want to cache and letting the interceptor do its job. This will not only save you time and energy, but also make your application more responsive and user-friendly.

So without further ado, let’s dive in and discover how to implement this powerful tool in your web development process.

Here’s the code:

Image description

After the interceptor is created don’t forget to reference it in app.module.ts

Image description

Alright, lets catch the code explanation:

We have an Angular interceptor called ApiCacheInterceptor that can cache HTTP responses from certain API endpoints. The interceptor uses the HttpInterceptor class and its intercept() method to handle all HTTP requests and responses.

When a request is intercepted, the interceptor checks if the request endpoint is in a predefined set of endpoints to cache. If the endpoint is in the set, the interceptor checks if the response is already cached. If the response is cached, the interceptor returns the cached response. If the response is not cached, the interceptor sends the request to the server, caches the response, and returns the response to the original caller.

The ApiCacheInterceptor class defines two private properties: cache, which is a Map object that stores cached responses, and endpointsToCache, which is a Set object that lists the endpoints to cache. The intercept() method uses these properties to implement the caching logic.

If you found this article informative and helpful, make sure to follow my account for more insightful and useful posts on various topics related to web development, technology, and much more. By following, you’ll be the first to know about my latest articles, tips, and tricks, and stay up to date with the latest trends in the industry.

I am dedicated to providing high-quality content that is easy to understand and helpful for readers of all levels of expertise. Whether you’re a beginner or an experienced developer, you’ll find my posts to be informative, engaging, and relevant to your interests.

Top comments (3)

Collapse
 
emadkhanqai profile image
Emad Hassan Khan

Very interesting article, MUST READ!

Collapse
 
pinnacle_20 profile image
Shivanand

Good read post!

Collapse
 
manuelromano1598 profile image
ManuelRomano1598

Make sure to control the type of the http call you are caching! A POST call on '/resources' won't get sended if you have already cached a GET call on '/resources'