| 6821 | var caches = {}; |
| 6822 | |
| 6823 | function cacheFactory(cacheId, options) { |
| 6824 | if (cacheId in caches) { |
| 6825 | throw minErr('$cacheFactory')('iid', 'CacheId \'{0}\' is already taken!', cacheId); |
| 6826 | } |
| 6827 | |
| 6828 | var size = 0, |
| 6829 | stats = extend({}, options, {id: cacheId}), |
| 6830 | data = createMap(), |
| 6831 | capacity = (options && options.capacity) || Number.MAX_VALUE, |
| 6832 | lruHash = createMap(), |
| 6833 | freshEnd = null, |
| 6834 | staleEnd = null; |
| 6835 | |
| 6836 | /** |
| 6837 | * @ngdoc type |
| 6838 | * @name $cacheFactory.Cache |
| 6839 | * |
| 6840 | * @description |
| 6841 | * A cache object used to store and retrieve data, primarily used by |
| 6842 | * {@link $templateRequest $templateRequest} and the {@link ng.directive:script script} |
| 6843 | * directive to cache templates and other data. |
| 6844 | * |
| 6845 | * ```js |
| 6846 | * angular.module('superCache') |
| 6847 | * .factory('superCache', ['$cacheFactory', function($cacheFactory) { |
| 6848 | * return $cacheFactory('super-cache'); |
| 6849 | * }]); |
| 6850 | * ``` |
| 6851 | * |
| 6852 | * Example test: |
| 6853 | * |
| 6854 | * ```js |
| 6855 | * it('should behave like a cache', inject(function(superCache) { |
| 6856 | * superCache.put('key', 'value'); |
| 6857 | * superCache.put('another key', 'another value'); |
| 6858 | * |
| 6859 | * expect(superCache.info()).toEqual({ |
| 6860 | * id: 'super-cache', |
| 6861 | * size: 2 |
| 6862 | * }); |
| 6863 | * |
| 6864 | * superCache.remove('another key'); |
| 6865 | * expect(superCache.get('another key')).toBeUndefined(); |
| 6866 | * |
| 6867 | * superCache.removeAll(); |
| 6868 | * expect(superCache.info()).toEqual({ |
| 6869 | * id: 'super-cache', |
| 6870 | * size: 0 |
| 6871 | * }); |
| 6872 | * })); |
| 6873 | * ``` |
| 6874 | */ |
| 6875 | return (caches[cacheId] = { |
| 6876 | |
| 6877 | /** |
| 6878 | * @ngdoc method |
| 6879 | * @name $cacheFactory.Cache#put |
| 6880 | * @kind function |