| 6446 | var caches = {}; |
| 6447 | |
| 6448 | function cacheFactory(cacheId, options) { |
| 6449 | if (cacheId in caches) { |
| 6450 | throw minErr('$cacheFactory')('iid', 'CacheId \'{0}\' is already taken!', cacheId); |
| 6451 | } |
| 6452 | |
| 6453 | var size = 0, |
| 6454 | stats = extend({}, options, {id: cacheId}), |
| 6455 | data = createMap(), |
| 6456 | capacity = (options && options.capacity) || Number.MAX_VALUE, |
| 6457 | lruHash = createMap(), |
| 6458 | freshEnd = null, |
| 6459 | staleEnd = null; |
| 6460 | |
| 6461 | /** |
| 6462 | * @ngdoc type |
| 6463 | * @name $cacheFactory.Cache |
| 6464 | * |
| 6465 | * @description |
| 6466 | * A cache object used to store and retrieve data, primarily used by |
| 6467 | * {@link $http $http} and the {@link ng.directive:script script} directive to cache |
| 6468 | * templates and other data. |
| 6469 | * |
| 6470 | * ```js |
| 6471 | * angular.module('superCache') |
| 6472 | * .factory('superCache', ['$cacheFactory', function($cacheFactory) { |
| 6473 | * return $cacheFactory('super-cache'); |
| 6474 | * }]); |
| 6475 | * ``` |
| 6476 | * |
| 6477 | * Example test: |
| 6478 | * |
| 6479 | * ```js |
| 6480 | * it('should behave like a cache', inject(function(superCache) { |
| 6481 | * superCache.put('key', 'value'); |
| 6482 | * superCache.put('another key', 'another value'); |
| 6483 | * |
| 6484 | * expect(superCache.info()).toEqual({ |
| 6485 | * id: 'super-cache', |
| 6486 | * size: 2 |
| 6487 | * }); |
| 6488 | * |
| 6489 | * superCache.remove('another key'); |
| 6490 | * expect(superCache.get('another key')).toBeUndefined(); |
| 6491 | * |
| 6492 | * superCache.removeAll(); |
| 6493 | * expect(superCache.info()).toEqual({ |
| 6494 | * id: 'super-cache', |
| 6495 | * size: 0 |
| 6496 | * }); |
| 6497 | * })); |
| 6498 | * ``` |
| 6499 | */ |
| 6500 | return (caches[cacheId] = { |
| 6501 | |
| 6502 | /** |
| 6503 | * @ngdoc method |
| 6504 | * @name $cacheFactory.Cache#put |
| 6505 | * @kind function |