DEV Community

Cover image for LocalStorage wrapper service in Angular
Ayyash
Ayyash

Posted on

LocalStorage wrapper service in Angular

Browser Storage is accessible in browser platform only, and it is just one way to cache in browser. Thus we should avoid accessing it directly in code, wrapping it with internal service, allows us to control it, or even replace it. In this post, let us create a Storage Service in Angular, and organize it to better control what goes into localStorage.

The benefits we want to cover:

  • Protect direct access to a web class, that could change in the future
  • Allow different implementations. Here is a short read about Browser storage limits and eviction criteria, to get you dreaming about other solutions
  • Allow different server platform implementation
  • Unify naming for better debugging
  • Control expiration and force reset of cache

The service

The basic service is a wrapper of the window localStorage, looks too innocent, but we will build on top of it as we go along.

Continue reading on Sekrab Garage.

RESOURCES

RELATED POSTS

Top comments (0)