微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

angularjs – 从Angular cacheFactory获取密钥

当我列出$cacheFactory对象时,它有几个方法,但我没有看到实际的键/值缓存.

假设您正在查看$http缓存,$cacheFactory($http)如何获取当前缓存的密钥列表或理想情况下的密钥和值?

解决方法

使用$cacheFactory的装饰器添加getKeys方法

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>


<body ng-app="decorateExample">

  <script>
  function cacheProvider($provide) 
    {
    // monkey-patches $templateCache to have a keys() method
    $provide.decorator('$templateCache',['$delegate',cacheDelegator]);
    }
                                          
  function cacheDelegator($delegate) 
    {
    var keys = [],origPut = $delegate.put;

    $delegate.put = function(key,value) 
      {
      origPut(key,value);
      keys.push(key);
      };

    // we would need cache.peek() to get all keys from $templateCache,but this features was never
    // integrated into Angular: https://github.com/angular/angular.js/pull/3760
    // please note: this is not feature complete,removing templates is NOT considered
    $delegate.getKeys = function() 
         {
         return keys;
         };

    return $delegate;
    }
  
angular.module('decorateExample',[]);
  
angular.module('decorateExample').config(['$provide',cacheProvider]);
</script>

</body>

参考

> AngularJS Issue #3760: Update cacheFactory.js: Added peek() for returning all keys
> Decorate Core Directives in Angular

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐