3

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

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

然后我通过像这样传递它来调用我的控制器内部的缓存对象。但由于某种原因,数据不是持久的。每次我重新加载页面时,缓存又是空的。

有任何想法吗?我错过了什么?

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

1 回答 1

2

数据不是持久的有很好的理由:$cacheFactory缓存只不过是普通 JS 对象的薄包装。

您可以检查以确保该服务只执行简单的 LRU 算法。

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

于 2016-05-26T20:25:55.680 回答