angular.js - How to understand angularjs $cacheFactory capacity
过去多啦不再A梦
过去多啦不再A梦 2017-05-15 17:12:19
0
2
562


How to understand the words in the picture, it is best to have a demo

I wrote a demo that exceeded the capacity limit: 3, but the size printed by cache.info() is the same every time. In addition, can the key in $cacheFactory(key,[option]) only be a string? How to define multiple cache objects at one time

过去多啦不再A梦
过去多啦不再A梦

reply all(2)
大家讲道理

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

某草草

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
    });
});
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template