加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – angular-cache无法按预期工作

发布时间:2020-12-17 07:26:21 所属栏目:安全 来源:网络整理
导读:我试图将 angular-cache模块用于我的项目,但不确定它是否正常工作.以下是代码 – angular.module('TrackerApp').factory('TrackerFactory',function ($resource,CacheFactory) { if (!CacheFactory.get('CenterCache')) { console.log('cache does not exist
我试图将 angular-cache模块用于我的项目,但不确定它是否正常工作.以下是代码 –
angular.module('TrackerApp').factory('TrackerFactory',function ($resource,CacheFactory) {
    if (!CacheFactory.get('CenterCache')) {
        console.log('cache does not exists');
        CacheFactory.createCache('CenterCache',{
            maxAge: 5 * 60 * 1000,deleteOnExpire: 'aggressive'
        });
    }
    var centerCache = CacheFactory.get('CenterCache');
    var CenterClass = $resource(_ServiceBaseURL + 'api/Center/:id',{},{
        query: {
            method: 'GET',cache: centerCache,isArray:true
        }
    });
    CenterClass.GetMultipleAsObject = function () { return this.query(); };
    return {
        CenterClass: CenterClass
    };
});

在加载应用程序时,它会在控制台中打印“缓存不存在”的消息,它会在本地存储中创建缓存.
它创建了本地存储的关键 –

“angular-cache.caches.CenterCache.keys” = [http://localhost/Services/Tracker/api/Center]

另一个关键是创建

“angular-cache.caches.CenterCache.data.http:// localhost/Services/Tracker/api/Center” = mystoredvalue

问题 –

>在页面刷新(f5),我确实看到再次打印控制台消息并在http调用中,我可以看到“中心”信息正在下载,它不是从缓存中挑选的.
>在从一个页面导航到另一个页面时,我看不到控制台消息被打印.但我确实看到“中心”api被调用.数据正在网络选项卡中下载.它应该从缓存中选择.

我觉得它不是从缓存中挑选数据.我错过了什么吗?

也许你选择使用这种结构;
angular.module('cacheApp',[]).
controller('CacheCtrl',['$scope','$cacheFactory',function($scope,$cacheFactory) {
  $scope.keys = [];
  // get cacheFactory
  $scope.cache = $cacheFactory('CenterCache');
  // Custom put
  // call function key and value
  $scope.put = function(key,value) {

  // Control cache
    if (angular.isUndefined($scope.cache.get(key))) {
      $scope.keys.push(key);
    }

   // Fill cache
    $scope.cache.put(key,angular.isUndefined(value) ? null : value);
  };
}]);

如果你想使用cookieStore

angular.module('cookieStoreExample',['ngCookies'])
.controller('ExampleController',['$cookieStore',function($cookieStore) {

  // Fill cookie
  $cookieStore.put('CenterCache',yourObject );


  // Get cookie
  var favoriteCookie = $cookieStore.get('CenterCache');

  //If you want
  // Removing a cookie
  $cookieStore.remove('CenterCache');
}]);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读