angularjs - $cacheFactory 在页面重新加载时删除我的缓存

标签 angularjs caching cachefactory

所以我有一个简单的工厂来创建缓存对象

.factory('jsonCache',function($cacheFactory){
    console.log("Creating cache");
    return $cacheFactory('weatherCache');
});

然后我通过像这样传入它来像这样在我的 Controller 中调用该缓存对象。但由于某种原因,数据不是持久的。每次我重新加载页面时,缓存再次为空。

有任何想法吗?我错过了什么?
.controller('citiesListCtrl',function($scope,$http,$filter,jsonCache){

    jsonCache.get('weatherCache');
    console.log(jsonCache); <----- EMPTY

    $http.get(url)
        .then(function(response) {

          jsonCache.put('weatherCache', response.data.records);
          console.log(jsonCache); <--- HAS DATA
    });
)}

最佳答案

数据不是持久的,原因很充分:$cacheFactory cache 只不过是一个围绕普通 JS 对象的薄包装。

您可以查看 the source确保服务只执行简单的 LRU 算法。

对于数据持久化,使用持久化存储(可能是 angular-local-storageangular-localForage )。 angular-cache是内置 $cacheFactory 的另一个替代品that supports persistence .

关于angularjs - $cacheFactory 在页面重新加载时删除我的缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37469725/

相关文章:

javascript - 单击按钮时在两个功能之间切换。

angularjs - 从 Angular cacheFactory 获取 key

angularjs - 使用 jasmine 对 karma 运行超过 10 次测试导致 : "ERROR: Some of your tests did a full page reload!"

javascript - 更改非事件页面标题

javascript - jQuery $.ajaxSetup header 适用于所有 ajaxRequest,即使被覆盖

php - Laravel Cache Facade - set 与 put

internet-explorer - 为什么 Chrome 和 IE 永远不会发送 If-None-Match 而其他浏览器呢?

javascript - 有没有办法获取 Angular $cacheFactory 中的所有键?

javascript - Angular : Provider must define factory method