Local storage vs AngularJS $cacheFactory

JavascriptAngularjsLocal Storage

Javascript Problem Overview


I have a problem of storing a lot of client-side data, and I can't decide which way is better. Now I'm using AngularJS's cacheFactory, it works fine, but all data reloads with a new session. Is it worth to use local storage instead?

Javascript Solutions


Solution 1 - Javascript

If your goal is to store client-side and persistent data, you can't use the $cacheFactory, which just caches the data for the current session.

One solution is to use the new local storage API. This awesome Angular module makes all the dirty job for you, and even falls back to cookies for old browsers!

Solution 2 - Javascript

An alternative solution is http://jmdobry.github.io/angular-cache/ which works well with ngResource and can also easily be configured to sync to localStorage, so requests don't need to be re-done after page refresh.

$resource('my/kewl/url/:key', { key: '@key' }, {
  'get': { method: 'GET', 
           cache: $angularCacheFactory('MyKewlResourceCache', { 
                                          storageMode: 'localStorage' })
         }
});

Solution 3 - Javascript

$cacheFactory seems to be clearly NOT your solution, because as Blackhole said, the cache will be cleared each time session expires. $cacheFactory is just a memcache implementation the Angular way.

angular-cache is just an helper API, basically it adds option to $cacheFactory and one of this option is to store cache into persistent storage (like localStorage).

So if you want to store data in persistent storage you can use use one of the module available like https://github.com/grevory/angular-local-storage">angular-local-storage</a> or use https://docs.angularjs.org/api/ngCookies/service/$cookieStore">$cookieStore</a> but it will create cookies...

Solution 4 - Javascript

Another angular module that does the job : https://github.com/jmdobry/angular-cache

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRasalomView Question on Stackoverflow
Solution 1 - JavascriptBlackholeView Answer on Stackoverflow
Solution 2 - JavascripttmaximiniView Answer on Stackoverflow
Solution 3 - JavascriptGutView Answer on Stackoverflow
Solution 4 - JavascripthugsbrugsView Answer on Stackoverflow