src/tilecache.js
function( options ) {
        $.console.assert( options, "[TileCache.cacheTile] options is required" );
        $.console.assert( options.tile, "[TileCache.cacheTile] options.tile is required" );
        $.console.assert( options.tile.cacheKey, "[TileCache.cacheTile] options.tile.cacheKey is required" );
        $.console.assert( options.tiledImage, "[TileCache.cacheTile] options.tiledImage is required" );

        var cutoff = options.cutoff || 0;
        var insertionIndex = this._tilesLoaded.length;

        var imageRecord = this._imagesLoaded[options.tile.cacheKey];
        if (!imageRecord) {
            $.console.assert( options.image, "[TileCache.cacheTile] options.image is required to create an ImageRecord" );
            imageRecord = this._imagesLoaded[options.tile.cacheKey] = new ImageRecord({
                image: options.image
            });

            this._imagesLoadedCount++;
        }

        imageRecord.addTile(options.tile);
        options.tile.cacheImageRecord = imageRecord;

        // Note that just because we're unloading a tile doesn't necessarily mean
        // we're unloading an image. With repeated calls it should sort itself out, though.
        if ( this._imagesLoadedCount > this._maxImageCacheCount ) {
            var worstTile       = null;
            var worstTileIndex  = -1;
            var worstTileRecord = null;
            var prevTile, worstTime, worstLevel, prevTime, prevLevel, prevTileRecord;

            for ( var i = this._tilesLoaded.length - 1; i >= 0; i-- ) {
                prevTileRecord = this._tilesLoaded[ i ];
                prevTile = prevTileRecord.tile;

                if ( prevTile.level <= cutoff || prevTile.beingDrawn ) {
                    continue;
                } else if ( !worstTile ) {
                    worstTile       = prevTile;
                    worstTileIndex  = i;
                    worstTileRecord = prevTileRecord;
                    continue;
                }

                prevTime    = prevTile.lastTouchTime;
                worstTime   = worstTile.lastTouchTime;
                prevLevel   = prevTile.level;
                worstLevel  = worstTile.level;

                if ( prevTime < worstTime ||
                   ( prevTime == worstTime && prevLevel > worstLevel ) ) {
                    worstTile       = prevTile;
                    worstTileIndex  = i;
                    worstTileRecord = prevTileRecord;
                }
            }

            if ( worstTile && worstTileIndex >= 0 ) {
                this._unloadTile(worstTileRecord);
                insertionIndex = worstTileIndex;
            }
        }

        this._tilesLoaded[ insertionIndex ] = new TileRecord({
            tile: options.tile,
            tiledImage: options.tiledImage
        });
    }
function( options ) {
        $.console.assert( options, "[TileCache.cacheTile] options is required" );
        $.console.assert( options.tile, "[TileCache.cacheTile] options.tile is required" );
        $.console.assert( options.tile.cacheKey, "[TileCache.cacheTile] options.tile.cacheKey is required" );
        $.console.assert( options.tiledImage, "[TileCache.cacheTile] options.tiledImage is required" );

        var cutoff = options.cutoff || 0;
        var insertionIndex = this._tilesLoaded.length;

        var imageRecord = this._imagesLoaded[options.tile.cacheKey];
        if (!imageRecord) {
            $.console.assert( options.image, "[TileCache.cacheTile] options.image is required to create an ImageRecord" );
            imageRecord = this._imagesLoaded[options.tile.cacheKey] = new ImageRecord({
                image: options.image
            });

            this._imagesLoadedCount++;
        }

        imageRecord.addTile(options.tile);
        options.tile.cacheImageRecord = imageRecord;

        // Note that just because we're unloading a tile doesn't necessarily mean
        // we're unloading an image. With repeated calls it should sort itself out, though.
        if ( this._imagesLoadedCount > this._maxImageCacheCount ) {
            var worstTile       = null;
            var worstTileIndex  = -1;
            var worstTileRecord = null;
            var prevTile, worstTime, worstLevel, prevTime, prevLevel, prevTileRecord;

            for ( var i = this._tilesLoaded.length - 1; i >= 0; i-- ) {
                prevTileRecord = this._tilesLoaded[ i ];
                prevTile = prevTileRecord.tile;

                if ( prevTile.level <= cutoff || prevTile.beingDrawn ) {
                    continue;
                } else if ( !worstTile ) {
                    worstTile       = prevTile;
                    worstTileIndex  = i;
                    worstTileRecord = prevTileRecord;
                    continue;
                }

                prevTime    = prevTile.lastTouchTime;
                worstTime   = worstTile.lastTouchTime;
                prevLevel   = prevTile.level;
                worstLevel  = worstTile.level;

                if ( prevTime < worstTime ||
                   ( prevTime == worstTime && prevLevel > worstLevel ) ) {
                    worstTile       = prevTile;
                    worstTileIndex  = i;
                    worstTileRecord = prevTileRecord;
                }
            }

            if ( worstTile && worstTileIndex >= 0 ) {
                this._unloadTile(worstTileRecord);
                insertionIndex = worstTileIndex;
            }
        }

        this._tilesLoaded[ insertionIndex ] = new TileRecord({
            tile: options.tile,
            tiledImage: options.tiledImage
        });
    }
function ( options ) { $ . console . assert ( options , "[TileCache.cacheTile] options is required" ) ; $ . console . assert ( options . tile , "[TileCache.cacheTile] options.tile is required" ) ; $ . console . assert ( options . tile . cacheKey , "[TileCache.cacheTile] options.tile.cacheKey is required" ) ; $ . console . assert ( options . tiledImage , "[TileCache.cacheTile] options.tiledImage is required" ) ; var cutoff = options . cutoff || 0 ; var insertionIndex = this . _tilesLoaded . length ; var imageRecord = this . _imagesLoaded [ options . tile . cacheKey ] ; if ( ! imageRecord ) { $ . console . assert ( options . image , "[TileCache.cacheTile] options.image is required to create an ImageRecord" ) ; imageRecord = this . _imagesLoaded [ options . tile . cacheKey ] = new ImageRecord ( { image : options . image } ) ; this . _imagesLoadedCount ++ ; } imageRecord . addTile ( options . tile ) ; options . tile . cacheImageRecord = imageRecord ; // Note that just because we're unloading a tile doesn't necessarily mean // we're unloading an image. With repeated calls it should sort itself out, though. if ( this . _imagesLoadedCount > this . _maxImageCacheCount ) { var worstTile = null ; var worstTileIndex = - 1 ; var worstTileRecord = null ; var prevTile , worstTime , worstLevel , prevTime , prevLevel , prevTileRecord ; for ( var i = this . _tilesLoaded . length - 1 ; i >= 0 ; i -- ) { prevTileRecord = this . _tilesLoaded [ i ] ; prevTile = prevTileRecord . tile ; if ( prevTile . level <= cutoff || prevTile . beingDrawn ) { continue ; } else if ( ! worstTile ) { worstTile = prevTile ; worstTileIndex = i ; worstTileRecord = prevTileRecord ; continue ; } prevTime = prevTile . lastTouchTime ; worstTime = worstTile . lastTouchTime ; prevLevel = prevTile . level ; worstLevel = worstTile . level ; if ( prevTime < worstTime || ( prevTime == worstTime && prevLevel > worstLevel ) ) { worstTile = prevTile ; worstTileIndex = i ; worstTileRecord = prevTileRecord ; } } if ( worstTile && worstTileIndex >= 0 ) { this . _unloadTile ( worstTileRecord ) ; insertionIndex = worstTileIndex ; } } this . _tilesLoaded [ insertionIndex ] = new TileRecord ( { tile : options . tile , tiledImage : options . tiledImage } ) ; }
Caches the specified tile, removing an old tile if necessary to stay under the
maxImageCacheCount specified on construction. Note that if multiple tiles reference
the same image, there may be more tiles than maxImageCacheCount; the goal is to keep
the number of images below that number. Note, as well, that even the number of images
may temporarily surpass that number, but should eventually come back down to the max specified.
@param {Object} options - Tile info.
@param {OpenSeadragon.Tile} options.tile - The tile to cache.
@param {String} options.tile.cacheKey - The unique key used to identify this tile in the cache.
@param {Image} options.image - The image of the tile to cache.
@param {OpenSeadragon.TiledImage} options.tiledImage - The TiledImage that owns that tile.
@param {Number} [options.cutoff=0] - If adding this tile goes over the cache max count, this
function will release an old tile. The cutoff option specifies a tile level at or below which
tiles will not be released.
Caches the specified tile removing an old tile if necessary to stay under the maxImageCacheCount specified on construction . Note that if multiple tiles reference the same image there may be more tiles than maxImageCacheCount ; the goal is to keep the number of images below that number . Note as well that even the number of images may temporarily surpass that number but should eventually come back down to the max specified .