| 7003 | var caches = {}; |
| 7004 | |
| 7005 | function cacheFactory(cacheId, options) { |
| 7006 | if (cacheId in caches) { |
| 7007 | throw minErr('$cacheFactory')('iid', 'CacheId \'{0}\' is already taken!', cacheId); |
| 7008 | } |
| 7009 | |
| 7010 | var size = 0, |
| 7011 | stats = extend({}, options, {id: cacheId}), |
| 7012 | data = createMap(), |
| 7013 | capacity = (options && options.capacity) || Number.MAX_VALUE, |
| 7014 | lruHash = createMap(), |
| 7015 | freshEnd = null, |
| 7016 | staleEnd = null; |
| 7017 | |
| 7018 | /** |
| 7019 | * @ngdoc type |
| 7020 | * @name $cacheFactory.Cache |
| 7021 | * |
| 7022 | * @description |
| 7023 | * A cache object used to store and retrieve data, primarily used by |
| 7024 | * {@link $templateRequest $templateRequest} and the {@link ng.directive:script script} |
| 7025 | * directive to cache templates and other data. |
| 7026 | * |
| 7027 | * ```js |
| 7028 | * angular.module('superCache') |
| 7029 | * .factory('superCache', ['$cacheFactory', function($cacheFactory) { |
| 7030 | * return $cacheFactory('super-cache'); |
| 7031 | * }]); |
| 7032 | * ``` |
| 7033 | * |
| 7034 | * Example test: |
| 7035 | * |
| 7036 | * ```js |
| 7037 | * it('should behave like a cache', inject(function(superCache) { |
| 7038 | * superCache.put('key', 'value'); |
| 7039 | * superCache.put('another key', 'another value'); |
| 7040 | * |
| 7041 | * expect(superCache.info()).toEqual({ |
| 7042 | * id: 'super-cache', |
| 7043 | * size: 2 |
| 7044 | * }); |
| 7045 | * |
| 7046 | * superCache.remove('another key'); |
| 7047 | * expect(superCache.get('another key')).toBeUndefined(); |
| 7048 | * |
| 7049 | * superCache.removeAll(); |
| 7050 | * expect(superCache.info()).toEqual({ |
| 7051 | * id: 'super-cache', |
| 7052 | * size: 0 |
| 7053 | * }); |
| 7054 | * })); |
| 7055 | * ``` |
| 7056 | */ |
| 7057 | return (caches[cacheId] = { |
| 7058 | |
| 7059 | /** |
| 7060 | * @ngdoc method |
| 7061 | * @name $cacheFactory.Cache#put |
| 7062 | * @kind function |