Maps > Using Load-On-Demand > Writing a New Cache Algorithm
 
Writing a New Cache Algorithm
This section explains how to write a customized cache algorithm to meet specific application requirements. The example in this section is a simplified version of the class IlvDefaultTileCache provided in the Rogue Wave® Views Maps library. The complete source code for this example can be found in the following file.
The class SimpleTileCache extends the class IlvTileCache.
 
class TileCache
:public IlvTileCache
{
public:
TileCache(int size);
TileCache::TileCache(IlvInputFile& file);
virtual void tileAboutToLoad(IlvTile *);
virtual void tileCached(IlvTile *);
virtual void tileRetrieved(class IlvTile *);
virtual void controllerDeleted(IlvTileController *);
virtual void write(IlvOutputFile& output) const;
private:
int _size;
IlList _tiles;
};
 
_size defines the maximum number of tiles that can be stored in the cache.
 
TileCache::TileCache(int size)
:IlvTileCache(),
_size(size)
{
}
 
The TileCache constructor creates an instance of the default cache with the specified size.
The following constructor reads the cache from the provided input stream. Caches created using this constructor are persistent and can thus be saved with an IlvTiledLayer object.
 
TileCache::TileCache(IlvInputFile& file)
:IlvTileCache()
{
file.getStream() >> _size;
}
 
The write method writes the cache to an output stream.
 
void
TileCache::write(IlvOutputFile& output) const
{
output.getStream() << _size;
}
 
The following method belongs to the IlvTileCache interface. It is called when a tile is cached. In this implementation, the tile is added at the end of the internal tile list.
 
void
TileCache::tileCached(IlvTile *tile)
{
_tiles.append(tile);
}
 
The following method belongs to the IlvTileCache interface. It is called when a tile is removed from the cache and locked again. With this implementation, the tile is removed from the internal tile list.
 
void
TileCache::tileRetrieved(IlvTile* tile)
{
_tiles.remove(tile);
}
 
The following method belongs to the IlvTileCache abstract class. It is called when a tile is about to be loaded. With this implementation, if the number of tiles in the cache exceeds the cache size, the least recently used tiles, at the top the internal tile list, will be unloaded to make room for new tiles.
 
void
TileCache::tileAboutToLoad(IlvTile *tile)
{
int toRemove = _tiles.length() - _size;
if (toRemove <= 0)
return;
for (int i = toRemove; i > 0; i--) {
IlvTile* current = (IlvTile*)_tiles[0];
_tiles.remove(current);
releaseTile(current);
}
}
 
The following method belongs to the IlvTileCache abstract class. It is called when a tiled layer is taken out of the manager to remove the tiles managed by its tile controller from the cache.
 
void
TileCache::controllerDeleted(IlvTileController* controller)
{
IlvLink* l;
l = _tiles.getFirst();
IlvTile* tile;
while (l) {
tile = (IlvTile*) l->getValue();
l = l->getNext();
if (tile->getController() == controller)
_tiles.remove(tile);
}
}
 

Version 5.8
Copyright © 2014, Rogue Wave Software, Inc. All Rights Reserved.