所以我有一个简单的工厂来创建缓存对象
.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-storage 或 angular-localForage )。 angular-cache
是内置 $cacheFactory
的另一个替代品that supports persistence .
关于angularjs - $cacheFactory 在页面重新加载时删除我的缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37469725/