【问题标题】:When using angular-cache is there any advantage to using the Angular LocalStorageModule?使用 Angular 缓存时,使用 Angular LocalStorageModule 有什么优势吗?
【发布时间】:2014-02-12 17:02:43
【问题描述】:

角度缓存可以这样设置:

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`.
    });
});

据我了解,如果 storageMode 设置为“localStorage”,那么它会自行处理备份到 localstorage。

我已经将 Angular LocalStorageModule 用于其他事情。

我设置 localStoragePolyfill 并使用 LocalStorageModule 有什么好处吗?

【问题讨论】:

    标签: javascript angularjs angular-local-storage angular-cache


    【解决方案1】:

    我想说,CacheLocalStorage 合作的最强大用法可以在这里找到:.put(key, value, options)

    如您所见,第三个参数是options,这个键值对的设置,不是整个缓存实例的设置。

    所以我们可以这样称呼它

    myAwesomeCache.put('someItem'
      , 'someValue'
      , { storageMode: 'localStorage', storageImpl: myLocalStoragePolyfill });
    

    myLocalStoragePolyfill 是我们本地存储的包装器在哪里?或者我们可以使用内置的处理程序并像{ storageMode: 'localStorage' }一样传递它

    那么,拥有这个,真正的优势是什么?我们可以缓存一些非常稳定、恒定的设置(如果有的话)。一个示例可能是一些元数据,应用程序的复杂配置,几乎无法更改。

    所以,万一,我们知道,某些东西几乎是静态的,我们确实有简单的方法来使用标准缓存,同时提高性能...

    注意:本地存储与内存缓存不同。它存储 JSON 类型的对象。没有方法!只是字符串表示

    【讨论】:

    • 我想你可能对我的问题感到困惑。或者我对你的回答感到困惑。我已经在使用“storageMode:'localStorage'”。我想知道的是使用 Angular LocalStorageModule 是否有任何优势?我可以从我的应用程序中删除它,因为角度缓存似乎可以满足我的一切需求吗?
    • @Melina 嗯,这里我试图解释本地存储(无缓存)stackoverflow.com/a/21229481/1679310 的不同用法。换句话说,我想说,这些概念是不同的。他们可以住在一起,但我确实以不同的方式使用它们。静态数据(如元数据或模板,或者最重要的是:用户自定义设置)不必每次都重新加载。他们应该可以准备好下次使用......然后本地存储获胜。虽然动态数据只能缓存...
    • "我们可以只使用内置处理程序并像 { storageMode: 'localStorage' } 一样传递它 - 现在你说那是需要的:github.com/grevory/angular-local-storage 或者你可以做所有需要的事情仅使用角度缓存?
    • @Melina,不要误会我的意思。我觉得你说的很有道理!真的 ;) 我会说是的,增强缓存可能是唯一的解决方案。唯一的 API……但我不确定,从设计、维护的角度来看,这是否可行。因为以防万一,当我想明确使用 LocalStarage... 但我不得不使用 缓存的 API .. 不太好... 我猜你知道我的意思;)..但是是的..如果您只想使用高级缓存,为什么不呢。其他话:I see benefits in using cache cooperating with local storage. But I see benefits to use them separately as well
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-09
    • 2011-12-05
    • 2019-11-29
    • 1970-01-01
    • 2013-04-08
    • 1970-01-01
    • 2015-07-20
    相关资源
    最近更新 更多