search

Home  >  Q&A  >  body text

angular.js - angularjs $cacheFactory capacity 怎么理解


怎么理解图中的话,最好有demo

自己写了个demo超过了限制的capacity:3但是每次缓存cache.info()打印出来的size都一样。另外$cacheFactory(key,[option])中的key只能是一个字符串吗,如何一次定义多个缓存对象

过去多啦不再A梦过去多啦不再A梦2744 days ago531

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-05-15 17:14:19

    capacity is equivalent to specifying a maximum usable capacity for the cache.
    For a simple example, if you have 3 buckets (capacity=3) and you fill the first bucket of water (put), then it still has 3 buckets. Fill another bucket of water until the fourth bucket of water is filled. Since there are only 3 buckets, it is obvious that this bucket of water cannot be filled. What should I do? Dump the first bucket of water and use this bucket to hold the fourth bucket of water (cache strategy: LRU)
    You can take a look at the implementation of $cacheFatory, you should have a deeper understanding

    reply
    0
  • 某草草

    某草草2017-05-15 17:14:19

    capacity involves LRU (Least Recenlty Used, least recently used) cache, such as:

    var lru = $cacheFactory('lru', {capacity: 20});
    
    // $http请求
    $http.get('/api/users.json', {cache: lru}).then(function(data){});

    Now, the latest 20 requests will be cached. The 21st request will cause LRU to remove the older request from the cache.

    We can also set a default cache for all $http requests through the application's .config() function, that is, the configuration phase:

    angular.module('myApp', [])
    .config(function($httpProvider, $cacheFactory) {
        $httpProvider.defaults.cache = $cacheFactory('lru', {
            capacity: 20
        });
    });

    reply
    0
  • Cancelreply