From b9bed7d9dadb4ba1a4186f2ae562f807b21bcf12 Mon Sep 17 00:00:00 2001 From: Alicia Lauerman Date: Sat, 11 Jul 2015 19:29:21 -0400 Subject: [PATCH] fix($cacheFactory): check key exists before decreasing cache size count Previously, there was no check for the existence of an item in the cache when calling `$cacheFactory.remove()` before modifying the cache size count. Closes #12321 Closes #12329 --- src/ng/cacheFactory.js | 11 ++++++----- test/ng/cacheFactorySpec.js | 13 +++++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/ng/cacheFactory.js b/src/ng/cacheFactory.js index f2a55cb3d810..9e5576a7a57d 100644 --- a/src/ng/cacheFactory.js +++ b/src/ng/cacheFactory.js @@ -93,9 +93,9 @@ function $CacheFactoryProvider() { var size = 0, stats = extend({}, options, {id: cacheId}), - data = {}, + data = createMap(), capacity = (options && options.capacity) || Number.MAX_VALUE, - lruHash = {}, + lruHash = createMap(), freshEnd = null, staleEnd = null; @@ -223,6 +223,8 @@ function $CacheFactoryProvider() { delete lruHash[key]; } + if (!(key in data)) return; + delete data[key]; size--; }, @@ -237,9 +239,9 @@ function $CacheFactoryProvider() { * Clears the cache object of any entries. */ removeAll: function() { - data = {}; + data = createMap(); size = 0; - lruHash = {}; + lruHash = createMap(); freshEnd = staleEnd = null; }, @@ -399,4 +401,3 @@ function $TemplateCacheProvider() { return $cacheFactory('templates'); }]; } - diff --git a/test/ng/cacheFactorySpec.js b/test/ng/cacheFactorySpec.js index 7159484d68d4..cb65a1740679 100644 --- a/test/ng/cacheFactorySpec.js +++ b/test/ng/cacheFactorySpec.js @@ -133,6 +133,19 @@ describe('$cacheFactory', function() { expect(cache.info().size).toBe(0); })); + it('should only decrement size when an element is actually removed via remove', inject(function($cacheFactory) { + cache.put('foo', 'bar'); + expect(cache.info().size).toBe(1); + + cache.remove('undefined'); + expect(cache.info().size).toBe(1); + + cache.remove('hasOwnProperty'); + expect(cache.info().size).toBe(1); + + cache.remove('foo'); + expect(cache.info().size).toBe(0); + })); it('should return cache id', inject(function($cacheFactory) { expect(cache.info().id).toBe('test');