The angular-cache can be set up like this:
app.service('myService', function ($angularCacheFactory) {
// This cache will sync itself with localStorage if it exists, otherwise it won't. Every time the
// browser loads this app, this cache will attempt to initialize itself with any data it had
// already saved to localStorage (or sessionStorage if you used that).
var myAwesomeCache = $angularCacheFactory('myAwesomeCache', {
maxAge: 900000, // Items added to this cache expire after 15 minutes.
cacheFlushInterval: 3600000, // This cache will clear itself every hour.
deleteOnExpire: 'aggressive', // Items will be deleted from this cache right when they expire.
storageMode: 'localStorage' // This cache will sync itself with `localStorage`.
});
});
From what I understand if the storageMode is set to 'localStorage' then it handles backing up to localstorage itself.
I am already using the angular LocalStorageModule for other things.
Is there any advantage in me setting up a localStoragePolyfill and using the LocalStorageModule?
I would say, that the most powerfull usage of the Cache
cooperating with LocalStorage
could be found here: .put(key, value, options)
As you can see, the third parameter, is the options
, the setting for this key and value pair, not for the whole cache instance.
So we can call it like this
myAwesomeCache.put('someItem'
, 'someValue'
, { storageMode: 'localStorage', storageImpl: myLocalStoragePolyfill });
where myLocalStoragePolyfill
is our wrapper of the local storage? or we can just use the built-in handler and pass it like { storageMode: 'localStorage' }
So, having this, what is the real advantage? we can cache some really stable, constant settings (if there are any). An example could be some Metadata, complex configuration for the application, which can hardly change.
So, in case, that we know, that something is almost static, we do have simple way how to use standard cache, while improving peformance...
NOTE: local storage is not the same as memory cache. It stores a JSON type of objects. No methods! just string representation