When using angular-cache is there any advantage to using the Angular LocalStorageModule?

与世无争的帅哥 提交于 2019-12-23 18:23:15

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/21286305/when-using-angular-cache-is-there-any-advantage-to-using-the-angular-localstorag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!