Skip to content

Commit 7db474e

Browse files
Merge pull request #336 from ahocevar/fix-cache-handling
Fix cache handling issues that caused an AggregateError
2 parents 00fd19a + 0f380e0 commit 7db474e

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,8 @@ const multiTiff = await fromUrls(
352352

353353
Geotiff.js supports the use of [`AbortController`s](https://developer.mozilla.org/en-US/docs/Web/API/AbortController). Calls to `getRasters`, `readRGB` and `getTileOrStrip` will throw an `Error` with name `AbortSignal` similar to the browser's `fetch` behavior.
354354

355-
You need to set the `cacheSize` parameter to `0` to enable this feature due to cache consistency issues - otherwise, once the cache becomes full,
356-
it will lose its consistency.
357-
358355
```javascript
359-
const tiff = await fromUrl(source, { cacheSize: 0 });
356+
const tiff = await fromUrl(source);
360357
const abortController = new AbortController();
361358
const { signal } = abortController;
362359
abortController.abort();

src/source/blockedsource.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,15 @@ export class BlockedSource extends BaseSource {
5050
this.source = source;
5151
this.blockSize = blockSize;
5252

53-
this.blockCache = new QuickLRU({ maxSize: cacheSize });
53+
this.blockCache = new QuickLRU({
54+
maxSize: cacheSize,
55+
onEviction: (blockId, block) => {
56+
this.evictedBlocks.set(blockId, block);
57+
},
58+
});
59+
60+
/** @type {Map<number, Block>} */
61+
this.evictedBlocks = new Map();
5462

5563
// mapping blockId -> Block instance
5664
this.blockRequests = new Map();
@@ -73,6 +81,7 @@ export class BlockedSource extends BaseSource {
7381
const blockRequests = [];
7482
const missingBlockIds = [];
7583
const allBlockIds = [];
84+
this.evictedBlocks.clear();
7685

7786
for (const { offset, length } of slices) {
7887
let top = offset + length;
@@ -138,7 +147,7 @@ export class BlockedSource extends BaseSource {
138147
throw new AbortError('Request was aborted');
139148
}
140149

141-
const blocks = allBlockIds.map((id) => this.blockCache.get(id));
150+
const blocks = allBlockIds.map((id) => this.blockCache.get(id) || this.evictedBlocks.get(id));
142151
const failedBlocks = blocks.filter((i) => !i);
143152
if (failedBlocks.length) {
144153
throw new AggregateError(failedBlocks, 'Request failed');

0 commit comments

Comments
 (0)