Skip to content
This repository has been archived by the owner on Jan 31, 2023. It is now read-only.

Commit

Permalink
implement setting cache size after initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvinabrokwa committed Oct 5, 2017
1 parent bffc461 commit 59f2b8a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
11 changes: 11 additions & 0 deletions Leaflet.VectorTiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,17 @@ L.VectorTiles = L.GridLayer.extend({
return this;
},

/**
* Set the maximum size of the cache
*
* @param {number} size
* returns {L.VectorTiles} this
*/
setTileCacheSize(size) {
this._tileCache.setSize(size);
return this;
},

/**
* Convert a GeoJSON feature into a Leaflet feature
* Point -> L.Circle
Expand Down
43 changes: 42 additions & 1 deletion tile_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export default class TileCache {
}

// place at heaad of order linked list
let node = new Node({ tile, tileKey });
let node = new Node({ tileKey });
if (this._head) {
this._head.prev = node;
}
Expand All @@ -118,5 +118,46 @@ export default class TileCache {
delete this._cache[tailtileKey];
}
}

/**
* @param {number} size
*/
setSize(size) {
if (size < 0) {
throw "Size cannot be a negative number";
}

if (size >= this._size) {
// we are increasing the size, no need to remove items from the cache
this._size = size;
return;
}

if (this._head == null) {
// this cache is empty
return;
}

let node = this._head;
let garbage = node;
if (size > 0) {
let c = 1;
while (c < size && node.next != null) {
node = node.next;
c++;
}
garbage = node.next;
node.next = null;
}

// collect the garbage
while (garbage != null) {
delete this._cache[garbage.data.tileKey]; // delete from cache
garbage = garbage.next;
}

// when this function exits, there should be no more references to the garbage
// nodes in the linked list, so they will be garbage collected as usual
}
}

0 comments on commit 59f2b8a

Please sign in to comment.