Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 2a5a52a

Browse files
tealtailpetebacondarwin
authored andcommitted
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
1 parent c690946 commit 2a5a52a

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/ng/cacheFactory.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ function $CacheFactoryProvider() {
9393

9494
var size = 0,
9595
stats = extend({}, options, {id: cacheId}),
96-
data = {},
96+
data = createMap(),
9797
capacity = (options && options.capacity) || Number.MAX_VALUE,
98-
lruHash = {},
98+
lruHash = createMap(),
9999
freshEnd = null,
100100
staleEnd = null;
101101

@@ -223,6 +223,8 @@ function $CacheFactoryProvider() {
223223
delete lruHash[key];
224224
}
225225

226+
if (!(key in data)) return;
227+
226228
delete data[key];
227229
size--;
228230
},
@@ -237,9 +239,9 @@ function $CacheFactoryProvider() {
237239
* Clears the cache object of any entries.
238240
*/
239241
removeAll: function() {
240-
data = {};
242+
data = createMap();
241243
size = 0;
242-
lruHash = {};
244+
lruHash = createMap();
243245
freshEnd = staleEnd = null;
244246
},
245247

@@ -399,4 +401,3 @@ function $TemplateCacheProvider() {
399401
return $cacheFactory('templates');
400402
}];
401403
}
402-

test/ng/cacheFactorySpec.js

+13
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,19 @@ describe('$cacheFactory', function() {
133133
expect(cache.info().size).toBe(0);
134134
}));
135135

136+
it('should only decrement size when an element is actually removed via remove', inject(function($cacheFactory) {
137+
cache.put('foo', 'bar');
138+
expect(cache.info().size).toBe(1);
139+
140+
cache.remove('undefined');
141+
expect(cache.info().size).toBe(1);
142+
143+
cache.remove('hasOwnProperty');
144+
expect(cache.info().size).toBe(1);
145+
146+
cache.remove('foo');
147+
expect(cache.info().size).toBe(0);
148+
}));
136149

137150
it('should return cache id', inject(function($cacheFactory) {
138151
expect(cache.info().id).toBe('test');

0 commit comments

Comments
 (0)