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
    })
  • 相关阅读:
    HTTP RFC解析
    Symfony框架的笔记
    Swoole学习总结1
    PHP Socket 学习笔记一
    Js解决解除多次Ajax请求的事件
    Python 操作串口
    python 实现微信自动回复和好友签名分析
    杂谈1
    Vue.js 功课1
    微信API demo
  • 原文地址:https://www.cnblogs.com/web-fusheng/p/7344608.html
Copyright © 2011-2022 走看看