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

Commit

Permalink
Merge pull request #8 from NBTSolutions/kelvin/update-cache-size
Browse files Browse the repository at this point in the history
Implement setting cache size after initialization
  • Loading branch information
eSlivinski authored Oct 10, 2017
2 parents bffc461 + 8f60a8a commit 0539bc1
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 4 deletions.
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
6 changes: 6 additions & 0 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
<div>
search: <input id="search" type="text"/>
</div>
<div>
<div>
set cache size: <input id="cache-size-input" type="range" min="0" max="150" value="50"/>
</div>
<div>cache size: <span id="cache-size">50</span></div>
</div>
<div id="map"></div>
<script src="index.js"></script>
</body>
Expand Down
6 changes: 6 additions & 0 deletions example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,10 @@ function main(geojson) {
.filter(c => c.indexOf(q) > -1)
.forEach(id => vtLayer.setFeatureStyle(id, { color: 'black' }));
};

document.getElementById('cache-size-input').onchange = e => {
const cacheSize = +e.target.value;
document.getElementById('cache-size').innerHTML = cacheSize;
vtLayer.setTileCacheSize(cacheSize);
};
}
81 changes: 77 additions & 4 deletions tile_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
*/
class Node {
constructor(data) {
self.data = data;
self.prev = null;
self.next = null;
this.data = data;
this.prev = null;
this.next = null;
}
}

Expand Down Expand Up @@ -44,7 +44,10 @@ export default class TileCache {
}

/**
* Retrieve an item from the cache
*
* @param {string} tileKey
* @returns {Tile|null}
*/
get(tileKey) {
if (!(tileKey in this._cache)) {
Expand Down Expand Up @@ -78,6 +81,7 @@ export default class TileCache {
put(tileKey, tile) {
if (this._debug) {
console.log('tile cache:', 'caching', tileKey);
console.log(this._stringifyList());
}

if (tileKey in this._cache) {
Expand All @@ -93,7 +97,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 @@ -114,9 +118,78 @@ export default class TileCache {
const tailtileKey = tailNode.data.tileKey;
if (this._debug) {
console.log('tile cache:', 'evicting', tileKey);
console.log(this._stringifyList());
}
delete this._cache[tailtileKey];
}
}

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

if (this._debug) {
console.log('tile cache:', 'changing cache size from', this._size, 'to', size);
}

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

this._size = size;

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) {
let { tileKey } = garbage.data;
if (this._debug) {
console.log('tile cache:', 'removing tile', tileKey, 'due to cache resize');
console.log(this._stringifyList());
}
delete this._cache[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
}

/**
* Returns a string reprenting the current order of tiles in the cache
*
* @returns {string}
* @private
*/
_stringifyList() {
let node = this._head;
let out = '';
while (node !== null) {
out += `(coords = ${node.data.tileKey}, feature count = ${Object.keys(this._cache[node.data.tileKey].tile._features).length}) -> `;
node = node.next;
}
return out;
}
}

0 comments on commit 0539bc1

Please sign in to comment.