| 6879 | var caches = {}; |
| 6880 | |
| 6881 | function cacheFactory(cacheId, options) { |
| 6882 | if (cacheId in caches) { |
| 6883 | throw minErr('$cacheFactory')('iid', 'CacheId \'{0}\' is already taken!', cacheId); |
| 6884 | } |
| 6885 | |
| 6886 | var size = 0, |
| 6887 | stats = extend({}, options, {id: cacheId}), |
| 6888 | data = createMap(), |
| 6889 | capacity = (options && options.capacity) || Number.MAX_VALUE, |
| 6890 | lruHash = createMap(), |
| 6891 | freshEnd = null, |
| 6892 | staleEnd = null; |
| 6893 | |
| 6894 | /** |
| 6895 | * @ngdoc type |
| 6896 | * @name $cacheFactory.Cache |
| 6897 | * |
| 6898 | * @description |
| 6899 | * A cache object used to store and retrieve data, primarily used by |
| 6900 | * {@link $templateRequest $templateRequest} and the {@link ng.directive:script script} |
| 6901 | * directive to cache templates and other data. |
| 6902 | * |
| 6903 | * ```js |
| 6904 | * angular.module('superCache') |
| 6905 | * .factory('superCache', ['$cacheFactory', function($cacheFactory) { |
| 6906 | * return $cacheFactory('super-cache'); |
| 6907 | * }]); |
| 6908 | * ``` |
| 6909 | * |
| 6910 | * Example test: |
| 6911 | * |
| 6912 | * ```js |
| 6913 | * it('should behave like a cache', inject(function(superCache) { |
| 6914 | * superCache.put('key', 'value'); |
| 6915 | * superCache.put('another key', 'another value'); |
| 6916 | * |
| 6917 | * expect(superCache.info()).toEqual({ |
| 6918 | * id: 'super-cache', |
| 6919 | * size: 2 |
| 6920 | * }); |
| 6921 | * |
| 6922 | * superCache.remove('another key'); |
| 6923 | * expect(superCache.get('another key')).toBeUndefined(); |
| 6924 | * |
| 6925 | * superCache.removeAll(); |
| 6926 | * expect(superCache.info()).toEqual({ |
| 6927 | * id: 'super-cache', |
| 6928 | * size: 0 |
| 6929 | * }); |
| 6930 | * })); |
| 6931 | * ``` |
| 6932 | */ |
| 6933 | return (caches[cacheId] = { |
| 6934 | |
| 6935 | /** |
| 6936 | * @ngdoc method |
| 6937 | * @name $cacheFactory.Cache#put |
| 6938 | * @kind function |