diff --git a/src/ol/style/iconimagecache.js b/src/ol/style/iconimagecache.js index 58b1baac69..64f621ec11 100644 --- a/src/ol/style/iconimagecache.js +++ b/src/ol/style/iconimagecache.js @@ -21,7 +21,6 @@ ol.style.IconImageCache = function() { this.cacheSize_ = 0; /** - * @const * @type {number} * @private */ @@ -91,3 +90,13 @@ ol.style.IconImageCache.prototype.set = function(src, crossOrigin, color, iconIm this.cache_[key] = iconImage; ++this.cacheSize_; }; + + +/** + * Set cache max size. + * @param {number} maxCacheSize Cache max size. + */ +ol.style.IconImageCache.prototype.setMaxCacheSize = function(maxCacheSize) { + this.maxCacheSize_ = maxCacheSize; + this.expire(); +}; diff --git a/test/spec/ol/style/iconimagecache.test.js b/test/spec/ol/style/iconimagecache.test.js index ef47345275..1c023470c3 100644 --- a/test/spec/ol/style/iconimagecache.test.js +++ b/test/spec/ol/style/iconimagecache.test.js @@ -66,4 +66,25 @@ describe('ol.style.IconImageCache', function() { expect(cache.get('4', null, null)).to.not.be(null); }); }); + + describe('#setMaxCacheSize', function() { + it('sets max cache size and expires cache', function() { + var cache = ol.style.iconImageCache; + + var i, src, iconImage; + + for (i = 0; i < 3; ++i) { + src = i + ''; + iconImage = new ol.style.IconImage(null, src); + cache.set(src, null, null, iconImage); + } + + expect(cache.cacheSize_).to.eql(3); + + cache.setMaxCacheSize(2); + + expect(cache.maxCacheSize_).to.eql(2); + expect(cache.cacheSize_).to.eql(2); + }); + }); });