from time import time from tilefetcher import TileFetcher class TileFetcher_Memory(TileFetcher): def _init(self, cachesize = 256): '''Cachesize is the amount of tiles to keep after an upkeep() call. The count of tiles in cache may temporarily be larger. ''' self.cachesize = cachesize self.tiles = {} def upkeep(self): if len(self.tiles) > self.cachesize: accesstimes = [tile[0] for tile in self.tiles.values()] accesstimes.sort(reverse = True) # oldest last treshold = accesstimes[self.cachesize] for key, value in self.tiles.items(): if value[0] < treshold: del self.tiles[key] def has_tile(self, coords): return self.tiles.has_key(coords) def _get_tile(self, coords, block): '''Only called if has_tile is true''' accesstime, image = self.tiles[coords] # Update accesstime self.tiles[coords] = (time(), image) return image def _put_tile(self, coords, image): self.tiles[coords] = (time(), image)