浏览代码

update LRUCache

Garrett Johnson 5 年之前
父节点
当前提交
a7146b628e
共有 3 个文件被更改,包括 7 次插入7 次删除
  1. 1 2
      src/base/TilesRendererBase.js
  2. 5 4
      src/utilities/LRUCache.js
  3. 1 1
      test/LRUCache.test.js

+ 1 - 2
src/base/TilesRendererBase.js

@@ -10,7 +10,6 @@ import { UNLOADED, LOADING, PARSING, LOADED, FAILED } from './constants.js';
 // TODO: Make sure active state works as expected
 
 // Function for sorting the evicted LRU items. We should evict the shallowest depth first.
-const lruSort = ( a, b ) => a.__depth - b.__depth;
 const priorityCallback = tile => 1 / tile.__depth;
 
 export class TilesRendererBase {
@@ -45,7 +44,7 @@ export class TilesRendererBase {
 		this.fetchOptions = {};
 
 		const lruCache = new LRUCache();
-		lruCache.sortCallback = lruSort;
+		lruCache.unloadPriorityCallback = priorityCallback;
 
 		const downloadQueue = new PriorityQueue();
 		downloadQueue.maxJobs = 4;

+ 5 - 4
src/utilities/LRUCache.js

@@ -23,7 +23,7 @@ class LRUCache {
 		this.itemList = [];
 		this.callbacks = new Map();
 
-		this.sortCallback = null;
+		this.unloadPriorityCallback = null;
 
 	}
 
@@ -120,11 +120,11 @@ class LRUCache {
 		const callbacks = this.callbacks;
 		const unused = itemList.length - usedSet.size;
 		const excess = itemList.length - targetSize;
-		const prioritySortCb = this.sortCallback;
+		const unloadPriorityCallback = this.unloadPriorityCallback;
 
 		if ( excess > 0 && unused > 0 ) {
 
-			if ( prioritySortCb ) {
+			if ( unloadPriorityCallback ) {
 
 				// used items should be at the end of the array
 				itemList.sort( ( a, b ) => {
@@ -139,7 +139,8 @@ class LRUCache {
 					} else if ( ! usedA && ! usedB ) {
 
 						// Use the sort function otherwise
-						return prioritySortCb( a, b );
+						// higher priority should be further to the left
+						return unloadPriorityCallback( b ) - unloadPriorityCallback( a );
 
 					} else {
 

+ 1 - 1
test/LRUCache.test.js

@@ -61,7 +61,7 @@ describe( 'LRUCache', () => {
 	it( 'should sort before unloading', () => {
 
 		const cache = new LRUCache();
-		cache.sortCallback = ( a, b ) => b.priority - a.priority;
+		cache.unloadPriorityCallback = item => item.priority;
 		cache.minSize = 0;
 		cache.maxSize = 10;
 		cache.unloadPercent = 1;