| 6232 | var caches = {}; |
| 6233 | |
| 6234 | function cacheFactory(cacheId, options) { |
| 6235 | if (cacheId in caches) { |
| 6236 | throw minErr('$cacheFactory')('iid', "CacheId '{0}' is already taken!", cacheId); |
| 6237 | } |
| 6238 | |
| 6239 | var size = 0, |
| 6240 | stats = extend({}, options, { id: cacheId }), |
| 6241 | data = createMap(), |
| 6242 | capacity = (options && options.capacity) || Number.MAX_VALUE, |
| 6243 | lruHash = createMap(), |
| 6244 | freshEnd = null, |
| 6245 | staleEnd = null; |
| 6246 | |
| 6247 | /** |
| 6248 | * @ngdoc type |
| 6249 | * @name $cacheFactory.Cache |
| 6250 | * |
| 6251 | * @description |
| 6252 | * A cache object used to store and retrieve data, primarily used by |
| 6253 | * {@link $http $http} and the {@link ng.directive:script script} directive to cache |
| 6254 | * templates and other data. |
| 6255 | * |
| 6256 | * ```js |
| 6257 | * angular.module('superCache') |
| 6258 | * .factory('superCache', ['$cacheFactory', function($cacheFactory) { |
| 6259 | * return $cacheFactory('super-cache'); |
| 6260 | * }]); |
| 6261 | * ``` |
| 6262 | * |
| 6263 | * Example test: |
| 6264 | * |
| 6265 | * ```js |
| 6266 | * it('should behave like a cache', inject(function(superCache) { |
| 6267 | * superCache.put('key', 'value'); |
| 6268 | * superCache.put('another key', 'another value'); |
| 6269 | * |
| 6270 | * expect(superCache.info()).toEqual({ |
| 6271 | * id: 'super-cache', |
| 6272 | * size: 2 |
| 6273 | * }); |
| 6274 | * |
| 6275 | * superCache.remove('another key'); |
| 6276 | * expect(superCache.get('another key')).toBeUndefined(); |
| 6277 | * |
| 6278 | * superCache.removeAll(); |
| 6279 | * expect(superCache.info()).toEqual({ |
| 6280 | * id: 'super-cache', |
| 6281 | * size: 0 |
| 6282 | * }); |
| 6283 | * })); |
| 6284 | * ``` |
| 6285 | */ |
| 6286 | return caches[cacheId] = { |
| 6287 | /** |
| 6288 | * @ngdoc method |
| 6289 | * @name $cacheFactory.Cache#put |
| 6290 | * @kind function |
| 6291 | * |