【问题标题】:How to define expiration time on Cache in Angular $cacheFactory如何在 Angular $cacheFactory 中定义缓存的过期时间
【发布时间】:2017-02-23 16:10:40
【问题描述】:

我阅读了有关 Angular '$cacheFactory' 的信息,但找不到任何有关为缓存内容设置到期日期的文档。

如果我想将所有 GET 请求缓存 30 秒,如何在“$cacheFactory”中定义它,或者我是否需要自己扩展功能。

【问题讨论】:

    标签: angularjs caching


    【解决方案1】:

    TTL 1h,见下例

    添加工厂:

    .factory('cacheInterceptor', ['$cacheFactory', function($cacheFactory) {
      var http_ttl_cache = {};
      return {
        request: function(config) {
          var N;
          if (config.timeToLive) {
            config.cache = true;
            N = config.timeToLive;
            delete config.timeToLive;
            if (new Date().getTime() - (http_ttl_cache[config.url] || 0) > N) {
              $cacheFactory.get('$http').remove(config.url);
              http_ttl_cache[config.url] = new Date().getTime();
            }
          }
          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
    })
    

    【讨论】:

    • 这个例子不处理 params 内部的 option 对象,但它可以简单地修复。
    • 这是一个干净又不错的解决方案!谢谢。我使用 $timeout 而不是检查日期来获得更好的 tll 与普通缓存语义,但这只是我的偏好
    • @Yaron,您能否提供您的超时解决方案。谢谢
    【解决方案2】:

    我也遇到了这个问题。默认的 $cacheFactory 没有生存时间 (TTL)。

    您需要自己实现。但在此之前,您可以环顾四周,看看是否有人已经这样做了:

    这个看起来很完整 - http://jmdobry.github.io/angular-cache/

    如果您真的想实现自己的解决方案(通过实现自己的 $cacheFactory)并需要帮助,请随时提出。

    希望它能给你一些线索。

    【讨论】:

    • 谢谢。你知道cacheFactory是用localStorage还是cookies来保存数据?
    • @AdrianE 我知道,但它已经消失了。我所知道的是,您应该编写自己的 cacheFactory,它提供来自 $cacheFactory 的工厂(以便您保持系统完整)以及一些实现 TTL 的功能/逻辑。
    【解决方案3】:

    我认为@miukki 的回答很棒。将我的修改添加到@Vil 的请求中

    我修改了 'cacheInterceptor' 的 'request' 函数以使用 $timeout 而不是依赖 Date。它允许 TTL 对请求更加全局,所以如果一个请求设置了 TTL 而第二个没有设置但数据仍在缓存中,它仍然会被使用。

    .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;
        }
      };
    }])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-10
      • 2022-01-05
      相关资源
      最近更新 更多