zoukankan      html  css  js  c++  java
  • angularjs 缓存

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

    设置带定时时间的缓存:

    .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
    })
  • 相关阅读:
    【转载】C++汇编器、连接器
    【转载】vi的使用命令
    JDK,SDK,JRE概念
    iOS 使用xmpp做聊天客户端
    cocopods安装
    用XMPP实现完整Android聊天项目
    xmpp发送文件
    ember.js学习笔记
    html5 drag and drop
    jquery 数组深拷贝
  • 原文地址:https://www.cnblogs.com/web-fusheng/p/7344608.html
Copyright © 2011-2022 走看看