Javascript 如何在angular js中获取更新的缓存数据

Javascript 如何在angular js中获取更新的缓存数据,javascript,angularjs,angular-cache,Javascript,Angularjs,Angular Cache,我正在使用模块缓存$http数据。如果缓存数据被更新,我想调用$http服务。我遇到的问题是,在我不手动清除缓存之前,此代码只返回缓存数据 //controller.js angular.module('adminPanelApp').controller('recordController',recordController); function recordController($scope,userService) { $scope.title='FeedBac

我正在使用模块缓存
$http
数据。如果缓存数据被更新,我想调用
$http
服务。我遇到的问题是,在我不手动清除缓存之前,此代码只返回缓存数据

//controller.js   
angular.module('adminPanelApp').controller('recordController',recordController);

    function recordController($scope,userService) {
      $scope.title='FeedBacks From Users';  //This is Title to be sset to page
      var promise = userService.getRecord(1);
      promise.then(function (response) {
        $scope.users = response;
      });
    }

    //service.js

    angular.module('adminPanelApp').service('userService', ['$http', '$q','CacheFactory', function ($http, $q,CacheFactory) {
      CacheFactory('dataCache', {
        cacheFlushInterval: 60, // This cache will clear itself every hour
        deleteOnExpire: 'aggressive', // Items will be deleted from this cache when they expire
        storageMode:'localStorage'
      });

      return {
        getRecord: function (id) {
          var deferred = $q.defer();
          var start = new Date().getTime();
          var dataCache = CacheFactory.get('dataCache');
          if (dataCache.get(id)) {
            deferred.resolve(dataCache.get(id));
          } else {
            $http.get('http://54.86.64.100:3000/api/v1/user/feedback-all', +id).success(function (data) {
              dataCache.put(id, data);
              console.log('time taken for request: ' + (new Date().getTime() - start) + 'ms');
              deferred.resolve(data);
              dataCache.get(id, data);
            });
          }
          return deferred.promise;
        }};//
    }]);//end of service

第一种方法-使用$resource自动执行:

$resource将自动为您管理缓存,因此无需强制清除缓存。其思想是,如果您有一个可以查询的资源,那么该查询响应将被缓存,但是如果您为同一资源保存了某些内容,那么以前缓存的数据必须是无效的,因此它将为您清除

第二种方法-基于事件强制缓存

在某些情况下,您可能不知道数据正在更新,您可能希望基于特定操作强制更新。您可以通过调用remove方法来实现这一点。就你而言:

dataCache.remove(key);

请定义“如果缓存数据已更新”,这是否意味着您在本地更新了信息,或者服务器上的数据已更新。您应该使用处理程序重新获取数据。