DEV Community

Client-side caching with Angular

Mauro Garcia on November 03, 2019

The time it takes our applications to show useful information for our users has a great impact on the user experience. That's why I think it's our ...
Collapse
 
kdfemi profile image
MiniSoda

Correct me i i am wrong your cached data is served but no request is made to check if there’s any update to the cached version

Collapse
 
mauro_codes profile image
Mauro Garcia

If you want to ignore the cached version, you just need to call the remove method of the cache service. Then, the http-client will return null after trying to get the data from cache and will fetch new data from the server

Collapse
 
kdfemi profile image
MiniSoda

I don’t mean ignoring the cached version, i feel the user should be served the cached version first the a request to the server should also be sent instead of the cached version only served without making any request to the serve. I don’t know if it an efficient approach thou

Thread Thread
 
mauro_codes profile image
Mauro Garcia

I think that what you say may be or may not be the best solution based on your requirements. Example: maybe you are not working with critical information, or information is not updated regularly... Or maybe you have a slow server and you want to optimize resource usage...in those cases I think it's totally OK to prevent requests for a couple of minutes.

But if you need to always get the newest, what you are saying is perfectly valid. You can show the cached data first and, under the hood, fetch for new data and show a refresh button when there is new data to display

Thread Thread
 
kdfemi profile image
MiniSoda

This is awesome, i will always put this in mind

Collapse
 
laccastello profile image
Lucas Accastello

Thanks for share Mauro. I think that your solution is clean and minimalist. I like it so much.

Collapse
 
mauro_codes profile image
Mauro Garcia

Thanks for your feedback Lucas! 😄

Collapse
 
gavinmagnus profile image
gavinmagnus

Thanks for the article. I'm wondering about the situation when two separate components are requesting the same data at the same time. This way we would make two identical calls, because we cache only received data. Did you consider adding f.e. an additional, short-living cache for pending requests, so that any additional call would wait for the pending to finish instead?

Collapse
 
ravavyr profile image
Ravavyr

There is a use for localStorage, but can we just call it that instead of "Client-side caching" ? It's localStorage, and it's also limited space, so with "caching" people will assume you mean it can store the entire site, which in my cases it cannot.

Collapse
 
ribizlim profile image
Mark Magyarodi

I don't see any reason to cache HTTP requests other than GET. For any backend modification the server needs to be contacted...

Collapse
 
mauro_codes profile image
Mauro Garcia

That's right! Maybe I could remove cacheMins from HttpOptions and add an additional param only for get requests.

Collapse
 
pavodev profile image
Ivan Pavic

Nice and clean solution! The customer of a project I'm working on has requested a caching system and I think I will try to implement this one!

Collapse
 
mauro_codes profile image
Mauro Garcia

Nice to hear that! I also have a mechanism in place to clean all cached queries every time I release a new version of my angular apps. I'm gonna be sharing my approach this week.

Collapse
 
vishwamlr profile image
Vishwam Sirikonda

here care should be taken we construct url appending httpParams encoded, there can be cases like /products?page=2