设置缓存请求的数据:只有请求URL不变,就不会重新发请求

angularjs 缓存

 

设置带定时时间的缓存:

 

.factory('cacheInterceptor', ['$cacheFactory', '$timeout', function($cacheFactory, $timeout) {
  var ttlMap = {};
  return {
    request: function(config) {
      if (config.ttl) {
        var ttl = config.ttl;
        delete config.ttl;
        config.cache = true;

        // If not in ttlMap then we set up a timer to delete, otherwise there's already a timer.
        if (!ttlMap[config.url]) {
          ttlMap[config.url] = true;
          $timeout(ttl)
          .then(function() {
            $cacheFactory.get('$http').remove(config.url);          
            delete ttlMap[config.url];
          });
        }
      }
      return config;
    }
  };
}])
.config(['$routeProvider', '$httpProvider', function($routeProvider, $httpProvider) {

  $httpProvider.interceptors.push('cacheInterceptor');
$http.get('/permissions.json', {timeToLive: Constant.timeToLive}).then(function(result){
.constant('Constant', {
  url: {
    logout: '/auth/logout'
  },
  timeToLive: 60*60*1000
})

 

相关文章:

  • 2022-12-23
  • 2021-07-22
  • 2021-10-10
  • 2022-01-11
  • 2021-08-19
  • 2022-01-06
  • 2021-12-14
  • 2022-12-23
猜你喜欢
  • 2022-01-07
  • 2021-12-31
  • 2021-11-22
  • 2022-02-26
  • 2021-08-04
  • 2021-07-08
  • 2022-12-23
相关资源
相似解决方案