Caching All You Can Cache!

Caching is one of the things you can do to improve your application performance. In JavaScript programming, we have sessionStorageand localStorageAPI to do the caching. sessionStorage is the browser's storage that will be kept only for that session only. So when you close the tab, it will disappear. Meanwhile, localStorage has been more persistent.

When you cache something, you will benefit from it because you don't need to retrieve again some data. But also if you don't use it in a good manner, it will leverage bugs on your system. For instance, you cache User data, but at the same time, someone changes your User data. For this case of course when you open your apps, it will display the wrong output because we still get data from the cache instead of retrieve from a database.

That is why usually I prefer to use sessionStorage because of its nature. I don't need to have a procedure to clear the cache because users will do it automatically when they close the browser. But of course, when that user needs the data, it will retrieve it again which is not so good compared to localStorage.

One thing you must consider before you cache something, you need to know what type of data you can cache. It is very important to know before doing it. If your data always changes all the time. Then you don't need to cache it.

This is API for cache using sessionStorage (you can change it into localStorage if you want to):

https://gist.github.com/temmyraharjo/441bf2a71993e296ec9c590b1cddc532

How to use it:

https://gist.github.com/temmyraharjo/d769a2f3335501875466ca22d3f4d4fb

Leave a comment

Your comment is sent privately to the author and isn't published on the site.