jmdobry / angular-cache

angular-cache is a very useful replacement for the Angular 1 $cacheFactory.
http://jmdobry.github.io/angular-cache
MIT License
1.39k stars 156 forks source link

Why does angular-cache ignore capacity configuration? #210

Closed valnub closed 8 years ago

valnub commented 8 years ago

I use angular-cache with localStorage to save articles locally in html5. The documentation states that there is an option called "capacity":

capacity

Maximum number of items a cache can hold. Adding more items than the capacity will cause the cache to operate like an LRU cache, removing the least recently used items to stay under capacity. Default: Number.MAX_VALUE.

Since I only want to store a max of 20 articles, I configure like this:

CacheFactory('articleCache', {
  storagePrefix : 'my.news.',
  capacity : 20,
  storageMode: 'localStorage'
});

And when I want to get data, I simply do:

$http.get(url, {cache: CacheFactory.get('articleCache')});

After using my app for a while I suddenly noticed that it stopped working because localStorage limit quota was reached. When I printed console.log(localStorage), I saw that there were a lot more than just 20 articles stored to localStorage. So, for some reason angular-cache does not delete the oldest items although the documentation promised the opposite.

So, did I do something wrong or is there a bug in angular-cache?